同一 Flutter 应用程序中的多个 Kotlin 运行时

Roc*_*nat 6 dependencies android kotlin flutter

我正在构建一个使用一些包的 Flutter 应用程序。其中一些包捆绑了 Kotlin 运行时。当然,不同的包使用不同的 Kotlin 运行时。此外,我的应用程序中有很多本机代码,当然也依赖于 Kotlin。因此,Kotlin 在我自己的应用程序本机代码和 Flutter 依赖项中都被声明为依赖项。

这会导致在构建发布 apk 和 appbundle 时出现此消息:

w: Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
    C:/Users/roc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.3.50/50ad05ea1c2595fb31b800e76db464d08d599af3/kotlin-stdlib-jdk7-1.3.50.jar (version 1.3)
    C:/Users/roc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.5.10/c49d0703d16c6cb1526cc07b9b46486da1dd8a60/kotlin-stdlib-jdk7-1.5.10.jar (version 1.5)
    C:/Users/roc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.5.10/da6a904b132f0402fa4d79169a3c1770598d4702/kotlin-stdlib-1.5.10.jar (version 1.5)
    C:/Users/roc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.5.10/6b84d926e28493be69daf673e40076f89492ef7/kotlin-stdlib-common-1.5.10.jar (version 1.5)
    C:/Users/roc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.3.71/898273189ad22779da6bed88ded39b14cb5fd432/kotlin-stdlib-1.3.71.jar (version 1.3)
    C:/Users/roc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.3.61/70dffc5f8ac5ea7c34f30deac5b9d8b1d48af066/kotlin-stdlib-jdk7-1.3.61.jar (version 1.3)
    C:/Users/roc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.3.71/e71c3fef58e26affeb03d675e91fd8abdd44aa7b/kotlin-stdlib-common-1.3.71.jar (version 1.3)
    C:/Users/roc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.3.50/b529d1738c7e98bbfa36a4134039528f2ce78ebf/kotlin-stdlib-1.3.50.jar (version 1.3)
    C:/Users/roc/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.3.50/3d9cd3e1bc7b92e95f43d45be3bfbcf38e36ab87/kotlin-stdlib-common-1.3.50.jar (version 1.3)
w: Some runtime JAR files in the classpath have an incompatible version. Consider removing them from the classpath
Run Code Online (Sandbox Code Playgroud)

如果它发生在应用程序的 Android 端,我可以将 Kotlin 从我的一些依赖项中排除,并使用应用程序的 Kotlin 版本。因此,Kotlin 只会有一个版本。但是,由于此依赖项不在 Android 中build.gradle,而是在 Flutter 中pubspec.yaml,所以我不知道如何dart pub排除 Kotlin 运行时并使用应用程序的运行时。

Gui*_*lem 0

对我来说,这个问题来自audioplayers包。

我正在运行 Flutter 2.0.0 和audioplayers0.20.1

我的解决方案:

  1. 运行flutter create .以更新 Flutter 项目上的 Flutter 设置。
  2. 更新 Kotlin 和 Gradle 的版本buildscript(在文件中android/build.gradle)。这些较新的版本对我有用:
buildscript {
    ext.kotlin_version = '1.4.32'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 修改 Gradle 的一个版本android/gradle/wrapper/gradle-wrapper.properities
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
Run Code Online (Sandbox Code Playgroud)
  1. 运行flutter clean和/或flutter pub get. 我不知道为什么这会有所帮助,也不会让你付出太多代价。
  2. 运行您的应用程序flutter run,或者flutter --no-sound-null-safety如果您有混合包或/和您自己的代码,不带null safety.

(可选)清理your_user_home_path/.gradle/caches/目录中的所有文件夹和文件。


  • 您可以尝试其他 Kotlin 和 Gradle 版本。要获得灵感,请为每个不同的 Flutter SDK 版本运行不同的 Flutter 项目,并查看android/build.gradlegradle-wrapper.properities
  • 新的 Flutter 版本(可能从 2.9.0 开始)将需要Kotlin 1.5.31. 在这里阅读https://docs.flutter.dev/release/writing-changes/kotlin-version