执行任务“:app:kaptDebugKotlin”失败。清理构建时出错。

4

我遇到了一个问题,就是在使用cmd构建项目时出错。当我用Android Studio构建项目时没有问题,但是当我清理项目并用cmd构建时出现错误。

这个命令:

gradlew assembleDebug

出现了以下错误:

FAILURE:构建失败,发生异常。

*出错原因:

执行任务“:app:kaptDebugKotlin”时失败。

内部编译器错误,请查看日志以获取更多详细信息。

e: java.util.ServiceConfigurationError: javax.annotation.processing.Processor: android.databinding.annotationprocessor.ProcessDataBinding 无法 获取公共的无参构造函数 at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:581) at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:672) at java.base/java.util.ServiceLoader.access$1000(ServiceLoader.java:390) at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1232) at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1264) ...

我的应用gradle文件:

    apply plugin: 'com.android.application'

    apply plugin: 'kotlin-android'

    apply plugin: 'kotlin-android-extensions'

    apply plugin: 'kotlin-kapt'

    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.example.robot.mvvm"
            minSdkVersion 21
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        dataBinding {
            enabled = true
        }
        kapt {
            useBuildCache = true
        }
    }

    dependencies {
        def lifecycle_version = "2.0.0"
        def retrofit_version = '2.4.0'
        def okhttp_version = '3.11.0'
        def nav_version = "1.0.0-alpha06"

        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        testImplementation 'junit:junit:4.12'

        //android KTX
        implementation 'androidx.core:core-ktx:1.0.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        implementation 'androidx.appcompat:appcompat:1.0.0'
        implementation 'com.google.android.material:material:1.0.0'
        androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

        implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
        implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
        implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"

        //retrofit2
        kapt "com.squareup.retrofit2:retrofit:$retrofit_version"
        kapt "com.squareup.retrofit2:converter-gson:$retrofit_version"
        kapt "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version"

        //okhttp interceptor
        kapt "com.squareup.okhttp3:logging-interceptor:$okhttp_version"

        //Firebase
        implementation 'com.google.firebase:firebase-core:16.0.4'
        implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5'
        implementation 'com.google.firebase:firebase-messaging:17.3.3'

        implementation "android.arch.navigation:navigation-fragment:$nav_version"
        implementation "android.arch.navigation:navigation-ui:$nav_version"

        // optional - Test helpers
        androidTestImplementation "android.arch.navigation:navigation-testing:$nav_version"


        implementation 'androidx.multidex:multidex:2.0.0'
    }

我的项目gradle配置:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.1.0' // google-services plugin

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

请帮助我。谢谢。

2个回答

7

虽然被接受的答案没有提供很多信息,但是它确实指引了我正确的方向。我的测试在Android Studio中通过了,但是在命令行中没有通过,因为Android Studio使用的是捆绑的JDK,路径为/ Applications / Android Studio.app / Contents / jre / jdk / Contents / Home 。我通过编辑gradle.properties文件添加以下行配置gradlew来使用相同的JDK:

org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home

现在我的测试在Android Studio和命令行中都能通过。

谢谢你,你救了我的一天!我现在怎么给你买咖啡呢? - Pedro Paulo Amorim
1
谢谢,直到看到你的解决方案我才能够应对这个问题,你真是太棒了! - nuhkoca

4
我找到了解决方法
使用 Java SE 8。

谢谢,兄弟。这救了我的一天。虽然我已经浪费了一天的时间。 - Bright Lee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接