Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude a Gradle dependency that is added by the Realm plugin

Sporadically, my Gradle syncs will fail. I'll be given the unhelpful message that "a 3rd party Gradle plugin" may be the cause. If I open up the Event Log, I'll see the message:

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.

The mismatched number is neither the Kotlin version in my Gradle files, or the version of my Kotlin plugin in Android Studio.

After running a Gradle dependency tree, I found the culprit:

+--- io.realm:realm-android-kotlin-extensions:5.1.0
|    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.10 -> 1.2.51

I'm not including any realm-android-kotlin-extensions library. I'm assuming that it's added by classpath "io.realm:realm-gradle-plugin:5.1.0" and apply plugin: "realm-android"

This makes matters difficult. If it were a regular dependency, I could try something like

implementation "io.realm:realm-android-kotlin-extensions:5.1.0" {
    transitive = false
}

or

implementation "io.realm:realm-android-kotlin-extensions:5.1.0" {
    exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk7"
}

Theoretically that would probably work. It would be forced to use the newer version of Kotlin, the error would go away, and hopefully Gradle syncs would work and all would be well with the world. But if I try this approach my Gradle sync fails and I get this error in the Event Log:

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.

I'm assuming that's because this isn't a dependency I added (perhaps not a public dependency?), that it fails because the dependency is added via the plugin instead of directly in my Gradle file.

So how do I fix this? Perhaps there's a line I can add to tell the Realm plugin to exclude the obsolete dependency? Or am I completely barking up the wrong tree and the solution for my Kotlin version clash issue is something else entirely?

(BTW, if you're wondering why I'm using Realm 5.1.0, 5.3.1 causes some weird bugs in our app, so we're waiting for a later version to be released in the hope that will no longer cause the issues.)

like image 388
Chad Schultz Avatar asked Jul 20 '18 16:07

Chad Schultz


1 Answers

One of the things on my personal "I wish I had known this a year ago" list is that you can manually add what Realm adds to your project instead of relying on the Gradle plugin.

 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"
 }

As per docs.

like image 96
EpicPandaForce Avatar answered Oct 12 '22 02:10

EpicPandaForce