排除 Realm 插件添加的 Gradle 依赖项

Cha*_*ltz 5 android realm gradle kotlin

偶尔,我的 Gradle 同步会失败。我会收到一条无用的消息,即“第 3 方 Gradle 插件”可能是原因。如果我打开事件日志,我会看到以下消息:

Outdated Kotlin Runtime
                                Your version of Kotlin runtime in 'Gradle: org.jetbrains.kotlin:kotlin-stdlib:1.2.10@jar' library is 1.2.10-release-109 (1.2.10), while plugin version is 1.2.51-release-Studio3.1-1.
                                Runtime library should be updated to avoid compatibility problems.
Run Code Online (Sandbox Code Playgroud)

不匹配的数字既不是我的 Gradle 文件中的 Kotlin 版本,也不是我在 Android Studio 中的 Kotlin 插件的版本。

运行 Gradle 依赖树后,我找到了罪魁祸首:

+--- io.realm:realm-android-kotlin-extensions:5.1.0
|    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.10 -> 1.2.51
Run Code Online (Sandbox Code Playgroud)

我不包括任何realm-android-kotlin-extensions图书馆。我假设它是由classpath "io.realm:realm-gradle-plugin:5.1.0"和添加的apply plugin: "realm-android"

这让事情变得困难。如果它是常规依赖,我可以尝试类似

implementation "io.realm:realm-android-kotlin-extensions:5.1.0" {
    transitive = false
}
Run Code Online (Sandbox Code Playgroud)

或者

implementation "io.realm:realm-android-kotlin-extensions:5.1.0" {
    exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk7"
}
Run Code Online (Sandbox Code Playgroud)

从理论上讲,这可能会奏效。它将被迫使用较新版本的 Kotlin,错误会消失,希望 Gradle 同步能够正常工作并且一切都会好起来。但是,如果我尝试这种方法,我的 Gradle 同步就会失败,并且会在事件日志中收到此错误:

Gradle sync failed: Could not find method io.realm:realm-android-kotlin-extensions:5.1.0() for arguments [build_2krw7i3nwfkd5lrq1ly9b8huw$_run_closure3$_closure29@7b5b2081] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Run Code Online (Sandbox Code Playgroud)

我假设这是因为这不是我添加的依赖项(也许不是公共依赖项?),它失败是因为依赖项是通过插件而不是直接在我的 Gradle 文件中添加的。

那么我该如何解决这个问题?也许我可以添加一行来告诉 Realm 插件排除过时的依赖项?或者我完全在错误的树上吠叫,而我的 Kotlin 版本冲突问题的解决方案完全是另一回事?

(顺便说一句,如果你想知道为什么我使用 Realm 5.1.0,5.3.1 会在我们的应用程序中导致一些奇怪的错误,所以我们正在等待更高版本的发布,希望不会再导致问题。)

Epi*_*rce 4

我个人的“我希望一年前就知道这一点”列表中的一件事是,您可以手动添加 Realm 添加到项目中的内容,而不是依赖 Gradle 插件。

 buildscript {
    ext.kotlin_version = '1.2.51'
    ext.realm_version = '5.3.1'

    repositories {
        jcenter()
        mavenCentral()
    }

   dependencies {
       classpath "io.realm:realm-transformer:5.1.0"
       classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
   }
 }

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

 import io.realm.transformer.RealmTransformer
 android.registerTransform(new RealmTransformer(project))

 dependencies {
   implementation "io.realm:realm-annotations:$realm_version"
   implementation "io.realm:realm-android-library:$realm_version"
   implementation "io.realm:realm-android-kotlin-extensions:$realm_version" {
       exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk7"
   }
   kapt "io.realm:realm-annotations-processor:$realm_version"
 }
Run Code Online (Sandbox Code Playgroud)

根据文档