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# Sanity check for renaming all the native libraries,
36# but excluding prebuilt libraries like libc++_shared.so and libfbjni.so.
37set +e
38UNRENAMED_LIBS=`unzip -l expoview/libs/ReactAndroid-temp.aar | grep 'lib.*\.so' | grep -v "$ABI_VERSION" | grep -v 'libc++_shared.so' | grep -v 'libfbjni.so'`
39if [ "x$UNRENAMED_LIBS" != "x" ]; then
40  echo -e "\n\n�� The following libraries have not been renamed. Please make sure to update \`tools/src/versioning/android/libraries.ts\`."
41  echo "$UNRENAMED_LIBS"
42  exit 1
43fi
44unset UNRENAMED_LIBS
45set -e
46
47# Write jarjar rules file. Used in next step to rename package
48rm -f jarjar-rules.txt
49
50while read PACKAGE
51do
52  echo "rule $PACKAGE.** temporarydontversion.$PACKAGE.@1" >> jarjar-rules.txt
53done < $TOOLS_DIR/android-packages-to-keep.txt
54
55while read PACKAGE
56do
57  echo "rule $PACKAGE.** $ABI_VERSION.$PACKAGE.@1" >> jarjar-rules.txt
58done < $TOOLS_DIR/android-packages-to-rename.txt
59
60# Rename packages in jars
61java -jar $TOOLS_DIR/jarjar-1.4.1.jar process jarjar-rules.txt expoview/libs/ReactAndroid-temp/classes.jar expoview/libs/ReactAndroid-temp/classes.jar
62
63# fix annotations
64unzip expoview/libs/ReactAndroid-temp/annotations.zip -d expoview/libs/ReactAndroid-temp/annotations
65mv expoview/libs/ReactAndroid-temp/annotations/com expoview/libs/ReactAndroid-temp/annotations/$ABI_VERSION
66find expoview/libs/ReactAndroid-temp/annotations -type f > annotations_xmls
67while read FILE
68do
69  while read PACKAGE
70  do
71    sed -i '' "s/$PACKAGE/$ABI_VERSION.$PACKAGE/g" $FILE
72  done < $TOOLS_DIR/android-packages-to-rename.txt
73done < annotations_xmls
74rm annotations_xmls
75pushd expoview/libs/ReactAndroid-temp/annotations
76rm ../annotations.zip
77zip -r ../annotations.zip .
78rm -rf expoview/libs/ReactAndroid-temp/annotations
79popd
80
81# Fix packages that we don't want renamed
82rm -f jarjar-rules.txt
83
84while read PACKAGE
85do
86  echo "rule temporarydontversion.$PACKAGE.** $PACKAGE.@1" >> jarjar-rules.txt
87done < $TOOLS_DIR/android-packages-to-keep.txt
88
89java -jar $TOOLS_DIR/jarjar-1.4.1.jar process jarjar-rules.txt expoview/libs/ReactAndroid-temp/classes.jar expoview/libs/ReactAndroid-temp/classes.jar
90
91
92pushd expoview/libs/ReactAndroid-temp
93# Update the manifest. This is used for deduping in the build process
94sed -i '' "s/com\.facebook\.react/$ABI_VERSION\.com\.facebook\.react/g" AndroidManifest.xml
95
96# Can't rename libc++_shared so remove. We share the copy from ReactAndroid.
97rm -rf jni/*/libc++_shared.so
98
99# Zip into aar
100set +e
101rm ../ReactAndroid-release-$ABI_VERSION.aar
102set -e
103zip -r ../ReactAndroid-release-$ABI_VERSION.aar .
104popd
105rm -rf expoview/libs/ReactAndroid-temp
106
107# Put aar into local maven repo
108mvn 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
109rm expoview/libs/ReactAndroid-temp.aar
110mkdir -p versioned-abis/expoview-$ABI_VERSION/maven/host/exp/reactandroid-$ABI_VERSION
111cp -r ~/.m2/repository/host/exp/reactandroid-$ABI_VERSION/ versioned-abis/expoview-$ABI_VERSION/maven/host/exp/reactandroid-$ABI_VERSION
112rm expoview/libs/ReactAndroid-release-$ABI_VERSION.aar
113
114# Add new aar to expoview/build.gradle
115NEWLINE='\
116'
117SED_APPEND_COMMAND=" a$NEWLINE"
118REPLACE_TEXT='DO NOT MODIFY'
119ADD_NEW_SDKS_HERE='ADD_NEW_SDKS_HERE'
120sed -i '' "/$ADD_NEW_SDKS_HERE/$SED_APPEND_COMMAND$NEWLINE$NEWLINE\ \ \/\/ BEGIN_SDK_$MAJOR_ABI_VERSION$NEWLINE\ \ versionedImplementation(project(':expoview-$ABI_VERSION'))$NEWLINE\ \ \/\/ END_SDK_$MAJOR_ABI_VERSION$NEWLINE" app/build.gradle
121sed -i '' "/$REPLACE_TEXT/$SED_APPEND_COMMAND\ \ \/\/ BEGIN_SDK_$MAJOR_ABI_VERSION$NEWLINE\ \ versionedApi 'host.exp:reactandroid-$ABI_VERSION:1.0.0'$NEWLINE\ \ \/\/ END_SDK_$MAJOR_ABI_VERSION$NEWLINE" expoview/build.gradle
122
123# Update Constants.java
124sed -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
125
126# Add new version
127
128# Update places that need to reference all versions of react native
129# THIS WILL PROBABLY BREAK OFTEN
130
131# Update classes in `versioned` flavor that implement DefaultHardwareBackBtnHandler or PermissionAwareActivity
132BACK_BUTTON_HANDLER_CLASS='com.facebook.react.modules.core.DefaultHardwareBackBtnHandler'
133PERMISSION_AWARE_ACTIVITY_CLASS='com.facebook.react.modules.core.PermissionAwareActivity'
134PERMISSION_LISTENER_CLASS='com.facebook.react.modules.core.PermissionListener'
135find expoview/src/versioned/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/"
136# Add implementation of PermissionAwareActivity.requestPermissions
137find expoview/src/versioned/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/"
138
139popd
140