Android Studio 意外顶级异常:

Android Studio UNEXPECTED TOP-LEVEL EXCEPTION:

今天我遇到了一个巨大的错误,它不允许我 运行 我的 phone 上的示例项目。

当Android Studio 构建项目时,它首先将以下目标显示为UP-TO-DATE

....
:demoproject:processDebugResources UP-TO-DATE
:demoproject:generateDebugSources UP-TO-DATE
:demoproject:compileDebugJava UP-TO-DATE
:demoproject:proguardDebug UP-TO-DATE
....

在构建过程中有几十个这样的 UP-TO-DATE 日志语句。但是,它们总是停在 :demoproject:dexDebug。对于 dexDebug,我似乎从来没有得到 UP-TO-DATE 日志语句。

相反,dexDebug 后跟这个错误:

:demoproject:dexDebug
warning: Ignoring InnerClasses attribute for an anonymous inner class
(com.xyz.corp.sample.project.demo.a) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.

现在有几十个这样的 Ignoring InnerClasses attribute 错误。它们甚至出现在 v4 支持库中 类,这真是令人费解。

最后,这些错误以一条新语句结束:

UNEXPECTED TOP-LEVEL EXCEPTION:
at com.android.dx.command.dexer.Main.access0(Main.java:83)
at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:280)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:602)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.processOne(Main.java:632)
at com.android.dx.command.Main.main(Main.java:106)
...while parsing com/xyz/corp/sdk/AbcSDKConfig.class
1 error; aborting
Error:Execution failed for task ':demoproject:dexDebug'.
> com.android.ide.common.process.ProcessException:    org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_40\bin\java.exe'' finished with non-zero exit value 1
com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:510)
...while parsing com/xyz/corp/sdk/AbcSDKConfig.class
1 error; aborting
at com.android.dx.command.dexer.Main.processClass(Main.java:704)
at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)

我已经查阅了以下链接:

1. .

2. What is the “Ignoring InnerClasses attribute” warning output during compilation?.

我不确定它们是否适用于我的情况。我什至无法 运行 该项目。我已将 IDE 更新为 SDK Tools 22.0.1 并修改了 build.gradle 文件中的 buildToolsVersion 标记,但无济于事。有人可以指导我如何处理这个错误吗?我们将不胜感激。

哦,这是我的 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    defaultConfig {
        applicationId "com.xyz.corp.demo.project"
        minSdkVersion 10
        targetSdkVersion 22

        versionCode 2060200
        versionName '2.6.02.00'
    }

    buildTypes {
        debug {

            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def file = output.outputFile
                    output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
                }
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.0'

    compile project(':xyzSDK')
}

帮助!!!

因为它正在报告:bad class file magic (cafebabe) or version (0034.0000) 您应该检查您是否使用与您在运行时尝试使用的相同 java 版本进行编译。 如果您使用 gradle,则应在 build.gradle 文件中使用以下或类似条目:

apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7

使用此 link 了解更多 gradle 详细信息。

在 Android Studio 中,从 File -> Project Structure -> SDK Location -> JDK Location 开始,您应该使用 jdk 1.7 而不是依赖 Project level 设置。有关类似的 Android Studio 问题,请参阅此 link

您的 libs 文件夹和库项目 xyzSDK 似乎包含相同的库,这在转换为 dex

您可以尝试识别此类库并将其从一个地方删除。 或者像这样排除它

compile project(':xyzSDK'){
      exclude module: 'support-v4'
    }

我以 support-v4 为例,因为我之前遇到过类似的问题。

UNEXPECTED TOP-LEVEL EXCEPTION 表示您被包含两次 .jar.

正如@Akhil 所说,您应该排除 support-v4 因为我猜这是主要问题,但如果 @Akhil 的回答没有帮助您尝试此方法。

compile (':xyzSDK') {
    exclude group: 'com.android.support', module: 'support-v4'
}

你试过删除这一行吗?

compile 'com.android.support:appcompat-v7:22.1.0'

也许:xyzSDK已经有了appcompat-v7:22+..

我给你good answer by @CommonsWare做一个gradle报告看看是什么问题

我正在努力寻找您的答案,所以如果我发现一些有趣的东西,我会更新我的答案。

完成此操作后,您必须 Build -> Rebuild project

正如我在@dan 的回答中看到的那样...我给你一个可能的 answer

而不是丹所说的 1.7 尝试:

compileOptions {

    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

否则你可以手动查看File > Project StructureCtrl+Alt+Shift+S 然后在 JDK location 上查看您使用的是什么,如果您使用的是 1.8,请更改您的 1.7 SDK 路径

顺便说一句,我想我已经读到你问过你是否可以使用 JAVA 8 代替 Android 而不是 JAVA 7 如果我没记错的话......是的,你可以使用 JDK 8,但前提是你也像 question..

上所说的那样使用 gradle-retrolamba