14

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) 

mikemike396
  • 2,435
  • 1
  • 26
  • 41
  • did you tried `multiDexEnabled false` ? – Thomas Mary Feb 28 '18 at 21:18
  • @ThomasMary Changing to false produces this error -> Error:Execution failed for task ':App:transformDexArchiveWithExternalLibsDexMergerForDebug'. > java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex – mikemike396 Feb 28 '18 at 21:25
  • `implementation "android.arch.lifecycle:extensions:1.1.0" // alternatively, just ViewModel implementation "android.arch.lifecycle:viewmodel:1.1.0" // alternatively, just LiveData implementation "android.arch.lifecycle:livedata:1.1.0" annotationProcessor "android.arch.lifecycle:compiler:1.1.0"` make sure you added all of them – Santanu Sur Feb 28 '18 at 21:29
  • Just added them all and same error after syncing gradle. (https://developer.android.com/topic/libraries/architecture/adding-components.html) // ViewModel and LiveData implementation "android.arch.lifecycle:extensions:1.1.0" // alternatively, just ViewModel implementation "android.arch.lifecycle:viewmodel:1.1.0" // alternatively, just LiveData implementation "android.arch.lifecycle:livedata:1.1.0" annotationProcessor "android.arch.lifecycle:compiler:1.1.0" – mikemike396 Feb 28 '18 at 21:41

4 Answers4

25

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);
}
mikemike396
  • 2,435
  • 1
  • 26
  • 41
7

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

-keep class android.arch.lifecycle.** {*;}
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • that helped! any idea why? I have `minifyEnabled = false` everywhere and I was runnig debug mode. I migrated my App to use `androidx` and had this issue afterwards. – Informatic0re Feb 27 '19 at 14:04
3

In my case, Somehow android architecture lifecycle files getting omitted with Android bundle on Pie(Android9). So what I did was added keep statement for the same in proguard-rules.pro

For AndroidX :

-keep class androidx.lifecycle.** {*;}

For Support :

-keep class android.arch.lifecycle.** {*;}
Dino
  • 7,779
  • 12
  • 46
  • 85
VishnuPrajapati
  • 119
  • 1
  • 6
1

Add the following to your your ProGuard file. This shouldn't be necessary since Android Architecture Components 1.0.0 (source), but apparently it still is.

-keep class * implements android.arch.lifecycle.GeneratedAdapter {<init>(...);}
Cristan
  • 12,083
  • 7
  • 65
  • 69