Kapt3:java.io.File无法强制转换为java.lang.String

时间:2017-03-21 19:15:48

标签: android gradle kotlin kapt

使用apply plugin: 'kotlin-kapt'并运行Gradle构建我的应用时会出现异常:

Caused by: java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String
        at org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin.disableAnnotationProcessingInJavaTask(Kapt3KotlinGradleSubplugin.kt:293)
        at org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin.buildOptions(Kapt3KotlinGradleSubplugin.kt:156)
        at org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin.createKaptKotlinTask(Kapt3KotlinGradleSubplugin.kt:246)
        at org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin.apply(Kapt3KotlinGradleSubplugin.kt:136)
        at org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin.apply(Kapt3KotlinGradleSubplugin.kt:48)
        at org.jetbrains.kotlin.gradle.plugin.SubpluginEnvironment.addSubpluginOptions(KotlinPlugin.kt:685)
        at org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPlugin.processVariantData(KotlinPlugin.kt:452)
        at org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPlugin.access$processVariantData(KotlinPlugin.kt:326)
        at org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPlugin$apply$2.execute(KotlinPlugin.kt:370)
        at org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPlugin$apply$2.execute(KotlinPlugin.kt:326)
        at org.gradle.internal.event.BroadcastDispatch$ActionInvocationHandler.dispatch(BroadcastDispatch.java:93)
        at org.gradle.internal.event.BroadcastDispatch$ActionInvocationHandler.dispatch(BroadcastDispatch.java:82)
        at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:44)
        at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:79)
        at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:30)
        at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
        at com.sun.proxy.$Proxy15.afterEvaluate(Unknown Source)
        at org.gradle.configuration.project.LifecycleProjectEvaluator.notifyAfterEvaluate(LifecycleProjectEvaluator.java:82)
        ... 56 more

此异常发生在配置阶段:

org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app'.

更多信息:

  • 使用Android Gradle Plugin 2.2.0(试过2.3.0,2。+)
  • 使用Kotlin 1.1.1(试过1.0.4 - 1.0.5-3)
  • 使用Dagger 2.2(试过2.7)
  • 使用Realm 1.0(也尝试了最新版本)
  • 使用Android数据绑定2.2.0
  • 使用Retrolambda
  • 不使用Android-apt

Root build.gradle

buildscript {
    ext.kotlinVersion = '1.1.1'
    ext.supportLibraryVersion = '23.2.1'
    ext.androidPluginVersion = '2.2.0'
    ext.testSupportLibraryVersion = '0.5'
    ext.newRelicAgentLibraryVersion = '5.11.0'
    ext.spekLibraryVersion = '1.0.89'
    ext.okhttpVersion = '3.6.0'
    ext.packetZoomVersion = '3.2.25'
    ext.mockitoLibraryVersion = '1.10.19'
    ext.powermockLibraryVersion = '1.6.2'

    repositories {
        jcenter()
        mavenCentral()
        maven {
            url 'https://maven.fabric.io/public'
        }
    }

    dependencies {
        classpath "com.android.tools.build:gradle:$androidPluginVersion"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath 'me.tatarka:gradle-retrolambda:3.3.0'
        classpath 'io.fabric.tools:gradle:1.21.6'
        classpath "io.realm:realm-gradle-plugin:1.0.0"
        classpath "com.newrelic.agent.android:agent-gradle-plugin:$newRelicAgentLibraryVersion"
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
        maven {
            url 'https://maven.fabric.io/public'
        }
        maven {
            url "https://s3.amazonaws.com/repo.commonsware.com"
        }
        maven {
            url "http://repository.jetbrains.com/all"
        }
        mavenLocal()
    }
}
模块build.gradle

app

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'io.fabric'
apply plugin: 'newrelic'

