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