导入 SDK 包
- 在
android/app下新建libs文件夹,然后把 jar 包放进去。 - 在
android/app/build.gradle中添加引用
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation files("libs/xxx.xxx.xxx-release.jar")
implementation "com.google.code.gson:gson:2.8.0"
}
这样就导入 SDK 包了。
建立 flutter 和 SDK 的调用
本人项目Android 使用的是 kotlin,就已 kotlin 为例,Java 的可以参照官方给的例子。
建立调用的说明官方也有文档,其它平台可以参照官方文档。
- 引入你要调用SDK的方法或者类
package cn.com.xxx.xxx.xxx
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.lang.Exception
import cn.com.xxx.xxx.GetInfo;// 这个地方引入
- 在
MainActivity中添加相应的代码
class MainActivity: FlutterActivity() {
private val CYBERTECH_CHANNEL = "xxx.xxx.getInfo"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine){
super.configureFlutterEngine(flutterEngine)
// 试用 MethodChannel 建立连接
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CYBERTECH_CHANNEL).setMethodCallHandler {
call, result ->
if (call.method == "getInfo"){
try {
val info: String = GetInfo.getInfo(getApplicationContext)// 这个地方调用方法
result.success(info);
} catch (e: Exception){
result.error("UNAVAILABLE", "info not available", e)
}
} else {
result.notImplemented()
}
}
}
}
- 在 flutter 中调用,获取到数据
import 'package:flutter/services.dart';
const MethodChannel methodChannel = MethodChannel('xxx.xxx.getInfo');// 这个要和上面定义的 CYBERTECH_CHANNEL 一致
// 调用 xxx 获取信息
Future getInfo() async {
var info = await methodChannel.invokeMethod("getInfo");// 这个调用对应的 method
return info;
}
总结
通过上面的操作就能完成第三方 SDK 的接入了。当然还有EventChannel接入方式,这种一般是持续性的事件触发,例如监听 GPS 或者加速度传感器等,官方都有详细的例子。