Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate release apk in react native

Tags:

react-native

I create an simple application by react native. After finishing deploying now i want to make release apk so in order to according it's official site i have created key:

"C:\Program Files\Java\jdk1.8.0_162\bin\keytool.exe" -genkeypair -v -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

And i added :

MYAPP_UPLOAD_STORE_FILE=my-upload-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=my-key-alias
MYAPP_UPLOAD_STORE_PASSWORD=mypass
MYAPP_UPLOAD_KEY_PASSWORD=mypass

into the android/gradle.properties file and finally i added release part into signingConfigs section in android/app/build.gradle file:

signingConfigs {
    debug {
        storeFile file('debug.keystore')
        storePassword 'android'
        keyAlias 'androiddebugkey'
        keyPassword 'android'
    }
     release {
        if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
            storeFile file(MYAPP_UPLOAD_STORE_FILE)
            storePassword MYAPP_UPLOAD_STORE_PASSWORD
            keyAlias MYAPP_UPLOAD_KEY_ALIAS
            keyPassword MYAPP_UPLOAD_KEY_PASSWORD
        }
    }
}

and i added this sign config to buildTypes part:

buildTypes {
    debug {
        signingConfig signingConfigs.debug
    }
    release {
        minifyEnabled enableProguardInReleaseBuilds
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        signingConfig signingConfigs.release
    }
}

but after running \android> ./gradlew bundleRelease :

PS F:\SafaProject\ReactNative\RNAuditMngm\android> ./gradlew bundleRelease

> Task :app:bundleReleaseJsAndAssets
warning: the transform cache was reset.
Loading dependency graph, done.
info Writing bundle output to:, F:\SafaProject\ReactNative\RNAuditMngm\android\app\build\generated\assets\react\release\index.android.bundle
info Writing sourcemap output to:, F:\SafaProject\ReactNative\RNAuditMngm\android\app\build\generated\sourcemaps\react\release\index.android.bundle.map
info Done writing bundle output   
info Done writing sourcemap output
info Copying 13 asset files
info Done copying assets

> Task :react-native-gesture-handler:compileReleaseJavaWithJavac
Note: F:\SafaProject\ReactNative\RNAuditMngm\node_modules\react-native-gesture-handler\android\src\main\java\com\swmansion\gesturehandler\react\RNGestureHandlerButtonViewManager.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

> Task :react-native-reanimated:compileReleaseJavaWithJavac
Note: F:\SafaProject\ReactNative\RNAuditMngm\node_modules\react-native-reanimated\android\src\main\java\com\swmansion\reanimated\NodesManager.java uses or overrides a deprecated 
API.
Note: Recompile with -Xlint:deprecation for details.
Note: F:\SafaProject\ReactNative\RNAuditMngm\node_modules\react-native-reanimated\android\src\main\java\com\swmansion\reanimated\NodesManager.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1m 54s
88 actionable tasks: 84 executed, 4 up-to-date
PS F:\SafaProject\ReactNative\RNAuditMngm\android> 

It just create app.aab file in \app\build\outputs\bundle\release folder?

How could i create apk file?

This is complete app build.gradle

like image 398
Cyrus the Great Avatar asked Dec 18 '22 14:12

Cyrus the Great


2 Answers

If you want to create .apk then run command :

cd android

./gradlew assembleRelease

It will generate release apk here :

android/app/build/output/apk/release/app-release.apk

If you want to generate a buldle (.aab) to upload to play store:

cd android

./gradlew bundleRelease

like image 189
Kishan Bharda Avatar answered Feb 01 '23 22:02

Kishan Bharda


This is the way I generate a signed APK.

I'll also show you how to securely load in your gradle variables so that the project can be safely pushed to Git without exposing your passwords (something that is annoyingly not covered in tutorials).

1. Generate an upload key.

You can generate a private signing key using keytool. On Windows keytool must be run from C:\Program Files\Java\jdkx.x.x_x\bin.

keytool -genkeypair -v -storetype PKCS12 -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

This command prompts you for passwords for the keystore and key and for the Distinguished Name fields for your key. It then generates the keystore as a file called my-upload-key.keystore.

The keystore contains a single key, valid for 10000 days.

2. Setting up Gradle variables and safely loading them in

Place the my-upload-key.keystore file under the android/app directory in your project folder.

Create a new file in the android folder: android/keystore.properties and add the following (but obviously with your own passwords).

MYAPP_UPLOAD_STORE_FILE=my-upload-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=my-key-alias
MYAPP_UPLOAD_STORE_PASSWORD=YOUR_PASSWORD_HERE
MYAPP_UPLOAD_KEY_PASSWORD=YOUR_PASSWORD_HERE

Then add this new file to .gitignore so you can safely push to github without exposing your variables:

keystore.properties

Next, modify your android/app/build.gradle code to load in your keystore properties:

// Load keystore
def keystorePropertiesFile = rootProject.file("keystore.properties");
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

// ...

android{

    // ...

 signingConfigs {
        release {
            if ( keystorePropertiesFile.exists() ) {
                storeFile file(keystoreProperties['MYAPP_UPLOAD_STORE_FILE'])
                storePassword keystoreProperties['MYAPP_UPLOAD_STORE_PASSWORD']
                keyAlias keystoreProperties['MYAPP_UPLOAD_KEY_ALIAS']
                keyPassword keystoreProperties['MYAPP_UPLOAD_KEY_PASSWORD']
            }

        }
  }
    buildTypes {
        release {
            // ...
            signingConfig signingConfigs.release
        }
    }

    // ...
}

Doing it this way ensures that the project will still work if someone else clones it from Git without the keystore.properties file - perfect.

3. Generate the Android release bundle file manually

Remove current index.android.bundle file:

rm android/app/src/main/assets/index.android.bundle

If no such file exists then you will get ‘No such file or directory’ message.

Next, generate the android bundle, note the below is one command:

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

You will likely get a “duplicate resources” error (at least I always do). Go to /android/app/src/main/res folder and delete any files that begin with “drawable”.

4. Generate the Android signed release build APK

Navigate to android folder:

cd android

And run the following command to generate your release APK:

./gradlew clean && ./gradlew assembleRelease

If any errors, delete android/.gradle and android/app/build/ and retry.

Your signed APK should now be located at android/app/build/outputs/apk/app-release.apk.

Once your build is successful then from your project’s root folder run below command to test your app in your device:

npx react-native run-android --variant=release

If you have any issues, try the following before re-doing the steps above:

  • Make sure to uninstall the old app on emulator/phone.
  • Delete old android/.gradle and android/app/build folder.
  • Npm cache clean –force.
  • Delete and reinstall node_modules.

I wrote this answer as an article for my own future reference but feel free to bookmark it :)

like image 35
Danny Adams Avatar answered Feb 02 '23 00:02

Danny Adams