当我启动我的应用程序时如何解决这个异常的 NoClassDefFoundError

How to fix this unusual NoClassDefFoundError when I launch my app

我的 Android 应用发生了奇怪的崩溃。事情是这样的:

如果我将应用程序发送到 phone(比如 蓝牙、Xender 或通过电子邮件发送给我的朋友),然后我启动它,应用程序崩溃,根本没有晚上开始。它甚至不显示启动画面。只需点击启动器图标,应用程序就会崩溃。但是,如果我从 Android Studio(即构建和 运行)启动应用程序,它不会崩溃。如果我完全卸载该应用程序(在从 Android Studio 运行ning 之后)并通过蓝牙或 Xender 发送,它将不再崩溃。

到目前为止,我已经能够在 Kitkat 上重现此错误,但无法在 Lollipop + 上重现。

请注意,我的应用程序处于调试版本中。

这是应用生成的崩溃数据:

08-26 05:32:52.865 1630-1630/com.ozuf.poster W/dalvikvm: VFY: unable to resolve static field 20 (applicationId) in Lcom/android/tools/fd/runtime/AppInfo;
08-26 05:32:52.865 1630-1630/com.ozuf.poster D/dalvikvm: VFY: replacing opcode 0x62 at 0x0016
08-26 05:32:52.865 1630-1630/com.ozuf.poster W/dalvikvm: VFY: unable to resolve static field 19 (applicationClass) in Lcom/android/tools/fd/runtime/AppInfo;
08-26 05:32:52.865 1630-1630/com.ozuf.poster D/dalvikvm: VFY: replacing opcode 0x62 at 0x0001
08-26 05:32:52.869 1630-1630/com.ozuf.poster W/dalvikvm: VFY: unable to resolve static field 22 (usingApkSplits) in Lcom/android/tools/fd/runtime/AppInfo;
08-26 05:32:52.869 1630-1630/com.ozuf.poster D/dalvikvm: VFY: replacing opcode 0x63 at 0x0000
08-26 05:32:52.869 1630-1630/com.ozuf.poster W/dalvikvm: VFY: unable to resolve static field 22 (usingApkSplits) in Lcom/android/tools/fd/runtime/AppInfo;
08-26 05:32:52.869 1630-1630/com.ozuf.poster D/dalvikvm: VFY: replacing opcode 0x63 at 0x0002
08-26 05:32:52.869 1630-1630/com.ozuf.poster I/dalvikvm: DexOpt: unable to optimize static field ref 0x0013 at 0x1b in Lcom/android/tools/fd/runtime/BootstrapApplication;.<init>
08-26 05:32:52.869 1630-1630/com.ozuf.poster I/dalvikvm: DexOpt: unable to optimize static field ref 0x0013 at 0x1d in Lcom/android/tools/fd/runtime/BootstrapApplication;.createRealApplication
08-26 05:32:52.869 1630-1630/com.ozuf.poster I/dalvikvm: DexOpt: unable to optimize static field ref 0x0013 at 0x2a in Lcom/android/tools/fd/runtime/BootstrapApplication;.createRealApplication
08-26 05:32:52.869 1630-1630/com.ozuf.poster I/dalvikvm: DexOpt: unable to optimize static field ref 0x0014 at 0x15 in Lcom/android/tools/fd/runtime/BootstrapApplication;.onCreate
08-26 05:32:52.873 1630-1630/com.ozuf.poster I/dalvikvm: DexOpt: unable to optimize static field ref 0x0014 at 0x45 in Lcom/android/tools/fd/runtime/BootstrapApplication;.onCreate
08-26 05:32:52.873 1630-1630/com.ozuf.poster I/dalvikvm: DexOpt: unable to optimize static field ref 0x0014 at 0x6f in Lcom/android/tools/fd/runtime/BootstrapApplication;.onCreate
08-26 05:32:52.873 1630-1630/com.ozuf.poster I/dalvikvm: DexOpt: unable to optimize static field ref 0x0014 at 0x99 in Lcom/android/tools/fd/runtime/BootstrapApplication;.onCreate
08-26 05:32:52.873 1630-1630/com.ozuf.poster D/AndroidRuntime: Shutting down VM
08-26 05:32:52.873 1630-1630/com.ozuf.poster W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4cacb20)
08-26 05:32:52.873 1630-1630/com.ozuf.poster E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.ozuf.poster, PID: 1630
                                                                   java.lang.NoClassDefFoundError: com.android.tools.fd.runtime.AppInfo
                                                                       at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:231)
                                                                       at android.app.Application.attach(Application.java:181)
                                                                       at android.app.Instrumentation.newApplication(Instrumentation.java:991)
                                                                       at android.app.Instrumentation.newApplication(Instrumentation.java:975)
                                                                       at android.app.LoadedApk.makeApplication(LoadedApk.java:502)
                                                                       at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4301)
                                                                       at android.app.ActivityThread.access00(ActivityThread.java:135)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:136)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                       at dalvik.system.NativeStart.main(Native Method)
