RuntimeException:ClassNotFoundException android.arch.lifecycle.ProcessLifecycleOwnerInitializer
RuntimeException:ClassNotFoundException android.arch.lifecycle.ProcessLifecycleOwnerInitializer
我只在 Android SDK < 5.0 上收到此错误。所以 4.0、4.2、4.3 等等。任何 运行 Android 5.0+ 都可以完美运行。有任何想法吗?启动时崩溃。
按照本指南进行设置 -> https://developer.android.com/topic/libraries/architecture/adding-components.html
App.java
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleListener(this));
registerActivityLifecycleCallbacks(this);
}
AppLifecycleListener.java
public class AppLifecycleListener implements LifecycleObserver {
private App app;
public AppLifecycleListener(App app)
{
this.app = app;
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onMoveToForeground() {
if (app.getCurrentActivity() instanceof BaseActivity)
{
BaseActivity baseActivity = (BaseActivity) app.getCurrentActivity();
baseActivity.runIsAPIVersionCheck();
baseActivity.fetchObjectsWithHUD(false);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onMoveToBackground() {}
}
Gradle
compileSdkVersion 26
dexOptions {
javaMaxHeapSize "4g"
}
defaultConfig {
applicationId "app"
minSdkVersion 16
targetSdkVersion 26
versionCode 71
versionName "4.9.9"
multiDexEnabled true
resConfigs "en"
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath false
}
}
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'android.arch.lifecycle:extensions:1.1.0'
compile 'android.arch.lifecycle:compiler:1.1.0'
02-28 20:54:03.151 2558-2558/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to get provider android.arch.lifecycle.ProcessLifecycleOwnerInitializer: java.lang.ClassNotFoundException: android.arch.lifecycle.ProcessLifecycleOwnerInitializer
at android.app.ActivityThread.installProvider(ActivityThread.java:4563)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4190)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4132)
at android.app.ActivityThread.access00(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: android.arch.lifecycle.ProcessLifecycleOwnerInitializer
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.app.ActivityThread.installProvider(ActivityThread.java:4548)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4190)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4132)
at android.app.ActivityThread.access00(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这最终成为一个 multdex 问题。我按照此处的文档 -> https://developer.android.com/studio/build/multidex.html#mdex-gradle 进行操作,现在效果很好!
Gradle:
implementation 'com.android.support:multidex:1.0.3'
App.java:
public class App extends Application implements Application.ActivityLifecycleCallbacks {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
将以下内容添加到您的 ProGuard 文件中。自 Android Architecture Components 1.0.0 (source) 以来,这应该不是必需的,但显然它仍然是。
-keep class * implements android.arch.lifecycle.GeneratedAdapter {<init>(...);}
我所要做的就是将以下内容添加到我的 proguard-rules.pro
文件中:
-keep class android.arch.lifecycle.** {*;}
在我的例子中,android 架构生命周期文件以某种方式被 Android 捆绑在 Pie(Android9) 上。所以我所做的是在 proguard-rules.pro
中添加相同的 keep 语句
对于 AndroidX :
-keep class androidx.lifecycle.** {*;}
支持:
-keep class android.arch.lifecycle.** {*;}
我只在 Android SDK < 5.0 上收到此错误。所以 4.0、4.2、4.3 等等。任何 运行 Android 5.0+ 都可以完美运行。有任何想法吗?启动时崩溃。
按照本指南进行设置 -> https://developer.android.com/topic/libraries/architecture/adding-components.html
App.java
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifecycleListener(this));
registerActivityLifecycleCallbacks(this);
}
AppLifecycleListener.java
public class AppLifecycleListener implements LifecycleObserver {
private App app;
public AppLifecycleListener(App app)
{
this.app = app;
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onMoveToForeground() {
if (app.getCurrentActivity() instanceof BaseActivity)
{
BaseActivity baseActivity = (BaseActivity) app.getCurrentActivity();
baseActivity.runIsAPIVersionCheck();
baseActivity.fetchObjectsWithHUD(false);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onMoveToBackground() {}
}
Gradle
compileSdkVersion 26
dexOptions {
javaMaxHeapSize "4g"
}
defaultConfig {
applicationId "app"
minSdkVersion 16
targetSdkVersion 26
versionCode 71
versionName "4.9.9"
multiDexEnabled true
resConfigs "en"
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath false
}
}
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'android.arch.lifecycle:extensions:1.1.0'
compile 'android.arch.lifecycle:compiler:1.1.0'
02-28 20:54:03.151 2558-2558/? E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to get provider android.arch.lifecycle.ProcessLifecycleOwnerInitializer: java.lang.ClassNotFoundException: android.arch.lifecycle.ProcessLifecycleOwnerInitializer at android.app.ActivityThread.installProvider(ActivityThread.java:4563) at android.app.ActivityThread.installContentProviders(ActivityThread.java:4190) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4132) at android.app.ActivityThread.access00(ActivityThread.java:130) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassNotFoundException: android.arch.lifecycle.ProcessLifecycleOwnerInitializer at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) at java.lang.ClassLoader.loadClass(ClassLoader.java:501) at java.lang.ClassLoader.loadClass(ClassLoader.java:461) at android.app.ActivityThread.installProvider(ActivityThread.java:4548) at android.app.ActivityThread.installContentProviders(ActivityThread.java:4190) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4132) at android.app.ActivityThread.access00(ActivityThread.java:130) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method)
这最终成为一个 multdex 问题。我按照此处的文档 -> https://developer.android.com/studio/build/multidex.html#mdex-gradle 进行操作,现在效果很好!
Gradle:
implementation 'com.android.support:multidex:1.0.3'
App.java:
public class App extends Application implements Application.ActivityLifecycleCallbacks {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
将以下内容添加到您的 ProGuard 文件中。自 Android Architecture Components 1.0.0 (source) 以来,这应该不是必需的,但显然它仍然是。
-keep class * implements android.arch.lifecycle.GeneratedAdapter {<init>(...);}
我所要做的就是将以下内容添加到我的 proguard-rules.pro
文件中:
-keep class android.arch.lifecycle.** {*;}
在我的例子中,android 架构生命周期文件以某种方式被 Android 捆绑在 Pie(Android9) 上。所以我所做的是在 proguard-rules.pro
对于 AndroidX :
-keep class androidx.lifecycle.** {*;}
支持:
-keep class android.arch.lifecycle.** {*;}