1 year ago

#376576

test-img

Okko Kauhanen

Android Studio Kotlin redeclaration error with actual/expect

Android Studio reports a redeclaration error with Kotlin objects even though Gradle builds the project correctly.

I have this in my commonMain

expect object ApplicationConfig {
    val url1: String
    val url2: String
    val url3: String
}

I have source sets commonMain, androidBase, commonDevelopment, commonStaging, commonRelease, androidDevelopment, androidStaging and androidRelease.

commonMain declares an expect object for application settings. commonDevelopment, commonStaging and commonRelease declare the actual objects with build specific settings. I am hoping to reuse these for Android and iOS builds.

androidDevelopment, androidStaging and androidRelease depend on the corresponding source sets commonXXXX and source set androidBase.

sourceSets {
    val commonMain by getting {
        dependencies { ... }
    }
    val androidBase by creating {
        dependencies { ... }
    }

    val commonDevelopment by creating {
        dependsOn(commonMain)
    }

    val commonRelease by creating {
        dependsOn(commonMain)
    }

    val commonStaging by creating {
        dependsOn(commonMain)
    }

    val androidRelease by getting {
        dependsOn(commonRelease)
        dependsOn(androidBase)
    }

    val androidStaging by creating {
        dependsOn(commonStaging)
        dependsOn(androidBase)
    }

    val androidDevelopment by creating {
        dependsOn(commonDevelopment)
        dependsOn(androidBase)
    }
}

Hierarchy

I have the following build types:

android {
    buildTypes {
        getByName("release")
        create("staging") {
            isJniDebuggable = true
        }
        create("development") {
            isDefault = true
            isJniDebuggable = true
        }
    }
...
}

Gradle seems to understand that only one source set with the actual object is included, but Android Studio doesn't detect this. I do not need product flavors. Build types are sufficient. I want to reuse these settings classes for Android and iOS.

Am I misusing the expect/actual mechanism and build types or is this just a bug in Android Studio?

I have these settings:

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10")
        classpath("com.android.tools.build:gradle:7.1.2")
    }

And

distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip

android-studio

kotlin

gradle

multiplatform

redeclaration

0 Answers

Your Answer

Accepted video resources