Android Studio 在第二次构建后显示 Kotlin 依赖警告 [英] Android Studio shows Kotlin Dependency Warning after Second Build

查看:93
本文介绍了Android Studio 在第二次构建后显示 Kotlin 依赖警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

警告:类路径中的运行时 JAR 文件应该具有相同的版本.这些文件是在类路径中找到的:~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.2.10/85fe1811f3e586d0cc53aba1394d8089f1862215/kotlin-stdlib-jdk8/1.2.10/85fe1811f3e586d0cc53aba1394d8089f1862215/kotlin-stdlib-1.2-jdversion)~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-reflect/1.2.10/19bc012f8c4cd6b705bd6512263777cc19bcf259/kotlin-reflect-1.2.10(2version.10)/Applications/Android Studio.app/Contents/gradle/m2repository/org/jetbrains/kotlin/kotlin-stdlib-jre7/1.1.51/kotlin-stdlib-jre7-1.1.51.jar(1.1版)〜/.gradle/高速缓存/模块-2/文件-2.1/org.jetbrains.kotlin/科特林-STDLIB-JDK7/1.2.10/cfe8b616b3bf0811ef70863c86b745a2e767a66e/科特林-STDLIB-jdk7-1.2.10.jar(版本1.2)~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.2.10/b9bf650516989595a5390e5a54181e16347208ac/kotlin-stdlib1.2.2.version

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

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

build.gradle

buildscript {分机{//共享构建属性kotlin_version = '1.2.10'构建工具版本 = '27.0.2'minSdkVersion = 15目标SDK版本= 27compileSdkVersion = 27}存储库{jcenter()谷歌()}依赖{类路径com.android.tools.build:gradle:3.0.1"类路径org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"}}

app/build.gradle

应用插件:'com.android.application'应用插件:'kotlin-android'应用插件:'kotlin-kapt'依赖{实现org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlin_version}"实现org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}"[...]}

也在我们的

<块引用>

检查 Kotlin 插件更新:

工具 |科特林 |配置 Kotlin 插件更新

<小时>

3.kotlin-stdlib-jre7 在 1.2.x 版本中更名为 kotlin-stdlib-jdk7

<块引用>

Sirrah 于 2017 年 12 月 27 日发表评论

<小时>

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

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

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

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

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

I've just enabled Kotlin in my Android project and I stumbled upon a warning. After the second build (Build -> Rebuild Project) this warning is shown under Messages:

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)

It seems like the second build includes the outdated kotlin-stdlib-jre7-1.1.51.jar from cache. After a clean build (Build -> Clean Project) the warning is gone, and the next Rebuild Project brings it up again.

I'm using Android Studio 3.0.1 and I explicitly include the Kotlin dependencies with version:

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}"
    [...]
}

Also on our travis builds the warning is shown. Thus, it is not only a problem with my local setup. Even if it is only a warning, I don't feel comfortable by releasing an apk that includes conflicting versions.

解决方案

Summary

  1. You already fixed it updating your dependencies but I'll answer here to link a related question.

  2. You configured correctly your project dependencies and updated the Kotlin plugin to 1.2.

  3. This is normally enough like here but stdlib-jre dependencies were changed to jdk in Kotlin 1.2.

  4. But your project was depending on Realm version 4.3.1, and they fixed this in version 4.3.2.

  5. You can find the dependency causing the issue via commands or the new build tab in AS 3.1.


1. Add explicit dependencies and update dependencies

Extended versions of the Kotlin standard library are explained in documentation and 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.

Update other dependencies, if the automatic resolution doesn't work properly (see point 5).


2. Configure Gradle and Kotlin plugin updates

Revise your Gradle configuration. In order to to build an Android project written in Kotlin:

  • Set up the kotlin-android gradle plugin and apply it to your project.
  • Add kotlin-stdlib dependencies.

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

Tools | Kotlin | Configure Kotlin in Project

Check for Kotlin plugin updates:

Tools | Kotlin | Configure Kotlin plugin updates


3. kotlin-stdlib-jre7 renamed to kotlin-stdlib-jdk7 during the 1.2.x release

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 updated to Kotlin 1.2 in 4.3.2 version

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


5. Find conflictive dependencies via command, or build tab since AS 3.1

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

Since Android Studio 3.1 Beta 1, you can use the new build tab to find the conflictive dependency:

In this case, you removed the warning and fixed the issue updating Realm version to 4.3.2:

It's also possible to check it in Travis-ci builds considering the order dependencies are downloaded:

这篇关于Android Studio 在第二次构建后显示 Kotlin 依赖警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