Android 5.x ClassNotFoundException 在 6.0+ 上工作正常

Android 5.x ClassNotFoundException works fine on 6.0+

我已将项目的 minSdkVersion 从 19 更新为 21。这导致 5.0/5.1 设备出现问题,我无法 运行 应用程序。我的应用程序 class 不断收到 ClassNotFoundException。完整日志、应用程序 class 和 gradle 文件如下。如果我将我的项目恢复到 minSdkVersion 19,那么该应用程序将 运行 在 4.4+ 上没有问题。

我试过的

日志

04-27 14:37:07.152 6278-6278/? E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.package.testapp, PID: 6278
     java.lang.RuntimeException: Unable to instantiate application com.package.TestApplication: java.lang.ClassNotFoundException: Didn't find class "com.package.Application" on path: DexPathList[[zip file "/data/app/com.package.testapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.package.testapp-1/lib/x86_64, /vendor/lib64, /system/lib64]]
         at android.app.LoadedApk.makeApplication(LoadedApk.java:563)
         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4526)
         at android.app.ActivityThread.access00(ActivityThread.java:151)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:135)
         at android.app.ActivityThread.main(ActivityThread.java:5254)
         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:903)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
      Caused by: java.lang.ClassNotFoundException: Didn't find class "com.package.TestApplication" on path: DexPathList[[zip file "/data/app/com.package.testapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.package.testapp-1/lib/x86_64, /vendor/lib64, /system/lib64]]
         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:980)
         at android.app.LoadedApk.makeApplication(LoadedApk.java:558)
         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4526) 
         at android.app.ActivityThread.access00(ActivityThread.java:151) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:135) 
         at android.app.ActivityThread.main(ActivityThread.java:5254) 
         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:903) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
        Suppressed: java.lang.ClassNotFoundException: com.package.testapp.welcome.TestClaimApplication
         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

Gradle

    buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
    }

    apply plugin: 'com.android.application'
    apply plugin: 'io.fabric'

    repositories {
    maven { url 'https://maven.fabric.io/public' }
    }

android {

    compileSdkVersion 25
    buildToolsVersion '25.0.3'

    defaultConfig {
        applicationId "com.package.testapp"
        minSdkVersion 21
        multiDexEnabled = true
        targetSdkVersion 25
        versionCode 2
        versionName "0.7"
        renderscriptTargetApi 18 // support mode not supported 21+
        renderscriptSupportModeEnabled true
        testInstrumentationRunner 
    "android.support.test.runner.AndroidJUnitRunner"
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    productFlavors {
        companylocal
        companymaven
    }
}

dependencies {


    def ext = rootProject.ext;

    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile "com.android.support:appcompat-v7:${ext.supportLibraryVersion}"
    compile "com.android.support:design:${ext.supportLibraryVersion}"
    compile 'com.romandanylyk:pageindicatorview:0.0.9'
    compile "com.jakewharton:butterknife:${ext.butterknifeLibraryVersion}"
    annotationProcessor "com.jakewharton:butterknife-compiler:${ext.butterknifeLibraryVersion}"
    compile "uk.co.chrisjenx:calligraphy:2.2.0"
    compile 'com.android.support:multidex:1.0.1'
    // Crashlytics Kit - for crash handling
    compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
        transitive = true
    }
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.0'
    testCompile "com.squareup.okhttp3:mockwebserver:${ext.okHttp3LibraryVersion}"
    testCompile('com.squareup.assertj:assertj-android:1.1.1') {
        exclude module: 'support-annotations'
    }
}

申请

public class TestApplication extends Application {

    Engine engine;

    @Override
    public void onCreate() {
        super.onCreate();

        String privateServerUrlLocal = getString(R.string.server_url);
        ProfileLauncher profileLauncher = getProfileLauncher();
        LoginLauncher loginLauncher = getLoginLauncher();

        // Set up Crash Analytics
        final CrashlyticsCore crashlyticsCore = new CrashlyticsCore.Builder().disabled(DEBUG).build();
        Fabric.with(this, new Crashlytics.Builder().core(crashlyticsCore).build());

        Intent intent = new Intent(this, LogoutService.class);
        startService(intent);

        CertificatePinner certificatePinner = new CertificatePinner.Builder()
                .add("")
                .add("")
                .build();

        engine = new Engine.Builder(privateServerUrlLocal, getString(R.string.other_server_url))
                .setCertificatePinner(certificatePinner)
                .setSDKPartnerSetupManager(getSdkPartnerManager())
                .setLogoutActionHandler(getLogoutActionHandler())
                .setVLocationManager(new VLocationManagerImp())
                .setDebug(DEBUG)
                .addDeepLinkHandler(new ClaimDeepLinkHandler(loginLauncher, profileLauncher))
                .addDeepLinkHandler(new LinkGiftCodeDeeplinkHandler(loginLauncher, profileLauncher))
                .addDeepLinkHandler(new OpenLinkDeepLinkHandler())
                .addDeepLinkHandler(new DetailsDeepLinkHandler())
                .addClaimConfig(new ClaimConfig(false, false, false, false, true))
                .build();
        engine.startup(this);
    }



    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.package.testapp">

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />
<uses-feature
    android:name="android.hardware.camera.autofocus"
    android:required="false" />
<uses-feature
    android:name="android.hardware.camera.flash"
    android:required="false" />

<!-- Required for g+ login -->
<uses-permission android:name="android.permission.CAMERA" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:name=".TestApplication"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service
            android:name="com.package.companycommon.ui.LogoutService"
            android:stopWithTask="true" />

