繁体   English   中英

如何从 kotlin 代码中访问 Java 类和 local.jar 文件的方法

[英]How to access the Java classes and methods of a local .jar file from kotlin code in flutter

我创建了一个 flutter 插件,我想在其中使用 local.aar 文件。 我的项目结构(android 部分)如下所示:

--android
  --gradle
  --libs
    --geth.jar
  --src
    --main
      --Kotlin code
  --build.gradle

所以要在我的插件中使用 a.aar 文件,首先我从这个链接下载了 geth.aar 文件:

https://geth.ethereum.org/downloads/

然后我将名称更改为 geth.jar(请注意仅通过更改名称来更改格式)然后我通过在插件的 android 文件夹中创建一个 libs 文件夹将其导入项目:

--android
  --gradle
  --libs <--!! here
    --geth.jar
  --src
    --main
      --Kotlin code
  --build.gradle

然后在依赖部分的 build.graadle 文件的末尾,我将其更改为:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation fileTree(dir: 'libs', include: ['*.jar']) <--!! this part was added

}

现在,我的问题是如何在 android 的 kotlin 文件中使用 this.aar (or.jar) 文件的类和方法? 在这里: --android --gradle --libs --geth.jar --src --main --Kotlin code <--:!here --build.gradle 我试过这个,但它给出了错误:

package com.example.flutter

import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import go.geth.gojni.Account; <--!! this is added
import go.geth.gojni.Geth; <--!! this is added
import go.geth.gojni.KeyStore; <--!! this is added
import go.geth.gojni.Node; <--!! this is added
import go.geth.gojni.NodeConfig; <--!! this is added


/** FlutterNPlugin */
class FlutterNPlugin: FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private lateinit var channel : MethodChannel

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter")
    channel.setMethodCallHandler(this)
  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    if (call.method == "getPlatformVersion") {
      NodeConfig nc = new NodeConfig();
      result.success("Android ${android.os.Build.VERSION.RELEASE}")
    } else {
      result.notImplemented()
    }
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }
}

它是无效的 Kotlin 语法,尝试替换这个:

NodeConfig nc = new NodeConfig();

这样:

val nc = NodeConfig()

暂无
暂无

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

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