无法使用Android Studio 3.0 + DataBinding + Kotlin构建项目

38

我有一个使用数据绑定、Kotlin和Dagger的大型项目。我试图使用stackoverflow上的几个解决方案尝试构建它,但已经花费了几天时间,最终决定亲自询问。

我认为一些第三方库使用数据绑定,因为添加这行代码没有起到帮助作用。

kapt 'com.android.databinding:compiler:3.0.0'

Android Studio提示:

'androidProcessor'依赖项将不被识别为kapt注释处理器。 请将这些构件的配置名称更改为'kapt':'com.android.databinding:compiler:3.0.0'。

我尝试过但没有帮助的方法:

   kapt {
    generateStubs = true
}

在 local.properties 文件中:kotlin.incremental=false

另一个 Stack Overflow 的解决方案没有帮助:

kapt ('com.android.databinding:compiler:3.0.0'){
        force = true
    }

我的 build.gradle

 apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'blockcanaryex'
apply plugin: 'kotlin-kapt'
apply plugin: 'newrelic'
apply plugin: 'kotlin-android-extensions'

def props = new Properties()
file("newrelic.properties").withInputStream { props.load(it) }

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def stdout = new ByteArrayOutputStream()
            exec {
                commandLine "git", "symbolic-ref", "--short", "HEAD"
                standardOutput = stdout
            }
            def branch = stdout.toString().trim()
            branch = "branch.$branch"

            if (project.hasProperty("buildNumber")) {
                def buildNumber = project.property("buildNumber")
                output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + branch + "-" + versionName + "." + versionCode + "." + buildNumber + ".apk"))
            } else {
                output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + branch + "-" + versionName + "." + versionCode + ".apk"))
            }
        }
    }

    def version = "4.15"

    defaultConfig {
        buildConfigField "String", "TOKEN", "\"" + props.getProperty("token") + "\""
        applicationId "xxx"
        minSdkVersion 19
        targetSdkVersion 26
        versionName version

        versionCode 260

        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
    }



    signingConfigs {
        release {
            storeFile file("xxx")
            storePassword "xxx"
            keyAlias "xxx"
            keyPassword "xxx"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            signingConfig signingConfigs.release
        }

        debug {
            versionNameSuffix ".debug"
        }
    }
    flavorDimensions "prod","stag"

    productFlavors {



        production {
            dimension "prod"
        }

        staging {
            dimension "stag"
            applicationId "app.id"
            versionName(version + ".staging")
        }

    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude '.readme'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    dataBinding {
        enabled = true
    }
}


repositories {
    jcenter()
    maven { url "https://jitpack.io" }
    maven { url 'https://bitbucket.org/gryphteam/maven/raw/release' }
    maven {
        url 'https://maven.google.com/'
        name 'Google'
    }
}


def supportVersion = "26.1.0"
def playServicesVersion = "11.6.0"
def retrofitVersion = "2.3.0"

