UNEXPECTED TOP-LEVEL EXCEPTION adding new Module t

2019-02-19 10:09发布

Frustrating error after adding a simple Java Library (new Module in Android Studio). I've read many similar questions but cannot find any solution, anyone can help me?

Error:Execution failed for task ':KeepLinkMain:preDexDebug'.

com.android.ide.common.internal.LoggedErrorException: Failed to run command: C:\Android\android-sdk\build-tools\19.1.0\dx.bat --dex --output C:...\AndroidStudioProjects\KeepLink\KeepLinkMain\build\intermediates\pre-dexed\debug\KeepLinkLib-d6db361e6605649280566ee9a3143d04322844dc.jar C:...\AndroidStudioProjects\KeepLink\KeepLinkLib\build\libs\KeepLinkLib.jar Error Code: 1 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000) at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472) at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406) at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388) at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251) at com.android.dx.command.dexer.Main.processClass(Main.java:665) at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634) at com.android.dx.command.dexer.Main.access$600(Main.java:78) at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572) at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284) at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166) at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144) at com.android.dx.command.dexer.Main.processOne(Main.java:596) at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264) at com.android.dx.command.dexer.Main.run(Main.java:230) at com.android.dx.command.dexer.Main.main(Main.java:199) at com.android.dx.command.Main.main(Main.java:103) ...while parsing com/rmpt/keeplinklib/Constants.class 1 error; aborting

Main module build.gradle file

    apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 1
        versionName '1.0'
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            debuggable false
            jniDebugBuild false
        }
    }
    dexOptions {
        incremental true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    productFlavors {
    }
}

dependencies {
    compile project(':KeepLinkLib')
    compile files('lib/gson-2.2.4.jar')
}

Java Library build.gradle file

apply plugin: 'java'

2条回答
一纸荒年 Trace。
2楼-- · 2019-02-19 10:28

For the experimental gradle edition, that uses a different syntax, you have to write it this way:

apply plugin: 'com.android.model.application'
//apply plugin: 'java' //do not include this line
model {
    android {
        compileOptions.with {
            sourceCompatibility = JavaVersion.VERSION_1_7
            targetCompatibility = JavaVersion.VERSION_1_7
        }
        ... //rest of gradle code
    }
}
查看更多
成全新的幸福
3楼-- · 2019-02-19 10:49

I ran into a similar issue and was able to resolve it by adding the following lines to the Java library's build.gradle below the apply plugin: line:

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

I also had to remove the compileOptions block from within the main module's build.gradle

查看更多
登录 后发表回答