【发布时间】: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