dependencies {


    implementation 'com.jakewharton:butterknife:6.1.0'
    implementation('de.keyboardsurfer.android.widget:crouton:1.8.5@aar') {
        exclude group: 'com.google.android', module: 'support-v4'
    }
    implementation 'com.google.dagger:dagger:2.10'
    kapt 'com.google.dagger:dagger-compiler:2.10'
    kapt 'com.android.databinding:compiler:3.0.0'


    implementation 'de.greenrobot:eventbus:2.4.0'
    implementation 'com.mcxiaoke.volley:library:1.0.19'

    implementation 'com.google.code.gson:gson:2.8.0'
    implementation "com.google.android.gms:play-services-places:$playServicesVersion"
    implementation "com.google.android.gms:play-services-plus:$playServicesVersion"
    implementation "com.google.android.gms:play-services-analytics:$playServicesVersion"
    implementation "com.google.android.gms:play-services-auth:$playServicesVersion"
    implementation "com.google.android.gms:play-services-gcm:$playServicesVersion"
    implementation "com.google.android.gms:play-services-ads:$playServicesVersion"

    implementation "com.android.support:appcompat-v7:$supportVersion"
    implementation "com.android.support:support-v13:$supportVersion"
    implementation "com.android.support:recyclerview-v7:$supportVersion"
    implementation "com.android.support:design:$supportVersion"
    implementation "com.android.support:support-annotations:$supportVersion"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:multidex:1.0.2'

    implementation 'commons-io:commons-io:2.4'
    implementation 'org.apache.commons:commons-lang3:3.1'
    implementation files('libs/comscore.jar')
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.2'
    implementation 'com.github.deano2390:MaterialShowcaseView:1.0.5'
    implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
    implementation 'com.google.ads.mediation:facebook:4.26.1.0'
    implementation 'com.facebook.android:audience-network-sdk:4.26.1'
    implementation 'org.jboss.netty:netty:3.2.10.Final'

    //Testing
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-core:1.10.19'
    androidTestImplementation 'com.android.support:multidex:1.0.2'
    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.1'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
    }
    androidTestImplementation('com.android.support.test:runner:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
    }
    androidTestImplementation('com.android.support.test:rules:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
    }
    androidTestImplementation('com.android.support.test.espresso:espresso-intents:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
    }
    androidTestImplementation('com.android.support.test.espresso:espresso-contrib:2.2') {
        exclude group: 'com.android.support', module: 'app-compat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
        exclude module: 'recyclerview-v7'

    }
    androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
    androidTestImplementation "com.android.support:support-annotations:$supportVersion"
    implementation project(':eventsource_android-release')
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
    debugImplementation 'com.letv.sarrsdesktop:BlockCanaryExJRT:0.9.5.3'
    releaseImplementation 'com.letv.sarrsdesktop:BlockCanaryExJRTNoOp:0.9.5.3'
    testImplementation 'com.letv.sarrsdesktop:BlockCanaryExJRTNoOp:0.9.5.3'


    implementation 'me.mvdw.recyclerviewmergeadapter:recyclerviewmergeadapter:2.0.0'
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'

    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"
    implementation 'io.reactivex.rxjava2:rxjava:2.1.2'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'com.github.ReactiveX:RxKotlin:2.1.0'
    implementation 'com.gojuno.koptional:koptional-rxjava2-extensions:1.2.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.newrelic.agent.android:android-agent:5.14.0'
    implementation 'org.jsoup:jsoup:1.10.3'
}

为了确保:您在build.gradle中是否应用了apply plugin: 'kotlin-kapt' - david.mihola
尝试使用数据绑定 { enabled = true } 我在我的项目中使用了所有的东西,如Dagger Realm,它运行良好。 - pankaj yadav
@SamiRajala,是的,我确定构建失败是因为那个原因。 - Dennis Zinkovski
谷歌示例生成了相同的警告。 - Andrzej Sawoniewicz
2
我在Google问题跟踪器中找到了两个与_annotationProcessor_警告有关的条目:Issue 77234253issue 65357518 - JJD
显示剩余5条评论
2个回答

16

我刚刚在一个新的、全新的 Kotlin 项目中使用了类似的 build.gradle 设置,并成功构建了该项目。不幸的是,我无法使用您的所有库和确切设置,但也许这只是一种顺序上的问题。下面是我的build.gradle文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

kapt {
    generateStubs = true
}

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "nice.fontaine.kottest"
        minSdkVersion 21
        targetSdkVersion 26
        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'
        }
    }
}

