Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Multiplatform Configuration issue

I continue getting Gradle configuration error in my KMP + Jetpack Compose project

A problem occurred configuring project ':shared'.

Configuration with name 'testApi' not found.

My setup is:

  1. Android Studio Arctic Fox 2020.3.1 Canary 3
  2. Project level setup
dependencies {
   classpath("com.android.tools.build:gradle:7.0.0-alpha03")
   classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.20")
}
  1. 'shared module'
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    id("com.android.library")
}

kotlin {
    android()
    ios {
        binaries {
            framework {
                baseName = "shared"
            }
        }
    }
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("com.google.android.material:material:1.2.1")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.1")
            }
        }
        val iosMain by getting
        val iosTest by getting
    }
}

android {
    compileSdkVersion(30)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(21)
        targetSdkVersion(30)
    }
}

val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
    val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}

tasks.getByName("build").dependsOn(packForXcode)

Note: By removing the configuration part by part, I seem to figure out w that the problem seems to be around the android configuration itself, so if I remove android() part from

kotlin {
    android()
    ....

and just go with simple jvm() it goes well

like image 956
Андрій Пугач Avatar asked Dec 19 '20 17:12

Андрій Пугач


People also ask

How do I set up Kotlin multiplatform?

In Android Studio, select Settings/Preferences | Plugins, search Marketplace for Kotlin Multiplatform Mobile, and then install it. The Kotlin plugin should be compatible with the Kotlin Multiplatform Mobile plugin.

Is Kotlin good for multiplatform?

Multiplatform libraries Kotlin Multiplatform is also useful for library authors. You can create a multiplatform library with common code and its platform-specific implementations for JVM, JS, and Native platforms. Once published, a multiplatform library can be used in other cross-platform projects as a dependency.

Is Kotlin multiplatform mobile stable?

Stable and customizable integration with iOS With the KMM Plugin you can run, test, and debug the iOS part of your application on iOS targets straight from Android Studio. Since the first release of the plugin, its iOS integration has become much more stable and configurable, and it now supports the latest iOS tooling.

Is Kotlin multiplatform production ready?

KMP gives us an unprecedented opportunity to share business logic written in Kotlin between Android and iOS native mobile applications. The technology has already proven itself a production-ready solution applied by many large, medium, and small companies, even though it's only in the alpha stage.


2 Answers

You can use the below code as a workaround in your shared module Gradle file

android {
    configurations {
        create("androidTestApi")
        create("androidTestDebugApi")
        create("androidTestReleaseApi")
        create("testApi")
        create("testDebugApi")
        create("testReleaseApi")
    }
}

NOTE: This has to be put before the kotlin {} block

like image 177
Panduka DeSilva Avatar answered Oct 15 '22 06:10

Panduka DeSilva


Fixed issue in Kotlin 1.5 M1 (pending)

The problem is in Canary or AGP 7.0.0:

  • IDE: Canary 11
  • distributionUrl: 6.8.2
  • 7.0.0-alpha11

Workaround 1:

IMPORTANT: Make sure the file is groovy or dsl

PRECONDITION: These configurations have to be done in all modules / sub-modules of the project that are KMM and the android {} block has to be before the kotlin {} block

For Kotlin DSL:

build.gradle.kts (:kmm_shared)

android {
    configurations {
        create("androidTestApi")
        create("androidTestDebugApi")
        create("androidTestReleaseApi")
        create("testApi")
        create("testDebugApi")
        create("testReleaseApi")
    }
}
kotlin { }

For Groovy:

build.gradle (:kmm_shared)

android {
    configurations {
        androidTestApi {}
        androidTestDebugApi {}
        androidTestReleaseApi {}
        testApi {}
        testDebugApi {}
        testReleaseApi {}
    }
}
kotlin { }

Also, you should to use AGP 7.0 because previous versions of gradle generate problems.

build.gradle.kts (:project) && build.gradle.kts (:buildSrc)

dependencies {
    implementation("com.android.tools.build:gradle:7.0.0-alpha11")
}

Workaround 2 (Deprecated)

Temporarily use a maximum of the beta versions:

  • IDE: Beta 6
  • distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
  • classpath 'com.android.tools.build:gradle:4.2.0-beta06'

Good Luck

  • issue/KT-43944
  • commit in Mobius
like image 28
Braian Coronel Avatar answered Oct 15 '22 04:10

Braian Coronel