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
24# 2021-03-23: hacky workaround for weird issue with missing .so libs
25# if we build the aar twice after cleaning, the libs will be packaged correctly
26./gradlew assembleRelease
27popd
28
29mkdir -p expoview/libs
30# Grab the aar and unzip it
31cp versioned-react-native/ReactAndroid/build/outputs/aar/ReactAndroid-release.aar expoview/libs/ReactAndroid-temp.aar
32rm -rf expoview/libs/ReactAndroid-temp
33unzip expoview/libs/ReactAndroid-temp.aar -d expoview/libs/ReactAndroid-temp
34
35# Write jarjar rules file. Used in next step to rename package
36rm -f jarjar-rules.txt
37
38while read PACKAGE
39do
40  echo "rule $PACKAGE.** temporarydontversion.$PACKAGE.@1" >> jarjar-rules.txt
41done < $TOOLS_DIR/android-packages-to-keep.txt
42
43while read PACKAGE
44do
45  echo "rule $PACKAGE.** $ABI_VERSION.$PACKAGE.@1" >> jarjar-rules.txt
46done < $TOOLS_DIR/android-packages-to-rename.txt
47
48# Rename packages in jars
49java -jar $TOOLS_DIR/jarjar-1.4.1.jar process jarjar-rules.txt expoview/libs/ReactAndroid-temp/classes.jar expoview/libs/ReactAndroid-temp/classes.jar
50
51# fix annotations
52unzip expoview/libs/ReactAndroid-temp/annotations.zip -d expoview/libs/ReactAndroid-temp/annotations
53mv expoview/libs/ReactAndroid-temp/annotations/com expoview/libs/ReactAndroid-temp/annotations/$ABI_VERSION
54find expoview/libs/ReactAndroid-temp/annotations -type f > annotations_xmls
55while read FILE
56do
57  while read PACKAGE
58  do
59    sed -i '' "s/$PACKAGE/$ABI_VERSION.$PACKAGE/g" $FILE
60  done < $TOOLS_DIR/android-packages-to-rename.txt
61done < annotations_xmls
62rm annotations_xmls
63pushd expoview/libs/ReactAndroid-temp/annotations
64rm ../annotations.zip
65zip -r ../annotations.zip .
66rm -rf expoview/libs/ReactAndroid-temp/annotations
67popd
68
69# Fix packages that we don't want renamed
70rm -f jarjar-rules.txt
71
72while read PACKAGE
73do
74  echo "rule temporarydontversion.$PACKAGE.** $PACKAGE.@1" >> jarjar-rules.txt
75done < $TOOLS_DIR/android-packages-to-keep.txt
76
77java -jar $TOOLS_DIR/jarjar-1.4.1.jar process jarjar-rules.txt expoview/libs/ReactAndroid-temp/classes.jar expoview/libs/ReactAndroid-temp/classes.jar
78
79
80pushd expoview/libs/ReactAndroid-temp
81# Update the manifest. This is used for deduping in the build process
82sed -i '' "s/com\.facebook\.react/$ABI_VERSION\.com\.facebook\.react/g" AndroidManifest.xml
83
84# Can't rename libc++_shared so remove. We share the copy from ReactAndroid.
85rm -rf jni/*/libc++_shared.so
86
87# Zip into aar
88set +e
89rm ../ReactAndroid-release-$ABI_VERSION.aar
90set -e
91zip -r ../ReactAndroid-release-$ABI_VERSION.aar .
92popd
93rm -rf expoview/libs/ReactAndroid-temp
94
95# Put aar into local maven repo
96mvn 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
97rm expoview/libs/ReactAndroid-temp.aar
98mkdir -p versioned-abis/expoview-$ABI_VERSION/maven/host/exp/reactandroid-$ABI_VERSION
99cp -r ~/.m2/repository/host/exp/reactandroid-$ABI_VERSION/ versioned-abis/expoview-$ABI_VERSION/maven/host/exp/reactandroid-$ABI_VERSION
100rm expoview/libs/ReactAndroid-release-$ABI_VERSION.aar
101
102# Add new aar to expoview/build.gradle
103NEWLINE='\
104'
105SED_APPEND_COMMAND=" a$NEWLINE"
106REPLACE_TEXT='DO NOT MODIFY'
107ADD_NEW_SDKS_HERE='ADD_NEW_SDKS_HERE'
108sed -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
109sed -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
110
111# Update Constants.java
112sed -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
113
114# Add new version
115
116# Update places that need to reference all versions of react native
117# THIS WILL PROBABLY BREAK OFTEN
118
119# Update classes that implement DefaultHardwareBackBtnHandler or PermissionAwareActivity
120BACK_BUTTON_HANDLER_CLASS='com.facebook.react.modules.core.DefaultHardwareBackBtnHandler'
121PERMISSION_AWARE_ACTIVITY_CLASS='com.facebook.react.modules.core.PermissionAwareActivity'
122PERMISSION_LISTENER_CLASS='com.facebook.react.modules.core.PermissionListener'
123find 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/"
124# Add implementation of PermissionAwareActivity.requestPermissions
125find 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/"
126
127popd
128