Skip to content

Instantly share code, notes, and snippets.

@milkersarac
Last active June 24, 2020 19:14
Show Gist options
  • Save milkersarac/7ca6c30464c5555a92a8d39ef85f2552 to your computer and use it in GitHub Desktop.
Save milkersarac/7ca6c30464c5555a92a8d39ef85f2552 to your computer and use it in GitHub Desktop.
This script can be used for building multiple react-native apps from a single git repo.
#!/bin/bash
# MUSTAFA ILKER SARAC
# This script can be used for building multiple react-native apps from a single git repo.
# It needs master files for the default configurations.
# master files are generally the copy of the current config files of that git repo.
# If current state of the app changes i.e. adding new 3rd party components so package.json changes;
# then one should be responsible to reflect these changes to the master files to be synced with the current state of the repo.
# This script can be used to alter the current state, so that new apps could be generated with new names etc.
# scriptAndroid.sh
# "chmod +x ./scriptAndroid" to make script executable.
# ls -al
# -rwxr-xr-x 1 milkers staff 6569 Jan 11 14:35 scriptAndroid.
if [ $# != 2 ]
then
echo "**************************************"
echo "This script works with 2 parameters."
echo "./scriptAndroid environment app_name"
echo "environment params: dev, test, prod."
echo "app_name params : APP1, APP2, APP3, APP4."
echo "**************************************"
echo "Ex-1:"
echo "./scriptAndroid dev APP1"
echo "./scriptAndroid dev APP2"
echo "./scriptAndroid dev APP3"
echo "./scriptAndroid dev APP4"
echo "**************************************"
echo "Ex-2:"
echo "./scriptAndroid test APP1"
echo "./scriptAndroid test APP2"
echo "./scriptAndroid test APP3"
echo "./scriptAndroid test APP4"
echo "**************************************"
echo "Ex-3:"
echo "./scriptAndroid prod APP2"
exit -1
elif [ $1 != "dev" ] && [ $1 != "test" ] && [ $1 != "prod" ]
then
echo "First param (environment) could be either \"dev, test, prod\" "
exit -1
elif [ $2 != "APP1" ] && [ $2 != "APP2" ] && [ $2 != "APP3" ] && [ $2 != "APP4" ]
then
echo "Second param (app_name) could be either \"APP1, APP2, APP3, APP4\" "
exit -1
fi
lcAppName="" # lower case app name
if [ $2 = "APP1" ]
then
lcAppName="app1"
elif [ $2 = "APP2" ]
then
lcAppName="app2"
elif [ $2 = "APP3" ]
then
lcAppName="app3"
elif [ $2 = "APP4" ]
then
lcAppName="app4"
fi
echo "1) Copy configuration files."
# copy gradle-wrapper.properties
rm -rf ./android/gradle/wrapper/gradle-wrapper.properties
cp -r ./_configFiles/androidMasterFiles/gradle-wrapper.properties ./android/gradle/wrapper/gradle-wrapper.properties
# copy android_build.gradle over android/build.gradle
rm -rf ./android/build.gradle
cp -r ./_configFiles/androidMasterFiles/android_build.gradle ./android/build.gradle
# copy app_build.gradle over android/app/build.gradle and update applicationId
rm -rf ./android/app/build.gradle
cp -r ./_configFiles/androidMasterFiles/app_build.gradle ./android/app/build.gradle
sed -i bckp -e "s/com.bbn.bbn_app1dev/com.bbn.$lcAppName$1/g" ./android/app/build.gradle
rm -rf ./android/app/build.gradlebckp
# create a correct path for MainApplication.java and MainActivity.java.
rm -rf ./android/app/src/main/java/com/*
mkdir -p ./android/app/src/main/java/com/bbn/$lcAppName$1/
cp -r ./_configFiles/androidMasterFiles/MainActivity.java ./android/app/src/main/java/com/bbn/$lcAppName$1/MainActivity.java
cp -r ./_configFiles/androidMasterFiles/MainApplication.java ./android/app/src/main/java/com/bbn/$lcAppName$1/MainApplication.java
sed -i bckp -e "s/com.bbn.bbn_app1dev/com.bbn.$lcAppName$1/g" ./android/app/src/main/java/com/bbn/$lcAppName$1/MainActivity.java
sed -i bckp -e "s/com.bbn.bbn_app1dev/com.bbn.$lcAppName$1/g" ./android/app/src/main/java/com/bbn/$lcAppName$1/MainApplication.java
rm -rf ./android/app/src/main/java/com/bbn/$lcAppName$1/MainActivity.javabckp
rm -rf ./android/app/src/main/java/com/bbn/$lcAppName$1/MainApplication.javabckp
# copy AndroidManifest.xml file and update its content.
rm -rf ./android/app/src/main/AndroidManifest.xml
cp -r ./_configFiles/androidMasterFiles/AndroidManifest.xml ./android/app/src/main/AndroidManifest.xml
sed -i bckp -e "s/com.bbn.bbn_app1dev/com.bbn.$lcAppName$1/g" ./android/app/src/main/AndroidManifest.xml
if [ $1 = "prod" ]
then
sed -i bckp -e "s/bbn-APP1-dev/bbn-$2/g" ./android/app/src/main/AndroidManifest.xml
else
sed -i bckp -e "s/bbn-APP1-dev/bbn-$2-$1/g" ./android/app/src/main/AndroidManifest.xml
fi
rm -rf ./android/app/src/main/AndroidManifest.xmlbckp
# update app name on strings.xml
rm -rf ./android/app/src/main/res/values/strings.xml
cp -r ./_configFiles/androidMasterFiles/strings.xml ./android/app/src/main/res/values/strings.xml
if [ $1 = "prod" ]
then
sed -i bckp -e "s/APP1dev/$2/g" ./android/app/src/main/res/values/strings.xml
else
sed -i bckp -e "s/APP1dev/$2$1/g" ./android/app/src/main/res/values/strings.xml
fi
rm -rf ./android/app/src/main/res/values/strings.xmlbckp
# copy proguard file in order to obfuscate on release mode.
rm -rf ./android/app/proguard-rules.pro
cp -r ./_configFiles/androidMasterFiles/proguard-rules.pro ./android/app/proguard-rules.pro
# copy Avenir font to assets.
cp -r ./images/fonts/Avenir.ttf ./android/app/src/main/assets/fonts/Avenir.ttf
# copy .cer file to assets for pinch module.
cp -r ./_configFiles/gd-class2-root.cer ./android/app/src/main/assets/gd-class2-root.cer
# copy launcher icons.
cp -r ./_configFiles/androidMasterFiles/androidLauncherIcon/$lcAppName-mdpi/ic_launcher_app.png ./android/app/src/main/res/mipmap-mdpi/ic_launcher_app.png
cp -r ./_configFiles/androidMasterFiles/androidLauncherIcon/$lcAppName-hdpi/ic_launcher_app.png ./android/app/src/main/res/mipmap-hdpi/ic_launcher_app.png
cp -r ./_configFiles/androidMasterFiles/androidLauncherIcon/$lcAppName-xhdpi/ic_launcher_app.png ./android/app/src/main/res/mipmap-xhdpi/ic_launcher_app.png
cp -r ./_configFiles/androidMasterFiles/androidLauncherIcon/$lcAppName-xxhdpi/ic_launcher_app.png ./android/app/src/main/res/mipmap-xxhdpi/ic_launcher_app.png
# Styles are arranged in a similar way but excluded because of not being generic enough,
# to solve problems of the most.
echo "2) Create Debug and Release .apk files."
# If you plan to release, delete the 'drawable' folders, they create problem while producing .apk for release.
rm -rf ./android/app/src/main/res/drawable-*
(cd android/ && ./gradlew assembleRelease) # be sure to put .keystore file under /app folder.
#(cd android/ && ./gradlew assembleDebug)
# copy images to the assests folder for 'debug' case => (cd android/ && ./gradlew assembleDebug)
# Use this if you want to call 'react-native run-android' instead of './gradlew installDebug'.
# Problems might occure. https://github.com/facebook/react-native/issues/5787
# This is important to save image files to assests during debug release.
react-native bundle --platform android --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --dev false --reset-cache --assets-dest android/app/src/main/res/
# If you have a device connected, you can install the release/debug version of the app using;
# (cd android/ && ./gradlew installRelease)
# (cd android/ && ./gradlew installDebug)
echo "3) Please check these configurated files;"
echo "AndroidManifest.xml"
echo "app/build.gradle"
echo "MainActivity.java"
echo "MainApplication.java"
echo "proguard-rules.pro"
echo "strings.xml"
echo ""
echo ".apk files are at android/app/build/outputs."
echo "BYE"
@Bernardomrf
Copy link

Thanks for sharing this!
Do you have one for iOS too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment