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