Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native compiled apk wont run on device

Walkthrought with react-native tutorial (react-native 0.30): I created app which couldn't be installed on android device here are steps:

  1. react-native init demo
  2. cd android
  3. gradlew assembleRelease
  4. Copying unsigned apk to mobile device ( htc m8 mini 2 )

and got result : Application demo was not installed

Based on this similiar question apk didn't run on mobile android react native: Unsigned apk won't install on mobile.

But why I am able to run unsiged application made by Ionic 2?

Whats the problem?

like image 701
Evgheny Kalkutin Avatar asked Jul 27 '16 13:07

Evgheny Kalkutin


Video Answer


1 Answers

In Ionic 2 you run a debug apk, its not possible to run an unsigned apk.

What you can do in react is sign your apk and run.

The first setup can be a little complex, but after you complete the setup is very easy to generate a signed apk when you need it.

You can follow these instructions to sign your apk:

Generating a signing key

You can generate a private signing key using keytool.

$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

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

The keystore contains a single key, valid for 10000 days. The alias is a name that you will use later when signing your app, so remember to take note of the alias.

Note: Remember to keep your keystore file private and never commit it to version control.

Setting up gradle variables

  1. Place the my-release-key.keystore file under the android/app directory in your project folder.
  2. Edit the file ~/.gradle/gradle.properties and add the following (replace ***** with the correct keystore password, alias and key password),

MYAPP_RELEASE_STORE_FILE=my-release-key.keystore

MYAPP_RELEASE_KEY_ALIAS=my-key-alias

MYAPP_RELEASE_STORE_PASSWORD=*****

MYAPP_RELEASE_KEY_PASSWORD=*****

These are going to be global gradle variables, which we can later use in our gradle config to sign our app.

Note about saving the keystore: Once you publish the app on the Play Store, you will need to republish your app under a different package name (losing all downloads and ratings) if you want to change the signing key at any point. So backup your keystore and don't forget the passwords. Note about security: If you are not keen on storing your passwords in plaintext and you are running OSX, you can also store your credentials in the Keychain Access app. Then you can skip the two last rows in ~/.gradle/gradle.properties.

Adding signing config to your app's gradle config Edit the file android/app/build.gradle in your project folder and add the signing config,

...
android {
    ...
    defaultConfig { ... }
    signingConfigs {
        release {
            storeFile file(MYAPP_RELEASE_STORE_FILE)
            storePassword MYAPP_RELEASE_STORE_PASSWORD
            keyAlias MYAPP_RELEASE_KEY_ALIAS
            keyPassword MYAPP_RELEASE_KEY_PASSWORD
        }
    }
    buildTypes {
        release {
            ...
            signingConfig signingConfigs.release
        }
    }
}
...

Generating the release APK

Simply run the following in a terminal:

$ cd android && ./gradlew assembleRelease

Gradle's assembleRelease will bundle all the JavaScript needed to run your app into the APK. If you need to change the way the JavaScript bundle and/or drawable resources are bundled (e.g. if you changed the default file/folder names or the general structure of the project), have a look at android/app/build.gradle to see how you can update it to reflect these changes.

The generated APK can be found under android/app/build/outputs/apk/app-release.apk, and is ready to be distributed.

Testing the release build of your app

Before uploading the release build to the Play Store, make sure you test it thoroughly. Install it on the device using:

$ cd android && ./gradlew installRelease

Note that installRelease is only available if you've set up signing as described above.

You can kill any running packager instances, all your and framework JavaScript code is bundled in the APK's assets.

like image 131
Kaleb Portilho Avatar answered Oct 30 '22 07:10

Kaleb Portilho