【问题标题】:How to access the Java classes and methods of a local .jar file from kotlin code in flutter如何从flutter中的kotlin代码访问本地.jar文件的Java类和方法
【发布时间】:2021-05-29 22:10:31
【问题描述】:

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

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

所以要在我的插件中使用 .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 文件中使用这个 .aar(或 .jar)文件的类和方法?这是在这里: - 安卓 --gradle --库 --geth.jar --src - 主要的 --Kotlin 代码

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)
  }
}

【问题讨论】:

  • 你遇到了什么错误?
  • @fartem Classifier 'NodeConfig' 没有伴随对象,因此必须在此处初始化 FAILURE: Build failed with an exception。

标签: java android flutter kotlin


【解决方案1】:

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

NodeConfig nc = new NodeConfig();

通过这个:

val nc = NodeConfig()

【讨论】:

  • 它现在给出这个错误:D8:程序类型已经存在:go.Seq$GoObject com.android.builder.dexing.DexArchiveMergerException:合并 dex 档案时出错:程序类型已经存在:去。 Seq$GoObject
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 2016-12-31
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
相关资源
最近更新 更多