        <activity
            android:name=".LaunchActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".welcome.WelcomeActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus" />
        <activity
            android:name=".RegisterActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus"
            android:windowSoftInputMode="adjustResize" />
        <activity
            android:name=".LoginActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus"
            android:windowSoftInputMode="adjustResize" />
        <activity
            android:name=".ActivationActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus"
            android:windowSoftInputMode="adjustResize" />
        <activity
            android:name=".AddPhoneNumberActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus"
            android:windowSoftInputMode="adjustResize" />
        <activity
            android:name=".ForgotPasswordActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus"
            android:windowSoftInputMode="adjustResize" />
        <activity
            android:name=".GiftHistoryActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus" />
        <activity
            android:name="com.package.PartnerLoginHiddenActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus" />
        <activity
            android:name=".tutorial.TutorialActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus" />
        <activity
            android:name=".CreateNewPasswordActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar.ClearStatus" />
        <activity
            android:name=".settings.SettingsActivity"
            android:screenOrientation="portrait">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".GiftHistoryActivity" />
        </activity>
        <activity
            android:name=".settings.ExtraSettingsActivity"
            android:label="@string/settings"
            android:screenOrientation="portrait">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".settings.SettingsActivity" />
        </activity>
        <activity
            android:name="companyapp.company.NewsActivity"
            android:label="@string/notifications"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".termsandconditions.CompanyTermsAndConditionsActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme" />

        <activity
            android:name="com.package.companycommon.ui.PhotoViewerActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme" />

        <activity
            android:name="com.package.company.details.CompanyDetailActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme" />
        <activity
            android:name="com.package.company.details.CompanyDetailActivityLocation"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.package.company.details.CompanyDetailActivity" />
        </activity>
        <activity
            android:name="com.package.company.ClaimActivity"
            android:theme="@style/AppTheme.NoActionBar"
            tools:replace="android:theme" />
    </application>

</manifest>

这意味着您的 multiDex 无法正常工作,因此丢失了一些文件。我认为 android 5.0 使用 multiDex 的方式不同。您是否在应用程序 class 中覆盖了某些内容?

您需要启用 multidex 吗?我一直 运行 对此有疑问。如果您仍然达到 65k 方法限制,我一直认为 multidex 是最后的手段。如果您没有 运行ning 到此方法限制,则不需要 multidex。如果您 运行 处于此限制,我建议您先使用 progaurd 来解决此问题。如果即使在使用 proguard 后仍然超出方法限制,请使用 multidex。

尝试删除 compile 'com.android.support:multidex:1.0.1'。 Google 文档 says

Therefore, if your minSdkVersion is 21 or higher, you do not need the multidex support library.

我不确定这是否有帮助,但可能值得一试。

出于某种原因,这个 class 不再在您的主 dex 中。

Declare the class in the main-classes list, using multiDexKeepFile (listing the classes in gradle) or multiDexKeepProguard(指向主要 classes 的特定混淆文件)。

尝试启用 Progaud 并禁用 multidex。

您不仅会解决这个问题,而且您的 apk 会更小,安装速度更快,并且安装 运行 次。

请注意,如果您需要这方面的帮助,请发表评论,我们可以解决。

  • 好的,所以它不是 Progaurd 或 MultiDex

错误是缺少com.package.Application。您发布的代码(或清单)的 None 包含对该 class 的引用。你在哪里定义com.package.Application?如果您不在项目中的哪个位置,它会被引用吗? (我会在项目范围内搜索 "com.package.Application" 来解决这个问题。)

您需要添加您的 android:name 以及完整的路径。

android:name="android.support.multidex.MultiDexApplication" 

并且您的 TestApplication 应该扩展 MultiDexApplication。

根据文档,如果需要更多详细信息,请参阅。 MultiDex Documentation

尝试在清单中给出完整路径-

  <application
        android:name=".TestApplication"

也手动删除构建文件夹-有时清理不会work.Also手动卸载应用程序并启动。

我已经根据你的代码和配置制作了 demo 并且它在下面的棉花糖中工作

下载并检查它并与您的代码进行比较。还有一件事,您如何创建包名称为 com.package.testapp 的项目。我无法创建具有相同包名的项目

创建 multidex-config.txt 以将此 类 保留在主 DEX 文件中:

com/package/Application.class
com/package/TestApplication.class
com/package/testapp/welcome/TestClaimApplication.class

multiDexKeepFile 添加到 build.gradle 的 android.buildTypes.release 部分,如下所示:

android {
    buildTypes {
        release {
            multiDexKeepFile file 'multidex-config.txt'
        }
    }
}

请尝试禁用 Instant 运行 选项,然后 运行 应用程序。在尝试了很多解决方案后,它对我有用,我终于解决了这个问题。

当目标 api 21 时,编译器将读取 100 个 dex 文件的限制。所以我发现一个简单的解决方案是将它添加到 gradle 文件中的 dexOptions。这会将您所有的 dex 文件合并到尽可能少的数量。

在您的 build.gradle 文件中添加以下内容

android {
    ...
    dexOptions {
        preDexLibraries = false
    }
}