8

When I switch build variants I got compilation error, but the problem is only for non default build variants (debug & release). So if I define customBuild { } and then choose that one, it fails with this error log:

    The consumer was configured to find a runtime of a component, 
    preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' 
    with value 'customBuild', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm'.
    However we cannot choose between the following variants of project :shared-module:
- iosArm64RuntimeOnly
          - iosX64RuntimeOnly
        All of them match the consumer attributes:
          - Variant 'iosArm64RuntimeOnly' capability ExampleApp:shared-module:1.0:
              - Unmatched attributes:
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'customBuild')
                  - Doesn't say anything about its target Java environment (preferred optimized for Android)
                  - Doesn't say anything about its usage (required a runtime)
                  - Provides attribute 'org.jetbrains.kotlin.native.target' with value 'ios_arm64' but the consumer didn't ask for it
                  - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'androidJvm')

My build.gradle.kts for shared module

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
  kotlin("multiplatform")
  kotlin("plugin.serialization")
  id("com.android.library")
  id("com.squareup.sqldelight")
  kotlin("native.cocoapods")
}

repositories {
  gradlePluginPortal()
  google()
  mavenCentral()
  maven {
    url = uri("https://plugins.gradle.org/m2/")
  }
}

version = "1.0"

kotlin {
  android {
    publishAllLibraryVariants()
  }
  ios()
  cocoapods { 
    framework {
      baseName = "core"
    }
    listOf(
      iosX64(),
      iosArm64(),
      iosSimulatorArm64(),
    ).forEach {
       it.binaries.framework {
       baseName = "core"
    }
   }
  }

  targets.filterIsInstance<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget>().forEach {
    it.binaries.filterIsInstance<org.jetbrains.kotlin.gradle.plugin.mpp.Framework>()
      .forEach { lib ->
        lib.isStatic = false
        lib.linkerOpts.add("-lsqlite3")
      }
  }

  sourceSets {

    val commonMain by getting {
      dependencies { ... }
    }
    val commonTest by getting {
      dependencies { ... }
    }
    val androidMain by getting {
      dependencies { ... }
    }

    val androidAndroidTestRelease by getting

    val androidTest by getting {
      dependsOn(androidAndroidTestRelease)
      dependencies { ... }
    }

    val iosMain by getting {
      dependencies { ... }
    }

    val iosTest by getting
  }
}

configurations.all { }

android {
  compileSdk = 30
  sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
  defaultConfig {
    minSdk = 21
    targetSdk = 30
    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
  }
  compileOptions {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
  }
}

sqldelight {
  database("db") {
    packageName = "com.example.app.core.sqldelight"
    sourceFolders = listOf("sqldelight")
    schemaOutputDirectory = file("src/commonMain/sqldelight/databases")
    dialect = "sqlite:3.24"
  }
}


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)
  ...
}

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

val generateIOSArm64Framework by tasks.creating(Sync::class) {
  group = "build"
  val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
  val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
  val targetName = "iosArm64"
  val framework =
    kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
  inputs.property("mode", mode)
  ...
}

val generateIOSXCFramework by tasks.creating(Sync::class) {
  dependsOn("packForXcode", "generateIOSArm64Framework")
}
Jemo Mgebrishvili
  • 5,187
  • 7
  • 38
  • 63

0 Answers0