Android Studio 在第二次构建后显示 Kotlin 依赖警告

标签 android android-studio android-gradle-plugin kotlin

我刚刚在我的 Android 项目中启用了 Kotlin,我偶然发现了一个警告。在第二次构建(构建 -> 重建项目)之后,此警告显示在消息下方:

Warning:Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.2.10/85fe1811f3e586d0cc53aba1394d8089f1862215/kotlin-stdlib-jdk8-1.2.10.jar (version 1.2)
~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-reflect/1.2.10/19bc012f8c4cd6b705bd6512263777cc19bcf259/kotlin-reflect-1.2.10.jar (version 1.2)
/Applications/Android Studio.app/Contents/gradle/m2repository/org/jetbrains/kotlin/kotlin-stdlib-jre7/1.1.51/kotlin-stdlib-jre7-1.1.51.jar (version 1.1)
~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.2.10/cfe8b616b3bf0811ef70863c86b745a2e767a66e/kotlin-stdlib-jdk7-1.2.10.jar (version 1.2)
~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.2.10/b9bf650516989595a5390e5a54181e16347208ac/kotlin-stdlib-1.2.10.jar (version 1.2)

似乎第二个版本包含缓存中过时的 kotlin-stdlib-jre7-1.1.51.jar。在干净的构建(构建 -> 清洁项目)之后,警告消失了,下一个重建项目会再次出现。

我使用的是 Android Studio 3.0.1,并且我明确地将 Kotlin 依赖项包含在版本中:

build.gradle

buildscript {
    ext {
        // shared build properties
        kotlin_version      = '1.2.10'
        buildToolsVersion   = '27.0.2'
        minSdkVersion       = 15
        targetSdkVersion    = 27
        compileSdkVersion   = 27
    }
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.0.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
    }
}

app/build.gradle

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

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlin_version}"
    implementation "org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}"
    [...]
}

也在我们的travis builds上显示警告。因此,这不仅是我的本地设置的问题。即使这只是一个警告,我也不愿意发布包含冲突版本的 apk。

最佳答案

总结

  1. 您已经修复了它,更新了您的依赖项,但我会在这里回答以链接相关问题。

  2. 您正确配置了项目依赖项并将 Kotlin 插件更新到 1.2。

  3. 这通常就足够了,例如 here但在 Kotlin 1.2 中 stdlib-jre 依赖项已更改为 jdk。

  4. 但是你的项目依赖于 Realm 4.3.1 版本,他们在 4.3.2 版本中修复了这个问题。

  5. 您可以通过命令或 AS 3.1 中的新构建选项卡找到导致问题的依赖项。


1。添加显式依赖和更新依赖

Kotlin 标准库的扩展版本在 documentation 中进行了解释。和 this answer.

If you're targeting JDK 7 or JDK 8, you can use extended versions of the Kotlin standard library which contain additional extension functions for APIs added in new JDK versions.

Instead of kotlin-stdlib, use one of the following dependencies:

compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

In Kotlin 1.1.x, use kotlin-stdlib-jre7 and kotlin-stdlib-jre8 instead.

如果自动解析无法正常工作,请更新其他依赖项(参见第 5 点)。


2。配置 Gradle 和 Kotlin 插件更新

修改您的 Gradle 配置。为了to build an Android project written in Kotlin :

  • 设置 kotlin-android gradle 插件并将其应用到您的项目中。
  • 添加 kotlin-stdlib 依赖项。

Those actions may also be performed automatically in IntelliJ IDEA / AS by invoking the action:

Tools | Kotlin | Configure Kotlin in Project

enter image description here

Check for Kotlin plugin updates:

Tools | Kotlin | Configure Kotlin plugin updates

enter image description here


3。 kotlin-stdlib-jre7 在 1.2.x 版本中重命名为 kotlin-stdlib-jdk7

Sirrah commented on 27 Dec 2017 here:

The Kotlin stdlib was renamed during the 1.2.x release. See here.

The old name kotlin-stdlib-jre7 was changed to kotlin-stdlib-jdk7.

This library is referenced in realm-library and kotlin-extensions.


4。 Realm 在 4.3.2 版本中更新到 Kotlin 1.2

Update Gradle Wrapper to 4.4.1 and Update to Kotlin 1.2 (#5642)

  • Update Gradle Wrapper and Update to Kotlin 1.2

  • kotlin-stdlib-jre7 -> kotlin-stdlib-jdk7

enter image description here


5。从 AS 3.1 开始通过命令或构建选项卡查找冲突的依赖项

./gradlew -q dependencies app:dependencies --configuration variantDebugCompileClasspath

enter image description here

从 Android Studio 3.1 Beta 1 开始,您可以使用新的构建选项卡来查找冲突的依赖项:

enter image description here

enter image description here

enter image description here

在这种情况下,您删除了警告并修复了将 Realm 版本更新到 4.3.2 的问题:

enter image description here

enter image description here

考虑到下载的顺序依赖项,也可以在 Travis-ci 构建中检查它:

enter image description here

关于Android Studio 在第二次构建后显示 Kotlin 依赖警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47833237/

相关文章:

android - 我无法理解如何实现 "Result Callback"。有人能指出我正确的方向吗?

Android:停止重新创建方向更改 Activity

java - 处理空 ArrayList 的 Android BaseAdapter

android - 将构建配置值添加到 list 元数据

java - 从匿名内部类中突破方法

java - ANDROID - 如何在 android studio 中将数据库 mysql php 的值显示到 TextView 中

Android studio应用程序与mysql的连接

android - 警告:配置 'compile'已过时,已被 'implementation'和 'api'取代

android - gradle 和 gradle-experimental 有什么区别?

android - 错误:未知主机 'jcenter.bintray.com: Name or service not known'。您可能需要在Gradle中调整代理设置