I have recently upgraded my React Native app to version 0.73.0 which also includes switching from Java to Kotlin for MainActivity and MainApplication classes.
Now when I try to run the app, I get the following error:
java.lang.RuntimeException: Unable to instantiate application xxx.xxx.xxx.MainApplication package xxx.xxx.xxx: java.lang.ClassNotFoundException: Didn’t find class "xxx.xxx.xxx.MainApplication" on path: DexPathList
How can I resolve this?
2
Answers
OK. After spending a couple of hours I finally figured out how to fix this. This is not mentioned in React Native 0.73.0 upgrade documentation.
So, in order to get the project working, you need to take the following steps after your actual RN upgrade:
build.gradle
and add includekotlinVersion = '1.9.0'
google()
in thebuildscript.repositories
andallprojects
in the samebuild.gralde
file.classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${project.ext.kotlinVersion}"
in thedependencies
.Sync
.build.grdle
file inside theapp
folder and addapply plugin: "kotlin-android"
at the top of the file.implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.0"
.Sync
.Now your project will run correctly.
After some additional testing today, I’ve found out that my issue is something with the new Kotlin files (MainActivity, MainApplication).
I’ve followed the React Native Upgrade Helper file by file since the npx upgrade failed, and what I’ve noticed is that my application will build successfully agaon once I revert the file extensions to .java instead of .kt. Even though I’ve updated the code inside the both files from RN Upgrade Helper, with the minimal custom code (in my case for Bootsplash screen and RN Screens).
This should be the temporary solution only, because the main change of this version is switching to Kotlin for Android, we should see how the next minor version can affect this.