Issue
Problem
I am trying to create my own native module, but React Native can't load it. I always get this error when trying to start the app with npx react-native start
:
/home/somedude/Development/my-app/android/app/src/main/java/com/coolapp/MainApplication.java:28: error: cannot find symbol packages.add(new GLBPackage()); ^ symbol: class GLBPackage
What I did
I followed the documentation to a point to create that module. This is from my MainApplication.java:
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
packages.add(new ReactViroPackage(ReactViroPackage.ViroPlatform.valueOf("AR")));
packages.add(new GLBPackage());
return packages;
}
I also created an GLBPackage.kt:
class GLBPackage : ReactPackage {
override fun createViewManagers(
reactContext: ReactApplicationContext
): MutableList<ViewManager<View, ReactShadowNode<*>>> = mutableListOf()
override fun createNativeModules(
reactContext: ReactApplicationContext
): MutableList<NativeModule> = listOf(GLBModule(reactContext)).toMutableList()
}
and a GLBModule.kt:
class GLBModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
override fun getName(): String {
return "GLBModule"
}
@ReactMethod
fun loadModel(path: String) {
Log.d("GLBModule", "Loading $path")
}
}
Why do I get this error? Android Studio also says everything is ok.
Solution
I had a similar error when trying to load the CalendarModule provided in the ReactNative docs using Kotlin.
What worked for me: I added the kotlin-android plugin the app level build.gradle file like so:
apply plugin: 'kotlin-android'
And, the androidx.core and kotlin implementations under the implementation section like so:
implementation 'androidx.core:core-ktx:$'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
Similarly, I also added the following configs in the project level build.gradle file in the dependencies section, like so:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Source: React Native Project with Kotlin and Swift
Answered By - Innocent Oyebode
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.