dependencies {
    implementation 'com.jakewharton:butterknife:6.1.0'
    annotationProcessor 'com.jakewharton:butterknife:6.1.0'
    implementation('de.keyboardsurfer.android.widget:crouton:1.8.5@aar') {
        exclude group: 'com.google.android', module: 'support-v4'
    }
    implementation 'com.google.dagger:dagger:2.10'
    kapt 'com.google.dagger:dagger-compiler:2.10'
    kapt 'com.android.databinding:compiler:3.0.0'

    implementation 'de.greenrobot:eventbus:2.4.0'
    implementation 'com.mcxiaoke.volley:library:1.0.19'

    implementation 'com.google.code.gson:gson:2.8.0'
    implementation "com.google.android.gms:play-services-places:11.6.0"
    implementation "com.google.android.gms:play-services-plus:11.6.0"
    implementation "com.google.android.gms:play-services-analytics:11.6.0"
    implementation "com.google.android.gms:play-services-auth:11.6.0"
    implementation "com.google.android.gms:play-services-gcm:11.6.0"
    implementation "com.google.android.gms:play-services-ads:11.6.0"

    implementation "com.android.support:appcompat-v7:26.1.0"
    implementation "com.android.support:support-v13:26.1.0"
    implementation "com.android.support:recyclerview-v7:26.1.0"
    implementation "com.android.support:design:26.1.0"
    implementation "com.android.support:support-annotations:26.1.0"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:multidex:1.0.2'

    implementation 'commons-io:commons-io:2.4'
    implementation 'org.apache.commons:commons-lang3:3.1'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
    implementation 'com.google.ads.mediation:facebook:4.26.1.0'
    implementation 'com.facebook.android:audience-network-sdk:4.26.1'
    implementation 'org.jboss.netty:netty:3.2.10.Final'

    //Testing
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-core:1.10.19'
    androidTestImplementation 'com.android.support:multidex:1.0.2'
    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.1'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
    }
    androidTestImplementation('com.android.support.test:runner:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
    }
    androidTestImplementation('com.android.support.test:rules:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
    }
    androidTestImplementation('com.android.support.test.espresso:espresso-intents:2.2') {
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
    }
    androidTestImplementation('com.android.support.test.espresso:espresso-contrib:2.2') {
        exclude group: 'com.android.support', module: 'app-compat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'support-annotations:22.2.1'
        exclude module: 'recyclerview-v7'

    }
    androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
    androidTestImplementation "com.android.support:support-annotations:26.1.0"
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
    debugImplementation 'com.letv.sarrsdesktop:BlockCanaryExJRT:0.9.5.3'
    releaseImplementation 'com.letv.sarrsdesktop:BlockCanaryExJRTNoOp:0.9.5.3'
    testImplementation 'com.letv.sarrsdesktop:BlockCanaryExJRTNoOp:0.9.5.3'

    implementation 'me.mvdw.recyclerviewmergeadapter:recyclerviewmergeadapter:2.0.0'
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'

    implementation "com.squareup.retrofit2:retrofit:2.3.0"
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
    implementation "com.squareup.retrofit2:converter-gson:2.3.0"
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
    implementation 'io.reactivex.rxjava2:rxjava:2.1.2'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'com.gojuno.koptional:koptional-rxjava2-extensions:1.2.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.newrelic.agent.android:android-agent:5.14.0'
    implementation 'org.jsoup:jsoup:1.10.3'
}

至少我可以通过这个测试来验证

kapt 'com.android.databinding:compiler:3.0.0' 

对我来说这个方法可行。也许你可以在新项目中尝试一下,检查它是否有效,然后逐步应用其他库。希望这可以让你走上正轨!


我不确定为什么它有帮助,因为我尝试了相同的配置,但现在我编写了插件和数据绑定编译器的“3.0.1”版本。也许他们在新插件中修复了一些问题,无论如何,谢谢,它可以工作了。 - Dennis Zinkovski
4
问题出在 dataBinding {enabled = true},所以如果你没有使用它,那么肯定不会有任何问题。但是使用数据绑定是必须的。 - user2058839

-8

只需移除 Kotlin 库。Android Studio 3.0 默认支持它。


你是指这一行代码 implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 吗? - Dennis Zinkovski
我已经尝试过了,没有帮助,构建失败并显示相同的消息。 - Dennis Zinkovski
没有这个库它就无法编译。这就像是说“要正确使用AppCompatActivity请移除AppCompat库”。 - Zoe stands with Ukraine

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