Android: rare and irreproducible ClassNotFoundException

I have a published application that receives ClassNotFoundException reports from users that I went beyond my mind trying to fix. I could not reproduce this failure and similar cases of this error here on SO, there were either cases of the wrong path to the dependency, or another package in MainActivity than the manifest.

Problems with the wrong dependency path are Eclipse's thing, while all other errors can be easily fixed using the Android sync "sync" and "clean project" (which I did to no avail).

I triple checked all the package names and did not find any discrepancies.

Here is my manifest:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.johan.fsc"> <uses-permission android:name = "android.permission.VIBRATE"/> <uses-permission android:name="com.android.vending.BILLING" /> <application android:allowBackup="true" android:icon="@drawable/logo" android:label="@string/app_name" android:fullBackupContent="true"> <activity android:name="com.johan.fsc.MainActivity" android:screenOrientation="portrait" android:label="@string/app_name" android:theme="@style/splashscreenTheme" android:hardwareAccelerated="false" android:largeHeap="true">> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name = "com.johan.fsc.SettingsActivity" android:theme="@style/PreferencesTheme"/> </application> 

Here is the error report that I always get from users:

 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.johan.fsc/com.johan.fsc.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.johan.fsc.MainActivity" on path: DexPathList[[zip file "/data/app/com.johan.fsc-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595) at android.app.ActivityThread.access$800(ActivityThread.java:178) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5624) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.johan.fsc.MainActivity" on path: DexPathList[[zip file "/data/app/com.johan.fsc-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] 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.newActivity(Instrumentation.java:1071) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) ... 10 more Suppressed: java.io.IOException: Failed to open oat file from dex location '/data/app/com.johan.fsc-1/base.apk' at dalvik.system.DexFile.openDexFileNative(Native Method) at dalvik.system.DexFile.openDexFile(DexFile.java:295) at dalvik.system.DexFile.<init>(DexFile.java:80) at dalvik.system.DexFile.<init>(DexFile.java:59) at dalvik.system.DexPathList.loadDexFile(DexPathList.java:262) at dalvik.system.DexPathList.makeDexElements(DexPathList.java:231) at dalvik.system.DexPathList.<init>(DexPathList.java:109) at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48) at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:65) at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:57) at android.app.LoadedApk.getClassLoader(LoadedApk.java:370) at android.app.LoadedApk.makeApplication(LoadedApk.java:562) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4818) at android.app.ActivityThread.access$1500(ActivityThread.java:178) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531) ... 7 more Caused by: java.io.IOException: Failed to open oat file from /data/app/com.johan.fsc-1/arm/base.odex (error Failed to open oat filename for reading: No such file or directory) (no dalvik_cache availible) and relocation failed. ... 22 more Caused by: java.io.IOException: ... 22 more Caused by: java.io.IOException: Failed to remove obsolete file from /data/dalvik-cache/arm/ data@app @ com.johan.fsc-1@base.apk @classes.dex when searching for dex file /data/app/com.johan.fsc-1/base.apk: Permission denied ... 22 more Suppressed: java.lang.ClassNotFoundException: com.johan.fsc.MainActivity at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 13 more Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available/output. 

It would seem the culprit of java.io.IOException: Failed to remove obsolete file from /data/dalvik-cache/arm/ data@app @ com.johan.fsc-1@base.apk @classes.dex when searching for dex file /data/app/com.johan.fsc-1/base.apk: Permission denied , but I did not find why it is called for some users and not for most, and more importantly, how to fix it.

+7
android android-studio android-runtime
source share
2 answers

Possible reason 1: There is a typo in the manifest in the android:largeHeap="true">> line android:largeHeap="true">> . xml ends with >> . This may interfere with the implementation of the intent filter.

Possible reason 2: I assume that the phone is looking for the class <PackageName>.<PackageName>.<ClassName> instead of <PackageName>.<ClassName> . So definitely <PackageName>.<PackageName>.<ClassName> will not be found, and a ClassNotFoundException will be the result. In the android studio in the manifest, the default activity name is written as follows: android:name=".<ClassName>" instead of android:name="<PackageName>.<ClassName>" . You can try.

Most phones can check if packagename is added before the class name in the manifest. If it is added, it does not add it during class search. Although several phones should add the default package name when searching for a class. Therefore, they cannot find your class.

+3
source share

Of course, you incorrectly name your activity. The action CANNOT consist of the name of the package in its name attribute.

Since you already define the package name here
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.johan.fsc">

You should not mention this name here
<activity android:name="com.johan.fsc.MainActivity"

The only situation when you must use the package name together with the class name is when the java class is in another package, as is done in the import statements.
Even if your activity is not in the src folder and is instead present in a subfolder, Android Studio must handle the binding of the manifest and the activity itself. And yet, the package name should not be used there.

0
source share

All Articles