我創建了 RN 模塊來檢查 RN 中的 java 和 kotlin 是否具有互操作性。我能夠在 java 中呼叫 kotlin 類和方法,但無法在 kotlin 模塊中呼叫 java。
\RNNativeToastLibrary2Module.kt: (23, 7): Unresolved reference: Demo
下面是使用 kotlin 構建的 RN 模塊代碼,并在 Kotlin 中呼叫 java 類。兩者都在同一個包下。
我試圖在 android 中打開該專案,但它無法同步 Gradle 并說要編輯 gradle 包裝器屬性,但路徑中不存在,并且沒有顯示自動修復。
Android Studio 控制臺。
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' }
}
}
爪哇
package com.reactlibrarynativetoast2;
public class Demo {
}
科特林
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}")
}
}
當我呼叫完整的包名時,它也無法識別包名,同時 kt 檔案沒有問題。我可以從 kt 檔案中呼叫方法和類。
uj5u.com熱心網友回復:
可能你不能使用它,因為你沒有分配一個變數。
驗證演示 = 演示()
要么
變種演示 = 演示()
uj5u.com熱心網友回復:
您使用的 Gradle 版本非常舊,可能對您來說是問題。更新 Gradle 版本以防止出現此問題。一周前我遇到了同樣的問題并以這種方式解決了。
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2' //update this to latest version
}
}
uj5u.com熱心網友回復:
從buildscript模塊的build.gradle. 您已經在專案的build.gradle. 無需嘗試在模塊的build.gradle.
順便說一句,您在該模塊檔案 (1.3.1) 中指定的版本是古老的歷史,并且jcenter()已過時。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/452592.html
