3

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

Community
  • 1
  • 1
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • can you able to restart your studio/machine, and rebuilt your project – Sree Jun 22 '15 at 11:58
  • What is your java version? 1.8? If so, roll back to 1.7 – Nizam Jun 22 '15 at 12:13
  • @NoName: I have described the log before the error: lots and lots of `UP-TO-DATE` statements. The `dexDebug` check doesn't get an `UP-TO-DATE` statement; it is instead followed by the above log. Is that enough for you ? And no, I haven't checked multidex issues yet ... – Yash Sampat Jun 22 '15 at 12:13
  • just noticed this in the log `com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)` bad class file magic ?? what does this mean – Yazan Jun 22 '15 at 12:14
  • please post your gradle dependencies.. – Akhil Jun 22 '15 at 12:18
  • @Akhil: please see my `build.gradle` in edited answer :) – Yash Sampat Jun 22 '15 at 12:35
  • Take a look at some of the answers `http://stackoverflow.com/questions/7866723/dx-bad-class-file-magic-cafebabe-or-version-0033-0000-with-adk14` – George Daramouskas Jun 28 '15 at 15:30
  • @Y.S. Did you find the solution for this question or shall I search more? My Answer didn't help you at all? – Skizo-ozᴉʞS ツ Aug 19 '15 at 11:26
  • @Skizo: I'm sorry to say I haven't had time to return to this problem, as there is a lot of other work. I'll return to this problem soon, and lets see if your answer helps ... :) – Yash Sampat Aug 19 '15 at 11:27
  • @Skizo: I'm currently working on [this](http://stackoverflow.com/q/32073420/3287204) ... any ideas ? – Yash Sampat Aug 19 '15 at 11:28
  • @Y.S. I've checked this question, I'm able in some hours and I'll take a look :) – Skizo-ozᴉʞS ツ Aug 19 '15 at 11:38

3 Answers3

2

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.

Community
  • 1
  • 1
dan
  • 13,132
  • 3
  • 38
  • 49
  • Interesting. Thank you for the answer. Let me try it out and get back to you ... :) – Yash Sampat Jun 22 '15 at 12:22
  • In the meantime, I have a question. I have to integrate this SDK into my main project which uses Java 1.8, and I would prefer that it remain that way. Is there no way I can compile this project with 1.8 ?? – Yash Sampat Jun 22 '15 at 12:24
  • You should be able to compile using 1.8 but the `targetCompatibility` should be set to 1.7. This way the binaries will be 1.7 compatible. – dan Jun 22 '15 at 12:25
  • so you mean in Project Structure I can point to JDK 1.8, and set `targetCompatibility` and `sourceCompatibility` to 1.7, and it will work ? – Yash Sampat Jun 22 '15 at 12:28
  • Yes, if the build is managed by gradle, using `targetCompatibility ` set to 1.7, it will produce 1.7 compatible binaries. – dan Jun 22 '15 at 12:30
  • Currently it is giving an error `Error:The 'java' plugin has been applied, but it is not compatible with the Android plugins.` Every time I try to sync this project with Gradle, it gives the same error :( – Yash Sampat Jun 22 '15 at 12:35
  • That means that you already have the 'java' plugin registered in you gradle.build file. But have you tried in Android Studio to use jdk1.7 or Android API 20, at least? – dan Jun 22 '15 at 12:38
  • I am going to try what you said first: to manually point the IDE to JDK 1.7. Let me see if I am at least able to run the program. and I will get back to you ... :) – Yash Sampat Jun 22 '15 at 12:44
1

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

Community
  • 1
  • 1
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • I am accepting this answer for now, but I am not yet done with it. I need to re-check this problem and do some more research ... :) – Yash Sampat Aug 30 '15 at 13:40
0

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.

Akhil
  • 6,667
  • 4
  • 31
  • 61
  • Thank you for the answer. This could indeed be the problem. Were you getting the exact same error as I am due to this ?? – Yash Sampat Jun 22 '15 at 12:57