将Kotlin添加到现有Java项目会打破Android Studio gradle消息错误

我是Gradle和Android的新手,但是因为我已经把Kotlin添加到了我的项目中,所以我在Android Studio中遇到一个错误,我需要通过Gradle控制台,默认情况下不会给我任何好的堆栈跟踪或路径。 在现在出现错误的Gradle消息视图中,我只能得到

错误:执行任务’:app:kaptDebugKotlin’失败。 内部编译器错误。 查看日志了解更多详情

这是预期的function,因为它似乎在jetbrains示例项目中工作正常。

我的项目Gradle文件

buildscript { ext.kotlin_version = '1.1.4-2' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } 

我的应用程序级别的Gradle文件

 buildscript { ext.kotlin_version = '1.1.4-2' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" } } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.test.andrew.ccombo_breaker" minSdkVersion 19 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main.java.srcDirs += 'src/main/java' main.java.srcDirs += 'src/main/kotlin' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile 'com.google.dagger:dagger:2.9' kapt 'com.google.dagger:dagger-compiler:2.9' testCompile 'junit:junit:4.12' } 

我没有看到任何问题,可能是你可以在这里看到[ Gradle控制台 ],它打印所有bulid日志。你可以看到错误的细节,并张贴错误消息。

这是旧的配置,可能是你可以像这样配置新的:

1.删​​除所有关于Kotlin配置。

2.使用菜单 – >工具 – > Kotlin – >在项目中配置Kotlin – >使用Gradle的Android

3.将版本更改为1.1.2-2,版本1.1.4-2 for gradle:2.3.3有一些bug

4.同步gradle

其他。 新版本使用扩展只配置这个:

 apply plugin: 'kotlin-android-extensions' 

不要使用这个:

 dependencies { classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" } 

最终,像这样的配置

项目Gradle文件

 buildscript { ext.kotlin_version = '1.1.2-2' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } 

应用程序级别的Gradle文件

 apply plugin: 'kotlin-kapt' apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.test.andrew.ccombo_breaker" minSdkVersion 19 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } kapt { generateStubs = true } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" compile 'com.google.dagger:dagger:2.9' kapt 'com.google.dagger:dagger-compiler:2.9' testCompile 'junit:junit:4.12' } 

科特林官方文档