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