ReactNative-如何在AndroidStudio上打Release包

如果你也是在现有原生Android应用上集成的ReactNative,碰到了打release包的问题,那么下面的内容可能会对你有帮助。如果不是,请跳过。

下面介绍一下如何在Android Studio上打集成了React Native的release包。

项目结构

我的项目结构如下图。所有android代码均写在android这个文件夹下面
这里写图片描述

步骤

  • cd到项目最外层目录,然后执行下面命令。这个命令是创建一个React Native的bundle集成到我们原生项目中
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/

注意:如果报 react-native command not found这个错误,你可能需要:修改react-native的环境变量。我的是在home/.bashrc文件里增加了export PATH=/home/huanglin/.npm-global/bin/:$PATH

  • 编辑app/proguard-rules.pro这个混淆文件,在文件最后添加以下内容
#recat-native
-keep public class com.facebook.** {
  *;
}

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Disabling obfuscation is useful if you collect stack traces from production crashes
# (unless you are using a system that supports de-obfuscate the stack traces).
-dontobfuscate

# Keep our interfaces so they can be used by other ProGuard rules.
# See http://sourceforge.net/p/proguard/bugs/466/
-keep,allowobfuscation @interface com.facebook.proguard.annotations.DoNotStrip
-keep,allowobfuscation @interface com.facebook.proguard.annotations.KeepGettersAndSetters

# Do not strip any method/class that is annotated with @DoNotStrip
-keep @com.facebook.proguard.annotations.DoNotStrip class *
-keepclassmembers class * {
    @com.facebook.proguard.annotations.DoNotStrip *;
}

-keepclassmembers @com.facebook.proguard.annotations.KeepGettersAndSetters class * {
  void set*(***);
  *** get*();
}

-keep class * extends com.facebook.react.bridge.JavaScriptModule { *; }
-keep class * extends com.facebook.react.bridge.NativeModule { *; }
-keepclassmembers,includedescriptorclasses class * { native <methods>; }
-keepclassmembers class *  { @com.facebook.react.uimanager.UIProp <fields>; }
-keepclassmembers class *  { @com.facebook.react.uimanager.annotations.ReactProp <methods>; }
-keepclassmembers class *  { @com.facebook.react.uimanager.annotations.ReactPropGroup <methods>; }

-dontwarn com.facebook.react.**

注意:混淆文件如果不配置,会报各种方法找不到的错,请务必配置好

  • cd到android目录下,执行打包命令。打包成功之后,就可以在/android/app/build/outputs/apk/release/下找到打包好的apk。
./gradlew assembleRelease

猜你喜欢

转载自blog.csdn.net/huanglin_developer/article/details/80681634
今日推荐