警告:API 'variant.getPackageLibrary()' 已过时并已替换为 'variant.getPackageLibraryProvider()'

WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'

我刚刚将 kotlin 更新到 1.3.30,现在在同步时出现此错误 gradle:

WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance. To determine what is calling variant.getPackageLibrary(), use -Pandroid.debug.obsoleteApi=true on the command line to display a stack trace. Affected Modules: hydatabase

这是我的 build.gradle:

apply plugin: 'com.squareup.sqldelight'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 19
    }
    lintOptions {
        abortOnError false
    }
}

sqldelight {
    Database {
        packageName = "com.company.hydatabase"
    }
}

kotlin {
    targets {
        fromPreset(presets.jvm, 'jvm')
        fromPreset(presets.android, 'android')
    }

    sourceSets {
        commonMain.dependencies {
            api 'org.jetbrains.kotlin:kotlin-stdlib-common'
        }
        jvmMain.dependencies {
            api 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
            // ICU4J: Use DecimalFormat
            // Get rid of this when minSDKLevel = API 24 - Nougat (7.0)
            // https://developer.android.com/guide/topics/resources/internationalization.html
            api 'com.ibm.icu:icu4j:60.2'
        }
        androidMain.dependencies {
            implementation 'org.jetbrains.kotlin:kotlin-stdlib'
            api "com.squareup.sqldelight:android-driver:1.1.1"
        }
        androidMain.dependsOn jvmMain
    }
}

task copyDatabase(type: Copy) {
    from "${rootProject.file('hyappcommon/Databases/').path}"
    into "${rootProject.file('hydatabase/src/main/assets/databases/').path}"
    include '**/*.sqlite'
}

preBuild.dependsOn(copyDatabase)

// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
    compileClasspath
}

这可能是一个错误,很快就会修复

您可以恢复到以前的版本或将此行添加到 gradle.properties

android.debug.obsoleteApi=true

调试的话显示 REASON: The Kotlin plugin is currently calling this API. We are working to solve this.

要查看此错误,请运行 ./gradlew -Pandroid.debug.obsoleteApi=true --stacktrace

我将 Kotlin 更新为 1.3.30 后,以下依赖项导致错误:

dependencies {
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    // ... other dependencies
}

我已在此处报告此问题:

https://github.com/bintray/gradle-bintray-plugin/issues/284

https://github.com/dcendents/android-maven-gradle-plugin/issues/81

顺便说一下,您可以忽略该错误消息。

正如 tommyboy 所说,Kotlin 插件称此已弃用 API。 如果您不想在 Kotlin 处理此问题时收到此警告,您可以使用以前版本的 Kotlin 插件,例如:

dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21"
}

这是 Kotlin 插件的问题,如 mentioned here。它会在以后的版本中得到修复。

你可以在根项目中运行这个命令

gradlew -Pandroid.debug.obsoleteApi=true

警告将消失。

刚刚更新到 "v1.3.40-release-Studio3.4-1" 插件。

正如您在 https://youtrack.jetbrains.com/issue/KT-30784

中看到的那样

我在使用带库插件的kotlin插件时遇到了这个问题。我发现如果将 kotlin 插件与应用程序插件一起使用,效果会很好。但是如果你使用kotlin plugin with library plugin,就会出现这个问题。所以这意味着:

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

// error:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

如错误所示,您可以使用./gradlew -Pandroid.debug.obsoleteApi=true --stacktrace来找出导致此问题的模块。

然后我发现我的一个模块使用了上面错误的插件组合。这似乎是 kotlin 插件的一个错误。所以最后,我升级了 kotlin 插件,然后它运行良好。下面是我最终使用的kotlin插件:

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

在我的项目 gradle 文件中我有

buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        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
    }
}

简单的改变 ext.kotlin_version = '1.3.31'ext.kotlin_version = '1.3.41' 解决了问题

在使用 1.3.31 版本时我曾尝试过 gradlew -Pandroid.debug.obsoleteApi=true

提到了

WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'. It will be removed at the end of 2019. For more information, see https://d.android.com/r/tools/task-configuration-avoidance.

REASON: The Kotlin plugin is currently calling this API. We are working to solve this.

WARNING: Debugging obsolete API calls can take time during configuration. It's recommended to not keep it on at all times.

1.3.41好像解决了

问题 tracked here 并且已 修复。

只需使用 Kotlin Gradle 插件 v 1.3.40 或更高版本。

WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'. It will be removed at the end of 2019.