我是Kotlin开发的新手,刚在我的项目中添加了Kotlin,就出现了一个过时的问题.

我得到的警告是:

API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'.
It will be removed at the end of 2019.

建筑格雷德尔:

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

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.sinamn75.androidtest"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}
repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    // Support
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0-rc02'
    implementation 'com.android.support:recyclerview-v7:28.0.0-rc02'
    implementation 'com.android.support:cardview-v7:28.0.0-rc02'
    implementation 'com.android.support:support-v4:28.0.0-rc02'
    implementation 'com.android.support:support-core-utils:28.0.0-rc02'
    implementation 'com.android.support:preference-v14:28.0.0-rc02'
    implementation 'com.android.support:exifinterface:28.0.0-rc02'
    // GooglePlay
    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    implementation 'com.google.android.gms:play-services-location:15.0.1'
    implementation 'com.google.android.gms:play-services-plus:15.0.1'
    implementation 'com.google.android.gms:play-services-places:15.0.1'
    implementation 'com.google.android.gms:play-services-vision:15.0.2'
    //AndPermission
    implementation 'com.yanzhenjie:permission:2.0.0-rc6'
    // AHNavigation
    implementation 'com.aurelhubert:ahbottomnavigation:2.1.0'
    // Lottie
    implementation 'com.airbnb.android:lottie:2.2.5'
    // SwitchButton
    implementation 'lib.kingja.switchbutton:switchbutton:1.1.7'
    // RoundedImageView
    implementation 'com.makeramen:roundedimageview:2.3.0'
    // Picasso
    implementation 'com.squareup.picasso:picasso:2.71828'
    // MaterialDialog
    implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
}

建筑格雷德尔:

buildscript {
    ext.kotlin_version = '1.2.70'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0-alpha11'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
apply plugin: 'kotlin'

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task customClean(type: Delete) {
    delete rootProject.buildDir
}
clean.dependsOn customClean
repositories {
    mavenCentral()
}
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

推荐答案

Update:这似乎是kotlin插件中的一个bug,它提到了in here

然而,使用新版本的kotlin插件(当kotlin开始使用新的API时)可能会消除以下错误:

https://github.com/JetBrains/kotlin/pull/1884/commits/1a17cb54a775ab3e55db66109cb12b7d54fbba6c

和:https://github.com/JetBrains/kotlin/pull/1884

提交实际上是为了解决此问题:

此promise does not change anything functionally,it is only to avoid the warning message,作为记者在 https://issuetracker.google.com/116198439


After a deep search into codes, seems like getJavaCompile() is obsolete in your current gradle (alpha11 version). However, you were using alpha version of gradle which I don't really recommend that.

相反,请try 使用以下稳定版本:

classpath 'com.android.tools.build:gradle:3.1.4'

然后,我希望警告应该消失了.

Kotlin相关问答推荐

Kotlin中的增广赋值语句中的难以理解的错误

在KMM合成多平台中创建特定于平台的视图

Kotlin-elvis算子don';不使用map.get()

Flow.state In() 未从其来源接收新值

Kotlin 可空泛型

为什么记得不将 StateFlow 转换为特定类型?

Mixin 在 Jackson 中添加 defaultImpl 不起作用

Kotlin 中 lambda 表达式中的默认参数

找不到 androidsdk.modules

零安全的好处

Kotlin 的 isNullOrBlank() 函数是否可以导入 xml 以用于数据绑定

如何在 Spring WebFlux 的响应正文中流式传输二进制数据

如何在使用 Gradle 的 AppEngine 项目中使用 Kotlin

在java代码中使用kotlin库

如何捕获传递给模拟函数的参数并返回它?

在 gradle android library kotlin 项目中禁用 META-INF/* 生成

如何在 Jetpack Compose 的 LazyColumn/LazyRow 中禁用和启用滚动?

用Gradle Kotlin DSL构建源jar?

在Kotlin中使用@Service时引发异常

具有泛型param的Kotlin抽象类和使用类型param的方法