Last active
June 18, 2023 17:52
-
-
Save shlomoweb1/af8b47f659cac8666444c4a848686501 to your computer and use it in GitHub Desktop.
React Native compile Android to APK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Get the script's directory (OS-safe) | |
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | |
| # Change directory to the script's directory | |
| cd "$script_dir" | |
| # Run 'bumpVersion.js' with node (OS-safe) | |
| if [ "$(uname -s)" = "Linux" ] || [ "$(uname -s)" = "Darwin" ]; then | |
| $(node bumpVersion.js) 2>/dev/null | |
| else | |
| winpty node bumpVersion.js 2>/dev/null | |
| fi | |
| # Get version and app name from 'package.json' | |
| VERSION_RN=$(node -p "require('./package.json').version") | |
| APP_NAME=$(node -p "require('./package.json').name") | |
| # Define the APK filename | |
| FILENAME="$APP_NAME-$VERSION_RN.apk" | |
| echo "Going to build version $VERSION_RN" | |
| # Remove existing 'index.android.bundle' (redirect errors to /dev/null) | |
| rm android/app/src/main/assets/index.android.bundle 2>/dev/null | |
| # Bundle React Native app | |
| cd "$script_dir" | |
| npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res | |
| # Remove existing 'drawable-*' directories (redirect errors to /dev/null) | |
| find "$script_dir/android/app/src/main/res/" -type d -name "drawable-*" -prune -exec rm -rf {} + 2>/dev/null | |
| # Change directory to 'android' | |
| cd "$script_dir/android/" | |
| # Clean and assemble release | |
| ./gradlew clean | |
| ./gradlew assembleRelease | |
| # Move the generated APK to the desired filename | |
| mv "$script_dir/android/app/build/outputs/apk/release/app-release.apk" "$script_dir/android/app/build/outputs/apk/release/$FILENAME" | |
| echo "$script_dir/android/app/build/outputs/apk/release" | |
| echo "The APK is located in a directory named $FILENAME" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const pkg = require('./package.json'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| // Function to increment the version string | |
| function incrementVersion(versionString) { | |
| // Parse the version components | |
| const version = versionString.split('.').map(Number); | |
| // Increment the last component by 1 and handle overflow | |
| version[2] += 1; | |
| if (version[2] >= 10) { | |
| version[1] += 1; | |
| version[2] = 0; | |
| if (version[1] >= 10) { | |
| version[0] += 1; | |
| version[1] = 0; | |
| } | |
| } | |
| // Join the version components back into a string | |
| const incrementedVersion = version.join('.'); | |
| return incrementedVersion; | |
| } | |
| // Increment the version in package.json | |
| pkg.version = incrementVersion(pkg.version); | |
| // Write the updated package.json file | |
| const pkgFile = path.join(__dirname, 'package.json'); | |
| fs.writeFileSync(pkgFile, JSON.stringify(pkg, null, 2)); | |
| // Read the build.gradle file | |
| const gradleFile = path.join(__dirname, 'android', 'app', 'build.gradle'); | |
| fs.readFile(gradleFile, 'utf8', (err, data) => { | |
| if (err) { | |
| console.error(`Error reading file: ${gradleFile}`); | |
| console.error(err); | |
| return; | |
| } | |
| // Regular expressions to locate versionCode and versionName values | |
| const versionCodeRegex = /versionCode\s+(\d+)/; | |
| const versionNameRegex = /versionName\s+["']([^"']+)["']/; | |
| const match = data.match(versionCodeRegex); | |
| if (match && match[1]) { | |
| const currentVersionCode = parseInt(match[1]); | |
| const newVersionCode = currentVersionCode + 1; | |
| // Update the file contents with new versionCode and versionName | |
| const updatedFileContents = data | |
| .replace(versionCodeRegex, `versionCode ${newVersionCode}`) | |
| .replace(versionNameRegex, `versionName "${pkg.version}"`); | |
| // Write the modified file contents back to build.gradle | |
| fs.writeFile(gradleFile, updatedFileContents, 'utf8', (err) => { | |
| if (err) { | |
| console.error(`Error writing to file: ${gradleFile}`); | |
| console.error(err); | |
| return; | |
| } | |
| console.log(`Successfully updated ${gradleFile} with new versionCode: ${newVersionCode}`); | |
| }); | |
| } else { | |
| console.error(`Unable to find versionCode in ${gradleFile}`); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment