Android Studio UNEXPECTED TOP-LEVEL EXCEPTION:

强颜欢笑 提交于 2019-12-19 09:41:42

问题


Today I am faced with a massive error that doesn't let me run a sample project on my phone.

When Android Studio is building the project, it first shows the following targets as UP-TO-DATE:

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

There are dozens of these UP-TO-DATE log statements during the build process. However, they always halt at :demoproject:dexDebug. For dexDebug, I never seem to get an UP-TO-DATE log statement.

Instead, dexDebug is followed by this error:

: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.

Now there are dozens of these Ignoring InnerClasses attribute errors. They even occur for classes in the v4 support library, which is truly perplexing.

Finally, these errors end with a new statement:

UNEXPECTED TOP-LEVEL EXCEPTION:
at com.android.dx.command.dexer.Main.access$300(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$1.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)

I have already consulted the following links:

1. Gradle finished with non-zero exit value 1.

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

I'm not sure they apply to my situation. I'm not even able to run the project. I've updated my IDE to SDK Tools 22.0.1 and modified the buildToolsVersion tag in my build.gradle file, but to no avail. Can someone please guide me how to deal with this error ? All help will be appreciated.

Oh, and here is my 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')
}

Please HELP !!!


回答1:


Because it is reporting: bad class file magic (cafebabe) or version (0034.0000) you should check if you are compiling with the same java version that you are trying to use at runtime. If you are using gradle you should use the following or similar entries in your build.gradle file:

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

Use this link for more gradle details.

In Android Studio, from File -> Project Structure -> SDK Location -> JDK Location you should use the jdk 1.7 and not relay on the Project level setting. See this link for a similar Android Studio question.




回答2:


UNEXPECTED TOP-LEVEL EXCEPTION means that you are getting included twice any .jar.

As @Akhil said you should exclude support-v4 because I guess it's the main problem, but if @Akhil answer didn't help you try it out this method.

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

Have you tried to delete this line?

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

Maybe :xyzSDK allready has appcompat-v7:22+..

I give you there a good answer by @CommonsWare to make a gradle report to see what's the problem.

I'm working for your answer, so if I find something interessant I'll be updating my answer.

Once you've done this you must Build -> Rebuild project

Also as I saw on @dan answer... I let you there a possible answer.

Instead of 1.7 as dan said try to :

compileOptions {

    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

Otherwise you can check it manually going to File > Project Structure or Ctrl+Alt+Shift+S and then on the JDK location see what are you using and if you are using 1.8 change to your 1.7 SDK path

And btw I think that I've read that you have asked if you could use JAVA 8 for Android instead of JAVA 7 if i'm not wrong.... and yes you could use JDK 8, but only if you also use gradle-retrolamba like says on this question..




回答3:


It seems your libs folder & library project xyzSDK contains same library, which is causing issues while converting to dex

You can try to identify such library and remove it from one place. Or excluding it like this

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

I have taken example of support-v4, as i have faced similar issues due to this one previously.



来源:https://stackoverflow.com/questions/30979355/android-studio-unexpected-top-level-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!