【问题标题】:Android: Rare and Non-Reproducible ClassNotFoundExceptionAndroid:罕见且不可重现的 ClassNotFoundException
【发布时间】:2020-01-06 17:38:42
【问题描述】:

我有一个已发布的应用程序,它从用户那里收到关于 ClassNotFoundException 的报告,我已经超出了我的智慧试图修复这些问题。我无法重现此崩溃,并且在 SO 上出现此错误的类似实例要么是依赖路径不正确,要么是 MainActivity 中的包与清单不同。

依赖路径错误的问题是 Eclipse 的问题,而所有其他错误都可以通过 Android Studio 的“同步”和“清理项目”轻松修复(我没有这样做)。

我已经三遍检查了所有包名称,没有发现任何差异。

这是我的清单:

<?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>

这是我经常从用户那里得到的错误报告:

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.

罪魁祸首似乎是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,但我还没有找到为什么它被一些用户而不是大多数用户调用,更重要的是——如何解决它。

【问题讨论】:

  • 这可能与Installation Unsuccessful 问题有关,基于Failed to remove obsolete file ... Permission denied 的行
  • 我也一直在想可能有连接,但没想到安装程序真的使用了manifest中定义的权限。
  • 您是否尝试过在重新安装之前从设备上卸载应用程序?
  • 正如我所提到的,我没有遇到这种崩溃,我的绝大多数用户也没有。这是我不时收到的反复发生的崩溃报告。
  • android:largeHeap="true"&gt;&gt; 行的清单中有错字。 xml 行以&gt;&gt; 结尾。

标签: android android-studio android-runtime


【解决方案1】:

可能的原因 1:android:largeHeap="true"&gt;&gt; 行中的清单中有错字。 xml 行以&gt;&gt; 结尾。这可能会阻止 Intent-Filter 执行。

可能的原因2:我的猜测是手机正在搜索类&lt;PackageName&gt;.&lt;PackageName&gt;.&lt;ClassName&gt;而不是&lt;PackageName&gt;.&lt;ClassName&gt;。所以肯定没有找到&lt;PackageName&gt;.&lt;PackageName&gt;.&lt;ClassName&gt;,结果是ClassNotFoundException。在 android studio 中,在清单中,默认情况下活动名称是这样写的:android:name=".&lt;ClassName&gt;" 而不是 android:name="&lt;PackageName&gt;.&lt;ClassName&gt;"。你可以试试这个。

大多数手机可能会检查是否在清单中的类名之前添加了包名。如果添加了它,那么他们在搜索类时不会添加它。虽然在搜索类时,默认情况下很少有手机必须添加包名称。因此,他们找不到您的班级。

【讨论】:

  • 我认为这更多的是您提到的明显,无错误的错字而不是这个问题。这是我为避免另一个 ClassNotFoundException 而采用的解决方案。
  • 那个错字在我尝试的时候并没有给我一个错误。太好了,请给我赏金
  • 我认为您应该在我的另一个问题stackoverflow.com/questions/36314269/… 中使用您的答案,因为虽然您的答案可能是解决问题的答案,但它仍然不是 ClassNotFoundException 的可靠解释。在那里发布您的答案,一旦测试人员确认可以再次安装它,我将奖励您赏金。如果事实证明它也会导致此错误,您将获得两个赏金。
  • 我会在它到期前一天给你赏金,因为仍然没有简明的方法来确定它是否确实导致了崩溃,但它确实看起来很有说服力。确保您对您的答案进行小幅编辑,以便在我这样做时将其包含在内。但是,从那以后我就再也没有报告过这个崩溃了。
  • 原来这没有帮助。
【解决方案2】:

当然,您将活动命名错误。一个活动不能在其名称属性中包含包的名称。

因为你已经在这里定义了包名
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.johan.fsc"&gt;

你不应该在这里提到那个名字
&lt;activity android:name="com.johan.fsc.MainActivity"

您应该使用包名和类名的唯一情况是当 java 类驻留在不同的包中时,如 import 语句中所做的那样。
即使您的活动不存在于src 文件夹中而是存在于嵌套文件夹中,android studio 也应该处理清单和活动本身的链接。再一次,不应该在那里使用包名。

【讨论】:

  • 绝对不能?该应用程序使用此配置。不仅如此,这是我在阅读另一个带有ClassNotFoundException 错误的问题时采用的一种方法,因此只是最近的一次更改。没有区别,所以我将恢复为默认格式。
猜你喜欢
  • 2011-02-09
  • 2020-02-24
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
相关资源
最近更新 更多