3

Background

Not so long ago, Google has changed the Google Play Console, so that crashes are automatically being reported as soon as they occur, having it as part of what they call "Android Vital".

From this day, I got many crash reports that I've decided to fix for my app.

The problem

Among some bugs I've fixed (and one that I haven't, here), there is another quite common crash, with this crash log or similar:

java.lang.RuntimeException: 
  at android.app.LoadedApk.makeApplication(LoadedApk.java:572)
  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4768)
  at android.app.ActivityThread.access$1500(ActivityThread.java:176)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1498)
  at android.os.Handler.dispatchMessage(Handler.java:111)
  at android.os.Looper.loop(Looper.java:194)
  at android.app.ActivityThread.main(ActivityThread.java:5576)
  at java.lang.reflect.Method.invoke(Native Method:0)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
Caused by: java.lang.ClassNotFoundException: 
  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
  at android.app.Instrumentation.newApplication(Instrumentation.java:984)
  at android.app.LoadedApk.makeApplication(LoadedApk.java:567)

This crash seem to be affecting various Android versions and devices, yet there aren't much clues about how to fix it:

enter image description here

What I've tried

The only thing I've found to solve such a crash, is by using multi-dex, as written here.

Seeing that even though my app is very small and barely using third party libraries (hence doesn't even get near the 64K methods count), and that multi-dex is not even needed on Android 5.x and above, I was still curious if this would help, but in just one day of trying it, it actually caused the crash to occur more frequently:

enter image description here

So I quickly stopped using it, and came back to without using it.

The questions

  1. Why does it occur?
  2. Is there any way to avoid this? Will avoiding extending Application class help?
  3. How common is it for other apps?
  4. Why would multi-dexing actually cause more issues with it?
  5. From users point of view, do users who have this issue - always have it? Does specific devices or Android versions have this issue? Does a restart of the app or re-installing of it help? What exactly is the experience the users get from this?
android developer
  • 106,412
  • 122
  • 641
  • 1,128
  • This is something that affects mostly early versions of Android. I've seen this happen to me. One thing that actually worked for me was to generate the APK with another laptop. I'm still not sure of the cause but I'm giving you a quick solution. – Gustavo Gomes Jun 05 '17 at 19:21
  • Just one thing, a lot of people also been having problems with Instant Run being enabled. Look here: https://stackoverflow.com/questions/39871338/unable-to-instantiate-application-caused-by-java-lang-classnotfoundexception-d – Gustavo Gomes Jun 05 '17 at 19:26
  • @Gugas But isn't instant-run used only for debug build? Is it also used for release? – android developer Jun 05 '17 at 20:50
  • AFAIK, multi-dex has nothing to do with this error. Why do you think it can help? The framework can't load your Application class. I've seen this type of issues before, and most of them were caused by VM verifier that (silently) rejects loading of your class (though, it generally prints to logcat the reason for that). Your best bet will be to find someone who has one of the listed devices and try to reproduce it there.. – Alex Lipov Jun 06 '17 at 05:23
  • @AlexLipov Well it's what I've read somewhere (to enable multidex), yet even though I didn't believe it can help, I wanted to try because I could be wrong. The weird thing is that it caused the crash to occur a lot more frequently. – android developer Jun 06 '17 at 09:08
  • Same issues here: https://stackoverflow.com/questions/44145786/thousands-of-strange-crashes-in-new-google-play-console-version?noredirect=1#comment76717953_44145786 and https://stackoverflow.com/questions/44495362/android-classnotfoundexception-crash-report-happening-rarely-after-each-updat. @dakshbhatt21 seems to have the issue without Proguard but with Multidex only! Android developer do you extend the Application class in your app? – Jérémy Reynaud Jul 02 '17 at 11:59
  • @JérémyReynaud I do extend it, but there isn't much in there. Why ? – android developer Jul 06 '17 at 13:19
  • The android source code where the exception occurs seems to play with the application class name. So I suspected proguard to mess up with it (sounds odd but...). Adding the 2 lines in my proguard files seems to have fixed the issue. Give it a try and tell us if it works for you too. – Jérémy Reynaud Jul 06 '17 at 14:49

2 Answers2

0

I managed to decrease occurrences of it by adding these lines to my Proguard file:

-keep class android.app.Application
-keep class <<MyPackage>>.MyApplication

that may be shortened to:

-keep class * extends android.app.Application

According to android source code I suppose it fixed the first issue.

I also updated all my libs to the last versions (Google Play Services 11.0.2, support lib 25.4.0) that should have fixed the second one.

UPDATE: it doesn't fix the issue completely. I got far less occurrences but there are still some.

Jérémy Reynaud
  • 2,832
  • 1
  • 22
  • 34
0

If inside your gradle you have minify enabled, you also have a file called app\build\outputs\mapping\release\usage.txt.

Inside this file you will see a list of all stripped code from your apk. Check there if any code that you actually need has been removed by proguard.

If you find any needed code there, then you must add instructions inside your proguard configuration file in order to keep it.