voize-gmbh / reakt-native-toolkit

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

Running example app IllegalStateException #52

Closed rocketraman closed 5 months ago

rocketraman commented 5 months ago

I'm trying to run the example app from the main branch with no modifications, and am getting the following error during the gradle build:

e: java.lang.IllegalStateException: e: Could not find "/Users/raman/source_ext/reakt-native-toolkit/example/android/shared/build/kotlinTransformedMetadataLibraries/commonMain/de.voize-reakt-native-toolkit-0.16.0-commonMain-_D7riQ.klib" in [/Users/raman/Library/Application Support/kotlin/daemon]
at org.jetbrains.kotlin.library.SingleFileResolveKt$resolveSingleFileKlib$1.fatal(SingleFileResolve.kt:21)
at org.jetbrains.kotlin.library.KotlinLibrarySearchPathResolver.resolve(SearchPathResolver.kt:171)
at org.jetbrains.kotlin.library.KotlinLibrarySearchPathResolver.resolve(SearchPathResolver.kt:176)
at org.jetbrains.kotlin.library.CompilerSingleFileKlibResolveStrategy.resolve(SearchPathResolver.kt:298)
at org.jetbrains.kotlin.library.SingleFileResolveKt.resolveSingleFileKlib(SingleFileResolve.kt:24)
at org.jetbrains.kotlin.library.SingleFileResolveKt.resolveSingleFileKlib$default(SingleFileResolve.kt:15)
at org.jetbrains.kotlin.cli.metadata.KlibMetadataDependencyContainer.<init>(K2MetadataKlibSerializer.kt:117)
at org.jetbrains.kotlin.cli.metadata.K2MetadataKlibSerializer$serialize$analyzer$1.invoke(K2MetadataKlibSerializer.kt:49)
at org.jetbrains.kotlin.cli.metadata.K2MetadataKlibSerializer$serialize$analyzer$1.invoke(K2MetadataKlibSerializer.kt:43)
at org.jetbrains.kotlin.cli.metadata.CommonAnalysisKt.runCommonAnalysisForSerialization(CommonAnalysis.kt:42)
at org.jetbrains.kotlin.cli.metadata.K2MetadataKlibSerializer.serialize(K2MetadataKlibSerializer.kt:48)
at org.jetbrains.kotlin.cli.metadata.K2MetadataCompiler.doExecute(K2MetadataCompiler.kt:122)
at org.jetbrains.kotlin.cli.metadata.K2MetadataCompiler.doExecute(K2MetadataCompiler.kt:40)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:100)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:46)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1486)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:712)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:587)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:705)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:704)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Legion2 commented 5 months ago

Which command did you use to run the project and which platform you are targeting?

rocketraman commented 5 months ago

Which command did you use to run the project and which platform you are targeting?

cd js
yarn install
cd ../example
yarn install
yarn start

Then press a for android. I haven't experimented much with the iOS build, but that fails for a different reason. I figured I'd try to get the "easier" one working first :-)

~One thing I noticed is that the stack trace contains the K2MetadataCompiler -- which is odd as this is not Kotlin 2.x.~ -- per https://github.com/JetBrains/kotlin/blob/master/compiler/cli/src/org/jetbrains/kotlin/cli/metadata/K2MetadataCompiler.kt this class is used by K1 and K2.

I've also tried this on a completely different box (Linux) for Android, and I get the same error there.

rocketraman commented 5 months ago

For iOS, doing:

cd android
./gradlew podPublishXCFramework

fails with the same error. So its not getting past the multiplatform build.

Legion2 commented 5 months ago

