简体   繁体   English

如何从Kotlin文件中的java文件调用class

[英]How to call class from java file in Kotlin file

I have created RN module to check if java and kotlin interoperability in RN.我创建了 RN 模块来检查 java 和 kotlin 在 RN 中是否互操作。 I was able to call kotlin class and methods in java but failed to call java in kotlin module.我能够调用 kotlin class 和 java 中的方法,但无法调用 kotlin 模块中的 java。

\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.下面是使用kotlin构建的RN模块代码,在Kotlin中调用java class。两者都在同一个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 中打开项目,但它未能同步 Gradle 并说要编辑 gradle 包装器属性,但该路径中不存在该项目,并且没有为其显示自动修复。

Android Studio console. 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 (Module) 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 (root project) 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 Java

package com.reactlibrarynativetoast2;

public class Demo {
    
}

kotlin 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.当我调用完整的 package 名称时,它也无法识别 package 名称,同时 kt 文件没有问题。 I can call methods and classes from kt files.我可以从 kt 文件中调用方法和类。

Probably, you can't use it because you haven't assigned a variable.可能,你不能使用它,因为你没有分配一个变量。

val demo = Demo()瓦尔演示=演示()

Or或者

var demo = Demo()变种演示=演示()

The Gradle version you are using is very old and that might be and issue for you.您使用的 Gradle 版本非常旧,可能是您的问题。 Update the Gradle version in order to prevent the issue.更新 Gradle 版本以防止出现此问题。 I faced the same issue a week ago and resolved it this way.一周前我遇到了同样的问题并以这种方式解决了它。

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.2' //update this to latest version
    }
}

Remove the entire buildscript block from your module's build.gradle .从模块的build.gradle中删除整个buildscript块。 You already have an appropriate recent version of the android build tools specified in your project's build.gradle .您已经拥有在项目的build.gradle中指定的 android 构建工具的适当最新版本。 There's no need to try to override that in the module's build.gradle .无需尝试在模块的build.gradle中覆盖它。

Incidentally, the version you're specifying in that module file (1.3.1) is ancient history, and jcenter() is obsolete.顺便说一句,您在该模块文件 (1.3.1) 中指定的版本是古老的历史,而jcenter()已过时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM