Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeException:ClassNotFoundException android.arch.lifecycle.ProcessLifecycleOwnerInitializer

I am getting this error only on Android SDK < 5.0. So 4.0, 4.2, 4.3 ect. Anything running Android 5.0+ works flawlessly. Any ideas? Crashes on launch.

Following this guide for setup -> 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.access$1300(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.access$1300(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) 

like image 543
mikemike396 Avatar asked Feb 28 '18 21:02

mikemike396


2 Answers

This ended up being a multdex issue. I followed the docs here -> https://developer.android.com/studio/build/multidex.html#mdex-gradle and it works great now!

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);
}
like image 151
mikemike396 Avatar answered Sep 28 '22 23:09

mikemike396


All I had to do was add the following to my proguard-rules.pro file:

-keep class android.arch.lifecycle.** {*;}
like image 42
Paul LeBeau Avatar answered Sep 28 '22 21:09

Paul LeBeau