I have created RN module to check if java and kotlin interoperability in RN. I was able to call kotlin class and methods in java but failed to call java in kotlin module.
\RNNativeToastLibrary2Module.kt: (23, 7): Unresolved reference: Demo
Below are codes of RN module built using kotlin and calling java class in Kotlin. Both are under same package.
I tried to open the project in android but it failed to sync Gradle and says to edit gradle wrapper properties but that does not exist in the path and there is no auto fixing shown for it.
Android Studio console.
oid\build.gradle' line: 12
* What went wrong:
A problem occurred evaluating root project 'android'.
> Failed to apply plugin 'com.android.library'.
> Gradle version 2.2 is required. Current version is 6.8. If using the gradle wrapper, try editing the distributionUrl in C:\Users\ADMIN\.gradle\daemon\6.8\gradle\wrapper\gradle-wrapper.properties to gradle-2.2-all.zip
build.gradle (Module)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
}
}
apply plugin: 'com.android.library'
// add for kotlin
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"
apply plugin: "org.jetbrains.kotlin.android"
// plugins {
// id 'com.android.application'
// id 'org.jetbrains.kotlin.android'
// }
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
lintOptions {
abortOnError false
}
kotlinOptions {
jvmTarget = '1.8'
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.facebook.react:react-native: '
// added or kotlin. version varibale is from project level gradle
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
build.gradle (root project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.6.10'
ext {
kotlin_version = '1.6.10'
buildToolsVersion = "30.0.2"
minSdkVersion = 21
compileSdkVersion = 30
targetSdkVersion = 30
ndkVersion = "21.4.7075529"
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:4.2.2")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" //KSR
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
allprojects {
repositories {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
}
maven {
// Android JSC is installed from npm
url("$rootDir/../node_modules/jsc-android/dist")
}
mavenCentral {
// We don't want to fetch react-native from Maven Central as there are
// older versions over there.
content {
excludeGroup "com.facebook.react"
}
}
google()
maven { url 'https://www.jitpack.io' }
}
}
Java
package com.reactlibrarynativetoast2;
public class Demo {
}
kotlin
package com.reactlibrarynativetoast2
...
class RNNativeToastLibrary2Module(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
...
@ReactMethod
fun Say(str: String) {
Demo() // CALLING JAVA CLASS HERE...!
// also tried
// val v = Demo()
// var v = Demo()
// var d:Demo = Demo()
// val v = com.reactlibrarynativetoast2.Demo()
// Error: Unresolved reference: Demo
Log.d("ReactNative","in Kotlin module: ${str}")
}
}
When I call whith full package name it can't recognise the package name as well meanwhile there is no issue with kt file. I can call methods and classes from kt files.
CodePudding user response:
Probably, you can't use it because you haven't assigned a variable.
val demo = Demo()
Or
var demo = Demo()
CodePudding user response:
The Gradle version you are using is very old and that might be and issue for you. Update the Gradle version in order to prevent the issue. I faced the same issue a week ago and resolved it this way.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1' //update this to latest version
}
}