android {
    compileSdkVersion 23
    buildToolsVersion "25.0.2"

    dataBinding {
        enabled = true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        config
    }

    defaultConfig {
        applicationId "com.example.android"
        targetSdkVersion 23
        signingConfig signingConfigs.config
        multiDexEnabled true

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    productFlavors {
        dev {
            // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
            // to pre-dex each module and produce an APK that can be tested on
            // Android Lollipop without time consuming dex merging processes.
            minSdkVersion 21
        }
        prod {
            // The actual minSdkVersion for the application.
            minSdkVersion 16
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFile 'proguard-support.pro'
            proguardFile 'proguard-retrofit.pro'
            proguardFile 'proguard-butterknife.pro'
            proguardFile 'proguard-retrolambda.pro'
            proguardFile 'proguard-crashlytics.pro'
            proguardFile 'proguard-newrelic.pro'
            proguardFile 'proguard-duktape.pro'
            proguardFile 'proguard-packetzoom.pro'
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    retrolambda {
        jvmArgs '-noverify'
    }

    if (project.hasProperty('releaseKeyStore') &&
            project.hasProperty('releaseKeyPassword') &&
            project.hasProperty('releaseKeyStorePassword') &&
            project.hasProperty('releaseKeyAlias')) {
        android.signingConfigs.config.keyAlias = releaseKeyAlias
        android.signingConfigs.config.storeFile = file(releaseKeyStore)
        android.signingConfigs.config.storePassword = releaseKeyStorePassword
        android.signingConfigs.config.keyPassword = releaseKeyPassword
    } else {
        android.signingConfigs.config.keyAlias ""
        android.signingConfigs.config.storeFile null
        android.signingConfigs.config.storePassword ""
        android.signingConfigs.config.keyPassword ""
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
        test.java.srcDirs += 'src/test/kotlin'
        androidTest.java.srcDirs += 'src/androidTest/kotlin'
    }
}

kapt {
    generateStubs = true
}

dependencies {
    kapt 'com.github.hotchemi:permissionsdispatcher-processor:2.0.8'
    kapt 'com.google.dagger:dagger-compiler:2.2'
    kapt "com.android.databinding:compiler:$androidPluginVersion"
    kapt "io.realm:realm-annotations:1.0.0"
    kapt "io.realm:realm-annotations-processor:1.0.0"
    kapt 'com.jakewharton:butterknife:6.1.0'
    kapt 'com.github.lukaspili.autodagger2:autodagger2-compiler:1.1'
    provided 'org.glassfish:javax.annotation:10.0-b28'
    compile 'com.android.support:multidex:1.0.1'
    compile "com.android.support:appcompat-v7:$supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
    compile "com.android.support:design:$supportLibraryVersion"
    compile "com.android.support:percent:$supportLibraryVersion"
    compile "com.android.support:gridlayout-v7:$supportLibraryVersion"
    compile "com.android.support:cardview-v7:$supportLibraryVersion"
    compile "com.android.support:customtabs:$supportLibraryVersion"
    compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
    compile 'io.reactivex:rxandroid:1.2.0'
    compile 'io.reactivex:rxjava:1.1.4'
    compile 'com.jakewharton.rxbinding:rxbinding:0.1.0'
    compile 'com.trello:rxlifecycle:0.5.0'
    compile 'com.trello:rxlifecycle-components:0.5.0'
    compile 'com.trello:rxlifecycle-kotlin:0.5.0'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.google.dagger:dagger:2.2'
    compile 'com.jakewharton:butterknife:6.1.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'me.relex:circleindicator:1.1.3@aar'
    compile 'com.jakewharton.timber:timber:3.0.2'
    compile 'com.cocosw:bottomsheet:1.+@aar'
    compile "com.newrelic.agent.android:android-agent:$newRelicAgentLibraryVersion"
    compile "com.newrelic.agent.android:class-rewriter:$newRelicAgentLibraryVersion"
    compile 'me.tatarka:bindingcollectionadapter:0.15'
    compile 'me.tatarka:bindingcollectionadapter-recyclerview:0.15'
    compile 'com.github.pwittchen:reactivenetwork:0.1.3'
    compile 'org.apmem.tools:layouts:1.10@aar'
    compile 'com.sothree.slidinguppanel:library:3.2.1'
    compile 'com.squareup.duktape:duktape-android:0.9.5'
    compile 'com.commonsware.cwac:provider:0.4.1'
    compile "com.squareup.okhttp3:logging-interceptor:$okhttpVersion"
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    compile 'com.squareup.retrofit2:converter-scalars:2.0.2'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
    compile "com.packetzoom:pz-android-sdk:$packetZoomVersion"
    compile "com.packetzoom:pz-okhttp3-interceptor:$packetZoomVersion"
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    compile 'org.jetbrains.kotlinx:kotlinx-support-jdk7:0.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }
    compile 'com.github.lukaspili.autodagger2:autodagger2:1.1'
    compile 'com.google.firebase:firebase-messaging:9.4.0'
    compile 'com.google.android.gms:play-services-analytics:9.4.0'
    compile 'com.google.android.gms:play-services-gcm:9.4.0'
    compile 'com.squareup.flow:flow:1.0.0-alpha2-SNAPSHOT'

    testCompile 'junit:junit:4.12'
    testCompile 'pl.pragmatists:JUnitParams:1.0.4'
    testCompile "org.mockito:mockito-core:$mockitoLibraryVersion"
    testCompile "org.powermock:powermock-api-mockito:$powermockLibraryVersion"
    testCompile "org.powermock:powermock-module-junit4:$powermockLibraryVersion"
    testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    testCompile "org.jetbrains.spek:spek-api:$spekLibraryVersion"
    testCompile "org.jetbrains.spek:spek-junit-platform-engine:$spekLibraryVersion"
    testCompile 'org.junit.platform:junit-platform-runner:1.0.0-M2'
    testCompile "org.jetbrains.kotlin:kotlin-test:$kotlinVersion"
    testCompile 'com.natpryce:hamkrest:1.2.3.0'
    androidTestCompile "com.android.support.test:runner:$testSupportLibraryVersion"
    androidTestCompile "com.android.support.test:rules:$testSupportLibraryVersion"
    androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
    androidTestCompile "org.mockito:mockito-core:$mockitoLibraryVersion"
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.3'
    androidTestCompile 'org.hamcrest:hamcrest-core:1.3'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile "com.android.support:support-annotations:$supportLibraryVersion"
}

// Plugin declared at the bottom of the build.gradle file according to requirements described in documentation:
// https://developers.google.com/android/guides/google-services-plugin#introduction
apply plugin: 'com.google.gms.google-services'

如何解决问题?

1 个答案:

答案 0 :(得分:0)

事实证明我错误地应用了Realm插件,因此问题是插件应用程序的顺序。 Realm的插件应该在 Kotlin的之后应用

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

这解决了我的问题。