08-26 05:33:28.765 1630-1630/com.ozuf.poster I/Process: Sending signal. PID: 1630 SIG: 9

项目gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'com.google.gms:google-services:3.0.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}


dependencies {
}

应用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' }
}

apply plugin: 'android-apt'

android {
    compileSdkVersion 23
    buildToolsVersion '24.0.0'
    defaultConfig {
        applicationId "com.ozuf.poster"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 17
        versionName '0.17.5 Alpha'
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}


dependencies {
    compile project(':volley')
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    apt 'com.jakewharton:butterknife-compiler:8.2.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.2@aar') {
        transitive = true;
    }
    compile('com.crashlytics.sdk.android:answers:1.3.9@aar') {
        transitive = true;
    }
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    compile 'org.jsoup:jsoup:1.9.2'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.google.firebase:firebase-core:9.0.2'
    compile 'com.google.firebase:firebase-ads:9.0.2'
    compile 'com.jakewharton:butterknife:8.2.1'
    compile 'org.greenrobot:eventbus:3.0.0'
    compile 'com.android.support:customtabs:23.4.0'
} 

apply plugin: 'com.google.gms.google-services'

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.ozuf.poster">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_LOGS"/>

    <application
        android:name=".activities.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".receivers.MyReceiver"
            android:enabled="true">
        </receiver>

        <provider
            android:name=".utils.SuggestionProvider"
            android:authorities="com.ozuf.poster.utils.SuggestionProvider"/>

        <activity
            android:name=".activities.SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <data
                    android:host="www.ozuflink.com"
                    android:pathPrefix="/20"
                    android:scheme="http"/>
                <data
                    android:host="www.ozuflink.com"
                    android:pathPrefix="/20"
                    android:scheme="https"/>
                <data
                    android:host="ozuflink.com"
                    android:pathPrefix="/20"
                    android:scheme="http"/>
                <data
                    android:host="ozuflink.com"
                    android:pathPrefix="/20"
                    android:scheme="https"/>
                <data
                    android:host="www.ozuflink.com"
                    android:pathPrefix="/rocks"
                    android:scheme="http"/>
                <data
                    android:host="www.ozuflink.com"
                    android:pathPrefix="/rocks"
                    android:scheme="https"/>
                <data
                    android:host="ozuflink.com"
                    android:pathPrefix="/rocks"
                    android:scheme="http"/>
                <data
                    android:host="ozuflink.com"
                    android:pathPrefix="/rocks"
                    android:scheme="https"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden">
        </activity>
        <activity
            android:name=".activities.PostActivity"
            android:label="@string/app_name"
            android:parentActivityName=".activities.MainActivity"
            android:windowSoftInputMode="stateHidden">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.ozuf.poster.activities.MainActivity"/>
        </activity>
        <activity
            android:name=".activities.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.ozuf.poster.activities.MainActivity"/>

            <intent-filter>
                <action android:name="android.intent.action.MANAGE_NETWORK_USAGE"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.AboutActivity"
            android:label="@string/title_activity_about"
            android:parentActivityName=".activities.MainActivity"
            android:theme="@style/AppTheme">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.ozuf.poster.activities.MainActivity"/>
        </activity>
        <activity
            android:name=".activities.StoreActivity"
            android:label="@string/title_activity_store"
            android:theme="@style/StoreTheme">
        </activity>
        <activity
            android:name=".activities.SearchResultsActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH"/>
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable"/>
        </activity>
        <activity
            android:name=".activities.QSettingsActivity"
            android:label="@string/title_activity_qsettings">
        </activity>
        <activity
            android:name=".activities.fSettingsActivity"
            android:label="@string/title_activity_f_settings">
        </activity>
    </application>

</manifest>

请问有什么解决办法吗?

替换您 gradle

中的库
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'

替换成下面的就可以了

compile 'com.android.support:support-v4:24.0.0-alpha1'
compile 'com.android.support:appcompat-v7:24.0.0-alpha1'
compile 'com.android.support:design:24.0.0-alpha1'

听起来像是即时 运行 问题。

请禁用 Instant 运行 并尝试清理构建。

它应该有效