voize-gmbh / reakt-native-toolkit

Combine React Native with Kotlin Multiplatform (KMP)
Apache License 2.0
124 stars 4 forks source link

Build error – Actual class/constructor has no corresponding expected declaration (generated Kotlin files) #29

Closed Samueljh1 closed 11 months ago

Samueljh1 commented 1 year ago

After following the project setup steps, I tried to build my project after adding the @ReactNativeModule and @ReactNativeMethod annotations (using the example CalculatorRNModule class).

During the build process, the toolkit generates the CalculatorRNModuleProvider.kt file, which then causes the two compilation errors:

The CalculatorRNModule class in my project is defined under commonMain in the shared module.

Legion2 commented 1 year ago

Which Multiplatform targets(Android,iOS,js) are you compiling for?

Samueljh1 commented 1 year ago

Which Multiplatform targets(Android,iOS,js) are you compiling for?

I am compiling for Android and iOS. The library code works fine when removing the annotations and calling it directly in Swift/Kotlin (i.e., in native code).

Legion2 commented 1 year ago

Have you defined your annotated RN module in common code and added this to you gradle config?

dependencies {
    add("kspCommonMainMetadata", "de.voize:reakt-native-toolkit-ksp:<version>")
    add("kspAndroid", "de.voize:reakt-native-toolkit-ksp:<version>")
    add("kspIosX64", "de.voize:reakt-native-toolkit-ksp:<version>")
    add("kspIosArm64", "de.voize:reakt-native-toolkit-ksp:<version>")
}

tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().configureEach {
    if (name != "kspCommonMainKotlinMetadata") {
        dependsOn("kspCommonMainKotlinMetadata")
    } else {
        finalizedBy("copyGeneratedTypescriptFiles")
    }
}

Please check that CalculatorRNModuleProvider is generated in the common code (android/shared/build/generated/ksp/metadata/commonMain/kotlin) and available in your common code during compilation.

boluokvmd commented 11 months ago

@Samueljh1 Were you able to resolve this? currently facing the same issue.

boluokvmd commented 11 months ago

Have you defined your annotated RN module in common code and added this to you gradle config?

dependencies {
    add("kspCommonMainMetadata", "de.voize:reakt-native-toolkit-ksp:<version>")
    add("kspAndroid", "de.voize:reakt-native-toolkit-ksp:<version>")
    add("kspIosX64", "de.voize:reakt-native-toolkit-ksp:<version>")
    add("kspIosArm64", "de.voize:reakt-native-toolkit-ksp:<version>")
}

tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().configureEach {
    if (name != "kspCommonMainKotlinMetadata") {
        dependsOn("kspCommonMainKotlinMetadata")
    } else {
        finalizedBy("copyGeneratedTypescriptFiles")
    }
}

Please check that CalculatorRNModuleProvider is generated in the common code (android/shared/build/generated/ksp/metadata/commonMain/kotlin) and available in your common code during compilation.

CalculatorRNModuleProvider is generated in my case

public expect class CalculatorRNModuleProvider() : ReactNativeModuleProvider have this there

but looks like two CalculatorRNModuleProvider are generated one in androidDebug and the other in androidRelease. The androidDebug version is the one with the error

Samueljh1 commented 11 months ago

@Samueljh1 Were you able to resolve this? currently facing the same issue.

No, I wasn't able to unfortunately. I ended up working on a different project so stopped trying to get this to work. Please let me know if you can get this working, however.

Legion2 commented 11 months ago

Hey, can you share a reproducer project, so we can investigate the root cause of the problem. Without the full picture it's difficult to pin point the exact problem.

Xerosigma commented 11 months ago

Hey, can you share a reproducer project, so we can investigate the root cause of the problem. Without the full picture it's difficult to pin point the exact problem.

Also seeing this. Will try to slap something together.

boluokvmd commented 11 months ago

Hey, can you share a reproducer project, so we can investigate the root cause of the problem. Without the full picture it's difficult to pin point the exact problem.

I have uploaded a zipped version of the reproducer project to Drive for you to download. Here is the link: Google Drive Link. Please let me know if there are any issues with accessing the file.

Legion2 commented 11 months ago

@boluokvmd thanks for the reproducer, I was able to find the missing configuration, I added kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin") to kotlin > sourceSets > commonMain in the shared/build.gradle.kts. I will add this to the readme

Legion2 commented 11 months ago

You also need to remove the kotlin.srcDir instructions from the androidMain source set (kotlin.srcDir("build/generated/ksp/android/androidDebug/kotlin")), which is not needed and causes gradle task dependency issues. Then it successfully builds the project.

boluokvmd commented 11 months ago

@boluokvmd thanks for the reproducer, I was able to find the missing configuration, I added kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin") to kotlin > sourceSets > commonMain in the shared/build.gradle.kts. I will add this to the readme

Thanks @Legion2 This solves the problem cc @Samueljh1

Legion2 commented 11 months ago

I'm closing this as resolved, feel free to reopen if you still have problems.