1#!/bin/bash
2# Usage: ./android-build-aar 4.0.0 builds versioned-react-native and packages it into an aar
3# with packages renamed. Must rename the JNI code before using this.
4# Requires $EXPO_ROOT_DIR to be defined in the environment.
5
6ORIGINAL_ABI_VERSION=`echo $1`
7MAJOR_ABI_VERSION=`echo $1 | sed 's/\..*//g'`
8ABI_VERSION_NUMBER=`echo $1 | sed 's/\./_/g'`
9ABI_VERSION="abi$ABI_VERSION_NUMBER"
10TOOLS_DIR=`pwd`
11
12pushd $EXPO_ROOT_DIR/android
13
14# Clean aar
15rm -rf expoview/libs/ReactAndroid-temp
16rm versioned-react-native/ReactAndroid/build/outputs/aar/ReactAndroid-release.aar
17
18# Build aar
19pushd versioned-react-native
20# The build directory sometimes has old .so files
21rm -rf ReactAndroid/build
22set -e
23./gradlew assembleRelease
24popd
25
26mkdir -p expoview/libs
27# Grab the aar and unzip it
28cp versioned-react-native/ReactAndroid/build/outputs/aar/ReactAndroid-release.aar expoview/libs/ReactAndroid-temp.aar
29rm -rf expoview/libs/ReactAndroid-temp
30unzip expoview/libs/ReactAndroid-temp.aar -d expoview/libs/ReactAndroid-temp
31
32# Write jarjar rules file. Used in next step to rename package
33rm -f jarjar-rules.txt
34
35while read PACKAGE
36do
37  echo "rule $PACKAGE.** temporarydontversion.$PACKAGE.@1" >> jarjar-rules.txt
38done < $TOOLS_DIR/android-packages-to-keep.txt
39
40while read PACKAGE
41do
42  echo "rule $PACKAGE.** $ABI_VERSION.$PACKAGE.@1" >> jarjar-rules.txt
43done < $TOOLS_DIR/android-packages-to-rename.txt
44
45# Rename packages in jars
46java -jar $TOOLS_DIR/jarjar-1.4.1.jar process jarjar-rules.txt expoview/libs/ReactAndroid-temp/classes.jar expoview/libs/ReactAndroid-temp/classes.jar
47
48# fix annotations
49unzip expoview/libs/ReactAndroid-temp/annotations.zip -d expoview/libs/ReactAndroid-temp/annotations
50mv expoview/libs/ReactAndroid-temp/annotations/com expoview/libs/ReactAndroid-temp/annotations/$ABI_VERSION
51find expoview/libs/ReactAndroid-temp/annotations -type f > annotations_xmls
52while read FILE
53do
54  while read PACKAGE
55  do
56    sed -i '' "s/$PACKAGE/$ABI_VERSION.$PACKAGE/g" $FILE
57  done < $TOOLS_DIR/android-packages-to-rename.txt
58done < annotations_xmls
59rm annotations_xmls
60pushd expoview/libs/ReactAndroid-temp/annotations
61rm ../annotations.zip
62zip -r ../annotations.zip .
63rm -rf expoview/libs/ReactAndroid-temp/annotations
64popd
65
66# Fix packages that we don't want renamed
67rm -f jarjar-rules.txt
68
69while read PACKAGE
70do
71  echo "rule temporarydontversion.$PACKAGE.** $PACKAGE.@1" >> jarjar-rules.txt
72done < $TOOLS_DIR/android-packages-to-keep.txt
73
74java -jar $TOOLS_DIR/jarjar-1.4.1.jar process jarjar-rules.txt expoview/libs/ReactAndroid-temp/classes.jar expoview/libs/ReactAndroid-temp/classes.jar
75
76
77pushd expoview/libs/ReactAndroid-temp
78# Update the manifest. This is used for deduping in the build process
79sed -i '' "s/com\.facebook\.react/$ABI_VERSION\.com\.facebook\.react/g" AndroidManifest.xml
80
81# Can't rename libc++_shared so remove. We share the copy from ReactAndroid.
82rm -rf jni/*/libc++_shared.so
83
84# Zip into aar
85set +e
86rm ../ReactAndroid-release-$ABI_VERSION.aar
87set -e
88zip -r ../ReactAndroid-release-$ABI_VERSION.aar .
89popd
90rm -rf expoview/libs/ReactAndroid-temp
91
92# Put aar into local maven repo
93mvn install:install-file -e -Dfile=expoview/libs/ReactAndroid-release-$ABI_VERSION.aar -DgroupId=host.exp -DartifactId=reactandroid-$ABI_VERSION -Dversion=1.0.0 -Dpackaging=aar
94rm expoview/libs/ReactAndroid-temp.aar
95mkdir -p versioned-abis/expoview-$ABI_VERSION/maven/host/exp/reactandroid-$ABI_VERSION
96cp -r ~/.m2/repository/host/exp/reactandroid-$ABI_VERSION/ versioned-abis/expoview-$ABI_VERSION/maven/host/exp/reactandroid-$ABI_VERSION
97rm expoview/libs/ReactAndroid-release-$ABI_VERSION.aar
98
99# Add new aar to expoview/build.gradle
100NEWLINE='\
101'
102SED_APPEND_COMMAND=" a$NEWLINE"
103REPLACE_TEXT='DO NOT MODIFY'
104ADD_NEW_SDKS_HERE='ADD_NEW_SDKS_HERE'
105sed -i '' "/$ADD_NEW_SDKS_HERE/$SED_APPEND_COMMAND$NEWLINE$NEWLINE\ \ \/\/ BEGIN_SDK_$MAJOR_ABI_VERSION$NEWLINE\ \ implementation(project(':expoview-$ABI_VERSION'))$NEWLINE\ \ \/\/ END_SDK_$MAJOR_ABI_VERSION$NEWLINE" app/build.gradle
106sed -i '' "/$REPLACE_TEXT/$SED_APPEND_COMMAND\ \ \/\/ BEGIN_SDK_$MAJOR_ABI_VERSION$NEWLINE\ \ api 'host.exp:reactandroid-$ABI_VERSION:1.0.0'$NEWLINE\ \ \/\/ END_SDK_$MAJOR_ABI_VERSION$NEWLINE" expoview/build.gradle
107
108# Update Constants.java
109sed -i '' "/$REPLACE_TEXT/$SED_APPEND_COMMAND\ \ \ \ \/\/ BEGIN_SDK_$MAJOR_ABI_VERSION$NEWLINE\ \ \ \ abiVersions.add(\"$ORIGINAL_ABI_VERSION\");$NEWLINE\ \ \ \ \/\/ END_SDK_$MAJOR_ABI_VERSION$NEWLINE" expoview/src/main/java/host/exp/exponent/Constants.java
110
111# Add new version
112
113# Update places that need to reference all versions of react native
114# THIS WILL PROBABLY BREAK OFTEN
115
116# Update classes that implement DefaultHardwareBackBtnHandler or PermissionAwareActivity
117BACK_BUTTON_HANDLER_CLASS='com.facebook.react.modules.core.DefaultHardwareBackBtnHandler'
118PERMISSION_AWARE_ACTIVITY_CLASS='com.facebook.react.modules.core.PermissionAwareActivity'
119PERMISSION_LISTENER_CLASS='com.facebook.react.modules.core.PermissionListener'
120find expoview/src/main/java/host/exp/exponent -iname '*.java' -type f -print0 | xargs -0 sed -i '' -e "s/ADD_NEW_SDKS_HERE/BEGIN_SDK_$MAJOR_ABI_VERSION$NEWLINE\ \ \ \ $ABI_VERSION\.$BACK_BUTTON_HANDLER_CLASS,$NEWLINE\ \ \ \ $ABI_VERSION\.$PERMISSION_AWARE_ACTIVITY_CLASS,$NEWLINE\ \ \ \ \/\/ END_SDK_$MAJOR_ABI_VERSION$NEWLINE\ \ \ \ \/\/ ADD_NEW_SDKS_HERE/"
121# Add implementation of PermissionAwareActivity.requestPermissions
122find expoview/src/main/java/host/exp/exponent -iname '*.java' -type f -print0 | xargs -0 sed -i '' "s/ADD_NEW_PERMISSION_AWARE_ACTIVITY_IMPLEMENTATION_HERE/BEGIN_SDK_$MAJOR_ABI_VERSION$NEWLINE\ \ \ \ @Override$NEWLINE\ \ \ \ public\ void\ requestPermissions(String\[\]\ strings\,\ int\ i\,\ $ABI_VERSION\.$PERMISSION_LISTENER_CLASS\ permissionListener)\ {$NEWLINE\ \ \ \ \ \ super\.requestPermissions(strings\,\ i\,\ permissionListener::onRequestPermissionsResult);$NEWLINE\ \ \ \ }$NEWLINE\ \ \ \ \/\/ END_SDK_$MAJOR_ABI_VERSION$NEWLINE\ \ \ \ \/\/ ADD_NEW_PERMISSION_AWARE_ACTIVITY_IMPLEMENTATION_HERE/"
123
124popd
125