https://kotlinlang.org logo
b

Ben Brugman

03/04/2021, 1:03 PM
Hello, as many others i am also new to this channel and i am looking for help. I have set-up everything and it works as expected and i can acces the shared folder from both apps (Android-Kotlin/iOS-Swift). My aim is to be able to acces app code during my automated ui test The current setup is that i made a Singleton in my shared module which contains a boolean (and other variables and methods). During my test run i want to set the Boolean to True When the method in the app is executed, it will check this Boolean wether or not is is True If the Boolean is True a method is used to assign values to the variables in the singleton Then in the continuation of the UITest i can acces those variables to validate stuff. Again most of it works, i can acces everything and it seems to work But, when the Singleton goes through Swift things go wrong, and it gives errors about frozen variables. The Annotation @Threadlocal takes this away. but then the end result is that the Singleton is instantiated twice and the values i am looking for are not stored in the Test run instance (.getInstance) Am i doing something wrong or is this the limitations (or strictness) of Swift? If anyone knows a better way i am also open to that because i believe Singletons are kind of frowned upon. I hope someone has done this successfully and can help me out, sorry for the long post.
k

Kris Wong

03/04/2021, 2:00 PM
b

Ben Brugman

03/04/2021, 9:47 PM
Ok thank you, i had a look and i feel like i should try out the Atomic values.
I have been trying for a while now but i get this error
Copy code
* What went wrong:
Execution failed for task ':shared:compileKotlinIosX64'.
> Could not resolve all files for configuration ':shared:iosX64CompileKlibraries'.
   > Could not resolve org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.15.1.
     Required by:
         project :shared
      > No matching variant of org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.15.1 was found. The consumer was configured to find a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native', attribute 'org.jetbrains.kotlin.native.target' with value 'ios_x64' but:
          - Variant 'apiElements' capability org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.15.1 declares an API of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_x64')
          - Variant 'runtimeElements' capability org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.15.1 declares a runtime of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'ios_x64')
Could i choose a different variant to fix this?
Copy code
atomicfu {
  dependenciesVersion = '0.15.1' // set to null to turn-off auto dependencies
  transformJvm = true // set to false to turn off JVM transformation
  transformJs = true // set to false to turn off JS transformation
  variant = "FU" // JVM transformation variant: FU,VH, or BOTH 
  verbose = false // set to true to be more verbose  
}
This is my shared gradle
Copy code
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("kotlin-parcelize")
    id("kotlinx-atomicfu")
}

group = "com.example.Brewster"
version = "1.0-SNAPSHOT"


kotlin {
    android()
    ios()

    cocoapods {
        summary = "Kotlin sample project with CocoaPods dependencies"
        homepage = "<https://github.com/Kotlin/kotlin-with-cocoapods-sample>"

        ios.deploymentTarget = "12.0"
        
        podfile = project.file("../BrewMachine/Podfile")
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.15.1")
                kotlin("stdlib-jdk8")
            }
        }

        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")
                implementation("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.15.1")
            }
        }
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13")
            }
        }
        val iosMain by getting{
            dependencies {
                implementation("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.15.1")
            }
        }
        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)
dependencies {
    implementation("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.15.1")
}