Not sure if this should be in multiplatform or her...
# gradle
t
Not sure if this should be in multiplatform or here, but it seems more like a gradle thing, so here it goes. I am refactoring my existing multiplatform library build.gradle/settings.gradle into build.grade.kts/settings.gradle.kts. Part of this is moving from the ‘old’ apply plugin to the ‘new’ plugins block. I spent some time getting the gradle build to work from Xcode via the cocoapods script. But when I go back into Android Studio I get this error:
Plugin request for plugin already on the classpath must not include a version
the plugins like is like so:
Copy code
id("org.jetbrains.kotlin.multiplatform") version "1.4.21"
So I need that version to work from iOS (which appears to go through the settings.gradle.kts resolutionStrategy eachPlugin block), but AS does not like it. Anybody have a solution handy?
v
Do you maybe have an MCVE instead of an abstract description? Would make things much easier I think.
t
I would advice you to define all plugins and versions via
settings.gradle.kts
: https://docs.gradle.org/current/dsl/org.gradle.plugin.use.PluginDependenciesSpec.html#N1B7C1 Then in build files you just use required plugin without version
t
Okay, I tried putting a plugins block in settings.gradle.kts, like so:
Copy code
plugins {
    kotlin("multiplatform") version "1.4.21" apply false
}
and the call from Xcode does not give me an error about finding the plugin. But now gives me an error in the kotlin { android() … } line.
Copy code
Showing All Messages
com/android/build/gradle/BaseExtension
Should writing build files be harder than the app itself, I wonder to myself. 🙂
v
It is not, except if you are doing funny things. Again, can you provide an MCVE instead of incomplete abstract descriptions?
t
Okay, lots of commented out code though:
v
Besides that this plugins block in the settings script does not really make any sense at all, with those two files I'm not getting
Plugin request for plugin already on the classpath must not include a version
but
Cannot add a configuration with name 'androidTestApi' as a configuration with that name already exists.
t
yeah, that is another issue. there is a bug in AGP 7+ that requires that android configuration block.
the above will show the version issue when building in AS
from Xcode we get:
Copy code
Showing All Messages
Plugin [id: 'com.android.library', artifact: 'com.android.tools.build:gradle:null'] was not found in any of the following sources:
Copy code
Showing All Messages
Plugin [id: 'org.jetbrains.kotlin.multiplatform', version: '1.4.21'] was not found in any of the following sources:
The plugins block in settings was the suggestion Yahor above..
v
Not really what he meant, he meant within a
pluginResolution
block. But that is probably not really related to your question. With what you have shown last, I get
Plugin [id: 'com.android.library', artifact: 'com.android.tools.build:gradle:null'] was not found in any of the following sources:
which is totally clear, because you apply the plugin without defining any version anywhere, neither where you apply the plugin, nor where you do the id-to-artifact mapping or where you could define default plugin versions for the whole build.
t
okay, i got that part working. build.gradle.kts:
Copy code
plugins {
    id("com.android.library")
    kotlin("multiplatform")
    kotlin("plugin.serialization") version "1.4.21"
    kotlin("native.cocoapods")
    id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3"
}
and settings.gradle.kts:
Copy code
pluginManagement {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven(url = "<https://kotlin.bintray.com/kotlinx/>")
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.namespace == "com.android") {
                useModule("com.android.tools.build:gradle:4.1.2")
            }
            when (requested.id.id) {
                "kotlin-multiplatform", "org.jetbrains.kotlin.multiplatform", "org.jetbrains.kotlin.native.cocoapods" -> useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21") //$kotlinVersion")
                "kotlinx-serialization", "org.jetbrains.kotlin.plugin.serialization" -> useModule("org.jetbrains.kotlin:kotlin-serialization:1.4.21") //$kotlinVersion")
            }
        }
    }
}
thank you for the input.
Now there is just this one to work around when calling from Xcode: https://youtrack.jetbrains.com/issue/KT-43944
v
Why is it relevant from where you call it? You should always use the Gradle Wrapper and any sane tool should respect the Gradle wrapper, so it should be the same no matter where you call from.
t
So the issue was that calling it from Xcode, it goes through the pluginManagement, where I was using AGP 4.1.2. But in the Android app which uses this shared mp module (and does not use the pluginManagement at the shared module level), it was using 7.0.0-alpha05. So I changed the settings.gradle.kts to use 7.0.0-alpha05 which has the bug and so now the workaround works everywhere.