[英]How to call class from java file in Kotlin file
我创建了 RN 模块来检查 java 和 kotlin 在 RN 中是否互操作。 我能够调用 kotlin class 和 java 中的方法,但无法调用 kotlin 模块中的 java。
\RNNativeToastLibrary2Module.kt: (23, 7): Unresolved reference: Demo
下面是使用kotlin构建的RN模块代码,在Kotlin中调用java class。两者都在同一个package下。
我试图在 android 中打开项目,但它未能同步 Gradle 并说要编辑 gradle 包装器属性,但该路径中不存在该项目,并且没有为其显示自动修复。
Android 工作室控制台。
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(模块)
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(根项目)
// 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}")
}
}
当我调用完整的 package 名称时,它也无法识别 package 名称,同时 kt 文件没有问题。 我可以从 kt 文件中调用方法和类。
可能,你不能使用它,因为你没有分配一个变量。
瓦尔演示=演示()
或者
变种演示=演示()
您使用的 Gradle 版本非常旧,可能是您的问题。 更新 Gradle 版本以防止出现此问题。 一周前我遇到了同样的问题并以这种方式解决了它。
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2' //update this to latest version
}
}
从模块的build.gradle
中删除整个buildscript
块。 您已经拥有在项目的build.gradle
中指定的 android 构建工具的适当最新版本。 无需尝试在模块的build.gradle
中覆盖它。
顺便说一句,您在该模块文件 (1.3.1) 中指定的版本是古老的历史,而jcenter()
已过时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.