我的React Native应用程序发布版本遇到了问题。调试版本工作正常,没有问题。当我使用gradlew assembleRelase
我得到了一个APK,并说它构建成功。然后我尝试使用ADB在我的设备上安装应用程序 - 没问题。但是在运行它时,应用程序会在崩溃之前暂时打开。
使用adb logcat
,我可以看到抛出以下两个错误:
1)获取Google注册ID时出错 - OneSignal / Google Play服务
05-09 13:07:33.387 24726 24778 E OneSignal: Error Getting Google Registration ID
05-09 13:07:33.387 24726 24778 E OneSignal: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/util/zzs;
05-09 13:07:33.387 24726 24778 E OneSignal: at com.google.android.gms.iid.zzaa.zzdr(Unknown Source)
05-09 13:07:33.387 24726 24778 E OneSignal: at com.google.android.gms.gcm.GoogleCloudMessaging.zza(Unknown Source)
05-09 13:07:33.387 24726 24778 E OneSignal: at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
05-09 13:07:33.387 24726 24778 E OneSignal: at com.onesignal.PushRegistratorGPS$2.run(PushRegistratorGPS.java:167)
05-09 13:07:33.387 24726 24778 E OneSignal: at java.lang.Thread.run(Thread.java:818)
05-09 13:07:33.387 24726 24778 E OneSignal: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.util.zzs" on path: DexPathList[[zip file "/data/app/com.***.app-1/base.apk"],nativeLibraryDirectories=[/data/app/com.***.app-1/lib/arm, /data/app/com.***.app-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
05-09 13:07:33.387 24726 24778 E OneSignal: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-09 13:07:33.387 24726 24778 E OneSignal: at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-09 13:07:33.387 24726 24778 E OneSignal: at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
05-09 13:07:33.387 24726 24778 E OneSignal: ... 5 more
05-09 13:07:33.387 24726 24778 E OneSignal: Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.common.util.zzs
05-09 13:07:33.387 24726 24778 E OneSignal: at java.lang.Class.classForName(Native Method)
05-09 13:07:33.387 24726 24778 E OneSignal: at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
05-09 13:07:33.387 24726 24778 E OneSignal: at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
05-09 13:07:33.387 24726 24778 E OneSignal: at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
05-09 13:07:33.387 24726 24778 E OneSignal: ... 6 more
05-09 13:07:33.387 24726 24778 E OneSignal: Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
对我而言,Google Play服务库似乎没有正确加载,但我已将它们添加到我的应用build.gradle
中,如下所示:
dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.facebook.react:react-native:+" // From node_modules
compile project(path: ':react-native-onesignal')
compile project(path: ':react-native-fbsdk')
compile project(path: ':react-native-config')
implementation (project(':react-native-maps')) {
//Exclude support-v4 module from react-native-maps plugin because a more recent version is pulled in by another dependency
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.google.android.gms', module: 'play-services-base'
exclude group: 'com.google.android.gms', module: 'play-services-maps'
}
compile (project(':react-native-geolocation-service')) {
exclude group: 'com.google.android.gms', module: 'play-services-location'
}
compile 'com.google.android.gms:play-services-location:+'
compile 'com.google.android.gms:play-services-base:+'
compile 'com.google.android.gms:play-services-maps:+'
}
2)模块AppRegistry不是注册的可调用模块
05-09 13:07:34.253 24726 24759 E AndroidRuntime: com.facebook.react.common.JavascriptException: Module AppRegistry is not a registered callable module (calling runApplication), stack:
05-09 13:07:34.253 24726 24759 E AndroidRuntime: value@23:3441
05-09 13:07:34.253 24726 24759 E AndroidRuntime: <unknown>@23:1067
05-09 13:07:34.253 24726 24759 E AndroidRuntime: value@23:3009
05-09 13:07:34.253 24726 24759 E AndroidRuntime: value@23:1039
05-09 13:07:34.253 24726 24759 E AndroidRuntime:
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at com.facebook.react.modules.core.ExceptionsManagerModule.showOrThrowError(ExceptionsManagerModule.java:54)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at com.facebook.react.modules.core.ExceptionsManagerModule.reportFatalException(ExceptionsManagerModule.java:38)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at android.os.Looper.loop(Looper.java:152)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192)
05-09 13:07:34.253 24726 24759 E AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
05-09 13:07:34.255 24726 24759 D AppTracker: App Event: crash
这里似乎无法找到已注册的组件,但是在我的index.js中我注册了这样的组件:
AppRegistry.registerComponent('SpotsApp', () => App);
其中App
是实际的应用程序组件。在我的build.gradle中,我指向此文件作为条目文件:
project.ext.react = [
entryFile: "index.js"
]
在MainApplication
内,我设置主模块名称如下:
@Override
protected String getJSMainModuleName() {
return "App";
}
对于第一个错误,在我看来,谷歌播放服务是正确导入并添加为依赖项。对于第二个错误,我相信我正确地注册了我的组件,但是我一直在使用发布版本来解决这些问题。任何帮助将不胜感激。
答案 0 :(得分:1)
Module AppRegistry is not a registered callable module (calling runApplication)
是由主JS模块名称配置错误引起的。
要修复,只需引用应用程序的入口点而不是应用程序组件本身。
例如
@Override
protected String getJSMainModuleName() {
return "index"; // Was: App
}
另外,请确保在MainActivity.java
方法getMainComponentName
中返回您在索引文件中注册的组件的名称。
答案 1 :(得分:0)
关于OneSignal / Google Play服务的NoClassDefFoundError的第一个错误:看起来好像是OneSignal
反应本机库与某些内容冲突(不太确定是什么)。经过一番挖掘后,我发现this comment (link to Github)隐藏在react-native-onesignal库中:
// Add the following to the top (Line 1) of your app/build.gradle if you run into any issues with duplicate classes.
// Such as the following error
// Error: more than one library with package name 'com.google.android.gms.license'
/*
plugins {
id 'com.onesignal.androidsdk.onesignal-gradle-plugin' version '0.8.1'
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
*/
所以我遵循了那些指示。我将这些行添加到应用程序的build.gradle的开头(所以在apply plugin: "com.android.application"
行之前):
plugins {
id 'com.onesignal.androidsdk.onesignal-gradle-plugin' version '0.8.1'
}
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
此解决方案修复了NoClassDefFoundError / DexMultiPath错误的问题。解决方案根本没有记录在OneSignal网站上,我不得不深入挖掘他们的build.gradle,这就是为什么我决定在这里发布它作为答案。
我还没有找到第二个错误的解决方案,这意味着我的应用程序在启动时仍会崩溃,但第一个错误已解决。当我找到第二个错误的解决方案时,我将编辑这个答案。