【问题标题】:Adding Kotlin to existing Java project breaks Android Studio gradle messages errors将 Kotlin 添加到现有 Java 项目会破坏 Android Studio gradle 消息错误
【发布时间】:2017-09-06 05:00:16
【问题描述】:

我对 Gradle 和 Android 非常陌生,但由于我已将 Kotlin 添加到我的项目中,因此我随时在 Android Studio 中遇到错误,我需要通过 Gradle 控制台,它没有给我任何好的堆栈跟踪或路径默认。在 Gradle 消息视图中,现在确实出现了错误,我只得到

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

这是预期的功能吗,因为它在 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'
}

【问题讨论】:

    标签: java android android-studio gradle kotlin


    【解决方案1】:

    我没有看到任何问题,可能你可以在这里看到 [Gradle Console] ,它打印所有 bulid 日志。你可以看到错误详细信息,并发布错误消息。

    那是旧配置,也许你可以像这样配置新的:

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

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

    3.change version to 1.1.2-2,gradle:2.3.3的1.1.4-2版本有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'
    }
    

    Kotlin official doc

    【讨论】:

      猜你喜欢
      • 2013-11-12
      • 2018-09-27
      • 2020-10-04
      • 2015-08-02
      • 1970-01-01
      • 2017-09-24
      • 2011-01-03
      • 2019-10-11
      • 2015-06-19
      相关资源
      最近更新 更多