Failure on building debug apk. Unable to get provider leakCanary when it tries to install AppWatcher
sample project that reproduces issue: https://drive.google.com/file/d/1DSTrQOqsiYNZ71zJUK_DjbihFCxNSR6Z/view?usp=sharing
Logcat:
12-06 15:42:23.987 15540-15540/? I/art: Late-enabling -Xcheck:jni
Late-enabling JIT
12-06 15:42:24.009 15540-15540/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
12-06 15:42:24.070 15540-15540/com.app.tablesign W/ActivityThread: Application com.app.tablesign is waiting for the debugger on port 8100...
12-06 15:42:24.074 15540-15540/com.app.tablesign I/System.out: Sending WAIT chunk
12-06 15:42:24.245 15540-15550/com.app.tablesign I/art: Debugger is active
12-06 15:42:24.275 15540-15540/com.app.tablesign I/System.out: Debugger has connected
waiting for debugger to settle...
12-06 15:42:24.475 15540-15540/com.app.tablesign I/System.out: waiting for debugger to settle...
12-06 15:42:24.675 15540-15540/com.app.tablesign I/System.out: waiting for debugger to settle...
12-06 15:42:24.876 15540-15540/com.app.tablesign I/System.out: waiting for debugger to settle...
12-06 15:42:25.076 15540-15540/com.app.tablesign I/System.out: waiting for debugger to settle...
12-06 15:42:25.277 15540-15540/com.app.tablesign I/System.out: waiting for debugger to settle...
12-06 15:42:25.477 15540-15540/com.app.tablesign I/System.out: waiting for debugger to settle...
12-06 15:42:25.677 15540-15540/com.app.tablesign I/System.out: waiting for debugger to settle...
12-06 15:42:25.878 15540-15540/com.app.tablesign I/System.out: debugger has settled (1497)
12-06 15:42:25.899 15540-15540/com.app.tablesign W/System: ClassLoader referenced unknown path: /data/app/com.app.tablesign-1/lib/arm
12-06 15:42:25.972 15540-15540/com.app.tablesign D/LeakCanary: Installing AppWatcher
12-06 15:42:25.984 15540-15540/com.app.tablesign I/art: Rejecting re-init on previously-failed class java.lang.Class<leakcanary.internal.AndroidSupportFragmentDestroyWatcher$fragmentLifecycleCallbacks$1>
12-06 15:42:25.985 15540-15540/com.app.tablesign I/art: Rejecting re-init on previously-failed class java.lang.Class<leakcanary.internal.AndroidSupportFragmentDestroyWatcher$fragmentLifecycleCallbacks$1>
12-06 15:42:25.989 15540-15540/com.app.tablesign I/art: Rejecting re-init on previously-failed class java.lang.Class<leakcanary.internal.AndroidSupportFragmentDestroyWatcher$fragmentLifecycleCallbacks$1>
Rejecting re-init on previously-failed class java.lang.Class<leakcanary.internal.AndroidSupportFragmentDestroyWatcher$fragmentLifecycleCallbacks$1>
12-06 15:42:25.991 15540-15540/com.app.tablesign I/art: Rejecting re-init on previously-failed class java.lang.Class<leakcanary.internal.AndroidSupportFragmentDestroyWatcher$fragmentLifecycleCallbacks$1>
12-06 15:42:25.991 15540-15540/com.app.tablesign D/AndroidRuntime: Shutting down VM
--------- beginning of crash
12-06 15:42:25.993 15540-15540/com.app.tablesign E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.tablesign, PID: 15540
java.lang.RuntimeException: Unable to get provider leakcanary.internal.AppWatcherInstaller$MainProcess: java.lang.reflect.InvocationTargetException
at android.app.ActivityThread.installProvider(ActivityThread.java:5156)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:742)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at leakcanary.internal.FragmentDestroyWatcher.getWatcherIfAvailable(FragmentDestroyWatcher.kt:99)
at leakcanary.internal.FragmentDestroyWatcher.install(FragmentDestroyWatcher.kt:62)
at leakcanary.internal.InternalAppWatcher.install(InternalAppWatcher.kt:70)
at leakcanary.internal.AppWatcherInstaller.onCreate(AppWatcherInstaller.kt:35)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)聽
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)聽
at android.app.ActivityThread.-wrap1(ActivityThread.java)聽
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)聽
at android.os.Handler.dispatchMessage(Handler.java:102)聽
at android.os.Looper.loop(Looper.java:148)聽
at android.app.ActivityThread.main(ActivityThread.java:5417)聽
at java.lang.reflect.Method.invoke(Native Method)聽
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:742)聽
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)聽
Caused by: java.lang.NoClassDefFoundError: leakcanary.internal.AndroidSupportFragmentDestroyWatcher$fragmentLifecycleCallbacks$1
at leakcanary.internal.AndroidSupportFragmentDestroyWatcher.<init>(AndroidSupportFragmentDestroyWatcher.kt:30)
at java.lang.reflect.Constructor.newInstance(Native Method)聽
at leakcanary.internal.FragmentDestroyWatcher.getWatcherIfAvailable(FragmentDestroyWatcher.kt:99)聽
at leakcanary.internal.FragmentDestroyWatcher.install(FragmentDestroyWatcher.kt:62)聽
at leakcanary.internal.InternalAppWatcher.install(InternalAppWatcher.kt:70)聽
at leakcanary.internal.AppWatcherInstaller.onCreate(AppWatcherInstaller.kt:35)聽
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)聽
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)聽
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)聽
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)聽
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)聽
at android.app.ActivityThread.-wrap1(ActivityThread.java)聽
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)聽
at android.os.Handler.dispatchMessage(Handler.java:102)聽
at android.os.Looper.loop(Looper.java:148)聽
at android.app.ActivityThread.main(ActivityThread.java:5417)聽
at java.lang.reflect.Method.invoke(Native Method)聽
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:742)聽
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)聽
Thanks @devism, thanks to your project I was able to figure it out.
In ./node_modules/cordova-android/spec/fixtures/android_studio_project/app/build.gradle you have this:
compile 'com.android.support:appcompat-v7:23.4.0'
This means your project depends on a really old version of the support library. In version 25.0.2 of the support library (February 2017), the FragmentManager.FragmentLifecycleCallbacks class changed from being an inner non static class (which was a mistake) to being a static class. That means that any code compiled against a later version of FragmentManager.FragmentLifecycleCallbacks will fail at runtime because that static class is not included.
In our initialization code we check for whether android.support.v4.app.Fragment is in the classpath (which is true here) and then try to load leakcanary.internal.AndroidSupportFragmentDestroyWatcher which has been compiled against the newer API and therefore fails to load. This leads to Class.forName throwing a NoClassDefFoundError instead of a ClassNotFoundException, which in turns leads to a crash on startup because we don't catch NoClassDefFoundError. cc @AndreasBoehm would might find this interesting (we definitely couldn't have thought of that)
I'm still getting the same error. Do I have to change the following to something else? :
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0'
You can wait for the next release (no date yet) or use the snapsnot version: https://square.github.io/leakcanary/faq/#how-do-i-use-the-snapshot-version
l slove this problem with multidex in android 4.3
same bug銆倀hrow NullPointerException using Context at calling getResources from Application
@jarlen if you have a crash happening on the latest version of LeakCanary please open an issue with the full stacktrace. Your comment here does not provide enough information.
Most helpful comment
You can wait for the next release (no date yet) or use the snapsnot version: https://square.github.io/leakcanary/faq/#how-do-i-use-the-snapshot-version