The problem is the react native version uses an old kotlin version which is not compatible with the kotlin version used in the library. The last version of the toolkit which is compatible with the rn version in the example is 0.11.0 (https://github.com/voize-gmbh/reakt-native-toolkit/commit/fb3bef93893ee37e6b51da7a849c498592a4a8a2). Somehow the version was bumped in the example. I have to check if the newest RN version uses a newer kotlin version so we can update it in the example.

We internally use a custom build process which uses the newest kotlin version, so we have no compatibility issues with old react native versions.

Legion2 commented 5 months ago

Upgrading the example to 0.74.0 should work, it uses kotlin 1.9.22, 0.73.0 does not work, because it still uses kotlin 1.8.0. Upgrade guide: https://react-native-community.github.io/upgrade-helper/?from=0.72.7&to=0.74.0-rc.5

rocketraman commented 5 months ago

@Legion2 Thank you very much, the upgrade helper tool was very useful. I've made progress but I still can't get past another issue -- for some reason the android source set in the example cannot resolve the util package from the toolkit:

> Task :app:compileReleaseKotlin FAILED
e: file:///home/raman/source_ext/reakt-native-toolkit/example/android/app/src/main/java/com/myrnproject/RNPackage.kt:10:36 Unresolved reference: util
e: file:///home/raman/source_ext/reakt-native-toolkit/example/android/app/src/main/java/com/myrnproject/RNPackage.kt:19:11 Unresolved reference: getModules

I have submitted a draft PR here with what I have so far: https://github.com/voize-gmbh/reakt-native-toolkit/pull/53. Some of what I have might need to be cleaned up a bit, I've been trying many things. I'll try to continue debugging, but if you get a chance to try this out, I'd appreciate your input.

rocketraman commented 5 months ago

On Android, I can't get past the error I mentioned previously about the compilation errors (it seems the aar file is being built correctly, but the example app android module just cannot see those util classes). And on iOS, the example AppDelegate.mm looks nothing like the upstream version, even in version 0.72.7, and doesn't compile at all. Reverting to the upstream code and adding just the modifications in the README to import the shared module and call implement extraModulesForBridge gets me past iOS compilation issues, but runtime fails with:

LOG  Running "MyRNProject" with {"rootTag":1,"initialProps":{"concurrentRoot":false}}
ERROR  TypeError: Cannot read property 'unsubscribeFromToolkitUseFlow' of null

This error is located at:
in Name (created by App)
in RCTSafeAreaView (created by App)
in App
in RCTView (created by View)
in View (created by AppContainer)
in RCTView (created by View)
in View (created by AppContainer)
in AppContainer
in MyRNProject(RootComponent), js engine: hermes
ERROR  TypeError: Cannot read property 'unsubscribeFromToolkitUseFlow' of null

This error is located at:
in Name (created by App)
in RCTSafeAreaView (created by App)
in App
in RCTView (created by View)
in View (created by AppContainer)
in RCTView (created by View)
in View (created by AppContainer)
in AppContainer
in MyRNProject(RootComponent), js engine: hermes
Legion2 commented 5 months ago

TypeError: Cannot read property 'unsubscribeFromToolkitUseFlow' of null mean that you either not using the same version of the toolkit in js and kotlin or you have not registered all your RN modules https://github.com/voize-gmbh/reakt-native-toolkit?tab=readme-ov-file#register-generated-ios-module

Legion2 commented 5 months ago

For the Android error make sure you use the latest toolkit version, older versions don't have the getModules helper.

rocketraman commented 5 months ago

I'm trying to run the example. I assumed the example had everything it needs. Is that a bad assumption? For Android I'm using my local snapshot version, which does have the getModules helper.

Legion2 commented 5 months ago

I fixed the android build and pushed it to your branch

rocketraman commented 5 months ago

I fixed the android build and pushed it to your branch

Thanks, I see you moved the offending code into the shared lib androidMain from the app build. I think that makes sense. The example works on Android now.

Unfortunately I still cannot get iOS to work for the example. I believe the example modules are registered:

https://github.com/voize-gmbh/reakt-native-toolkit/pull/53/commits/2187bbbb8e3a8655042bc9ea0107a6ab5f6da314#diff-495317fd5335e57c0c1df4380cd5bf2278818bca178a5edff3f4d8a1015c1cc3

And the toolkit is the right version:

grep toolkit example/yarn.lock
reakt-native-toolkit@0.16.0:
resolved "https://registry.yarnpkg.com/reakt-native-toolkit/-/reakt-native-toolkit-0.16.0.tgz#880791d36454f69e7b3bc5921a378dfebf064604"

I believe the new RCTDelegate is not running the extraModulesForBridge method. Migrating back to the full-blown AppDelegate.mm that does not extend the new RCTDelegate, I'm running into https://github.com/facebook/react-native/issues/38193, which I haven't yet figured out how to work around.

rocketraman commented 5 months ago

I figured out how to work around https://github.com/facebook/react-native/issues/38193 and I believe the issue is upstream -- see https://github.com/facebook/react-native/issues/38193#issuecomment-2072243996. I have it working on both iOS and Android with the latest React Native.

I'm submitting the PR for review.

mmmoussa commented 1 month ago

I believe the new RCTDelegate is not running the extraModulesForBridge method

I ran into the same problem while upgrading from RN 0.73 to RN 0.74. Eventually I discovered that this issue is now fixed in RN 0.75 without switching back to the old AppDelegate.mm pattern. Leaving this comment here as context in case someone else runs into the same issue.