APK errors

ClassNotFound / NoSuchMethod crash

Error text

java.lang.ClassNotFoundException: Didn't find class "com.example.Foo" on path: DexPathList
java.lang.NoSuchMethodError: No virtual method bar()V in class Lcom/example/Foo;

Cause

A class or method is missing at runtime. Usually R8 removed code, a library exists in two versions, or multidex is misconfigured.

Step-by-step fix

  1. Grab the full stack trace with adb logcat -b crash.
  2. If it happens only in release, R8 is the cause: add a -keep rule.
  3. Protect reflection and JSON model classes with -keep.
  4. Use dependencies to see whether one library exists in two versions.
  5. If the trace is scrambled, run retrace with mapping.txt.

Example commands

adb logcat -b crash
./gradlew :app:dependencies --configuration releaseRuntimeClasspath
# proguard-rules.pro
-keep class com.example.Foo { *; }
retrace mapping.txt trace.txt

How to prevent it

If you use reflection, test the release APK every time.

Author and license: Nibras Code