Hi. I have a KMM project which uses cinterop. When...
# multiplatform
k
Hi. I have a KMM project which uses cinterop. When I build/run from Android Studio or XCode I get "unresolved reference" errors for all the cinterop calls. I've figured out the issue is because the .klib files are not generated for some reason when deploying from the IDE. The task that's failing is compileKotlinIosSimulatorArm64 (or equivalent for whatever platform I'm targeting). Strangely if I run with --scan the .klib files are generated and everything works perfectly! Doing a
./gradlew build
works great too. Once I trigger a work-around I can run from the IDE. It's just when running a clean build from the IDE that I have the issue. Any ideas what might be causing the issue?
Copy code
listOf(
    iosX64(),
    iosArm64(),
    iosSimulatorArm64()
).forEach { target ->
    target.apply {
        compilations.getByName("main") {
            cinterops {
                val libmylib by cinterops.creating {
                    defFile(project.file("src/commonMain/cpp/ios/libmylib.def"))
                    packageName("libmylib")
                }
            }
        }

        binaries {
            framework {
                baseName = "shared"
                embedBitcode("disable")
            }
            staticLib()
        }
    }
}
Copy code
package = libmylib

compilerOpts = -Ipath/to/headers/
headers = my_header.h

staticLibraries = mylib.a
libraryPaths = path/to.lib/
I'm thinking maybe there's gradle task I can use to force generate the klibs?
Just noticed:
Copy code
> Task :compileKotlinIosSimulatorArm64 UP-TO-DATE
Looks like it thinks everything is built for some reason
This solves the issue, but increases my build time:
Copy code
tasks.withType<org.jetbrains.kotlin.gradle.tasks.CInteropProcess> {
    outputs.upToDateWhen { false }
}
There's probably a better solution, but for now I've solved it by checking if the klib file is there. If it is I assume it's up to date and accept it, if it's not I build it
Copy code
listOf(
	iosX64(),
    iosArm64(),
	iosSimulatorArm64()
).forEach { target ->
	target.apply {
		tasks.withType<org.jetbrains.kotlin.gradle.tasks.CInteropProcess> {          
			outputs.upToDateWhen {
				// Check if the klib file exists for this build. 
				// If it is then assume it's up to date.
				file(
					"build/classes/kotlin/${target.name}/main/cinterop/${this.outputFileName}"
				).exists()
			}
		}
	}
}