android - 同一个 Flutter 应用中的多个 Kotlin 运行时

标签 android flutter kotlin dependencies

我正在构建一个使用一些包的 Flutter 应用程序。其中一些包 bundle 了 Kotlin 运行时。当然,不同的包使用不同的 Kotlin 运行时。此外,我的应用程序中有很多 native 代码,当然也依赖于 Kotlin。因此,Kotlin 在我自己的应用程序 native 代码和 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
如果它发生在应用程序的 Android 端,我可以从我的一些依赖项中排除 Kotlin,并使用应用程序的 Kotlin 版本。所以,只有一个版本的 Kotlin 会在那里。但是,由于此依赖项不在 Android 的 build.gradle 中但在 Flutter 的 pubspec.yaml , 不知道怎么说dart pub排除 Kotlin 运行时并使用应用程序的运行时。

最佳答案

对我来说,这个问题来自 audioplayers 包裹。
我正在运行 Flutter 2.0.0 和 audioplayers 0.20.1
我的解决方案:

  • 运行flutter create .更新 Flutter 项目的 Flutter 设置。
  • buildscript 上更新 Kotlin 和 Gradle 版本(在文件 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"
        }
    }
    
  • 修改 android/gradle/wrapper/gradle-wrapper.properities 中的 Gradle 版本:
  • distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
    
  • 运行flutter clean和/或 flutter pub get .我不知道为什么这会有所帮助,它对你来说不会花费太多。 😉
  • 运行您的应用程序flutter runflutter --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/breaking-changes/kotlin-version
  • 关于android - 同一个 Flutter 应用中的多个 Kotlin 运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67885993/

    相关文章:

    android - NoSuchFieldError : PREFER_HIGHEST_OR_REMOTE_VERSION_NO_FORCE_STAGING 错误

    android - ViewPager 转换像 Google Play Music "Player"

    flutter - 如何在改变 parent 时使用 `GlobalKey` 来维护小部件的状态?

    flutter - 关闭应用程序时保持 VideoPlayerController 播放音频

    java - 不幸的是 MyApp 已停止。我该如何解决这个问题?

    c# - 将 Lambda 函数传递给 Xamarin.Android 绑定(bind)库中的 Kotlin 的 C# 生成代码

    android - 如何加载联系人照片?

    java - 如何使用 onClick 监听器将字符串从 TextView 传输到主 Activity

    flutter - Dialogflow 机器人没有响应

    rx-java - 如何在没有 .flatMap 的情况下控制流,这会破坏 react 流,从而阻止 distinctUntilChanged 等运算符在整个流上工作