react native 打包

官方文档

生成签名文件

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](http://facebook.github.io/react-native/releases/0.36/docs/signed-apk-android.html#setting-up-gradle-variables)
Place the my-release-key.keystore
 file under the android/app
 directory in your project folder.
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.

### [Adding signing config to your app's gradle config](http://facebook.github.io/react-native/releases/0.36/docs/signed-apk-android.html#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](http://facebook.github.io/react-native/releases/0.36/docs/signed-apk-android.html#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](http://facebook.github.io/react-native/releases/0.36/docs/signed-apk-android.html#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:
```$ react-native run-android --variant=release```
Note that --variant=release
 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.
### [代码混淆Enabling Proguard to reduce the size of the APK (optional)](http://facebook.github.io/react-native/releases/0.36/docs/signed-apk-android.html#enabling-proguard-to-reduce-the-size-of-the-apk-optional)
Proguard is a tool that can slightly reduce the size of the APK. It does this by stripping parts of the React Native Java bytecode (and its dependencies) that your app is not using.
***IMPORTANT**: Make sure to thoroughly test your app if you've enabled Proguard. Proguard often requires configuration specific to each native library you're using. See `app/proguard-rules.pro`
`.*
To enable Proguard, edit android/app/build.gradle
:`
/** * Run Proguard to shrink the Java bytecode in release builds. */def `enableProguardInReleaseBuilds = true`

###注:windows android上发现的问题
打包后生成的release.apk 安装在手机上

com.facebook.react.devsupport.JSException: Could not get BatchedBridge, make sure your bundle is packaged correctly

发现包的大小和debug的大小差不多,应该有很多js文件,打包后应该包含的结果没,
http://reactnative.cn/docs/0.20/signed-apk-android.html打到个办法,
原来是没将js打包到assets夹下
想可能是windows上的问题
生成release所需全部脚本目前版本(0.36):

rem begin

rem 创建assets文件夹
mkdir -p android\app\src\main\assets

rem 清理工作空间
android\gradlew clean

rem js打包
react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

先在src/main在手动创建assest文件夹

rem 生成release包
cd android
gradlew assembleRelease
rem gradlew installRelease

rem 安装包
adb install app\build\outputs\apk\app-release.apk

rem end


react-native的命令集合

![Paste_Image.png](http://upload-images.jianshu.io/upload_images/1351616-9629f2e15594ea20.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

你可能感兴趣的:(react native 打包)