Android 集成 UniMPSDK
1.下載并解壓uni小程序 SDK。
SDK文件結(jié)構(gòu)如下:
|-- SDK-Android@3.98-20231127
|-- DEMO //uni小程序SDK示例DEMO
|-- SDK //uni小程序SDK
|-- assets // assets資源文件
|-- libs //依賴庫
|-- res // 資源文件
|-- src //微信分享支付需要的activity
|-- proguard.cfg //混淆配置
小程序打包基座:
提示:盡量與 UniMPSDK 版本一致
HBuilderX: 3.9.8
vue-cli @dcloudio/uvm版本: 3.0.0-3090820231124001
2.復(fù)制SDK資源到android項(xiàng)目相應(yīng)的目錄中。
libs依賴庫配置
查看Flutter項(xiàng)目中android/app目錄中是否存在libs文件夾,沒有就創(chuàng)建。
將 SDK-Android@3.98-20231127/SDK/libs 中相應(yīng)的依賴復(fù)制到 項(xiàng)目 android/app/libs/
libs 文件夾依賴庫可根據(jù)功能需要進(jìn)行增加或刪除,除視頻、地圖、分享、支付、登錄、直播pusher 等 SDK,只集成以下基礎(chǔ)模塊就可使用:
uniMPSDK-V2-release.aar //必須集成,uni小程序sdk引擎需要
uniapp-v8-release.aar //必須集成,uni-app引擎需要
breakpad-build-release.aar //必須集成,breakpad用于采集系統(tǒng)崩潰日志
sqlite-release.aar
base_oaid_sdk.aar 必須集成 注意(3.3.8版本的SDK及以下版本請集成oaid_sdk_1.0.25.aar)
messaging-release.aar
iBeacon-release.aar
fingerprint-release.aar
contacts-release.aar
Bluetooth-release.aar
android-gif-drawable-release@1.2.23.aar //必須集成
assets 基礎(chǔ)資源配置
1,將SDK-Android@3.98-20231127/SDK/assets/ 目錄下的所有文件復(fù)制到 Flutter項(xiàng)目中android/app/src/main/assets/。(main目錄下沒有assets文件夾則自行創(chuàng)建)
2, Flutter項(xiàng)目中android/app/src/main/assets 目錄中創(chuàng)建 apps 文件夾,用于放置 uni小程序打包發(fā)行的程序。
在小程序項(xiàng)目的 manifest.json 內(nèi)獲取并使用 uni-app 應(yīng)用標(biāo)識(AppID),也是我們之后指定操作小程序的標(biāo)識。
vue-cli命令行打包:npm run build:app 生成本地包。
HBuilderX可視化界面打包:使用 【發(fā)行 => 原生App-本地打包 => 生成本地打包App資源】 生成本地包。
將打包好的uniapp小程序復(fù)制到apps文件下,apps文件夾結(jié)構(gòu)必須如下:
├── apps
│ ├── __UNI__xxxxxxx # 小程序1
│ │ └── www
│ │ │ └── ... #小程序編碼
│ ├── __UNI__xxxxxxx # 小程序2
│ │ └── www
│ │ │ └── ... #小程序編碼
proguard 混淆配置
將 SDK-Android@3.98-20231127/SDK/proguard.cfg 復(fù)制到Flutter項(xiàng)目中android/app/ 中。
3.修改android配置
修改MainActivity.kt配置文件
MainActivity.kt文件路徑:Flutter項(xiàng)目/android/app/src/main/kotlin/com/example/xxxx/MainActivity.kt文章來源:http://www.zghlxwxcb.cn/news/detail-853584.html
package com.example.xxxx //改成你自己的包名
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterFragmentActivity;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.Log
import io.dcloud.feature.sdk.DCUniMPSDK;
import io.dcloud.feature.sdk.Interface.IUniMP
import io.dcloud.feature.sdk.DCSDKInitConfig
import io.dcloud.feature.sdk.MenuActionSheetItem
import io.dcloud.common.adapter.util.Logger
class MainActivity: FlutterFragmentActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
val messenger = flutterEngine.dartExecutor.binaryMessenger
// Channel 對象
val channel = MethodChannel(messenger, "UniMP_mini_apps")
// Channel 設(shè)置回調(diào)
channel.setMethodCallHandler { call, res ->
// 根據(jù)方法名,分發(fā)不同的處理
when(call.method) {
// 打開指定的 UniMP 小程序
"open" -> {
try {
// 接收 Flutter 傳入的參數(shù)
val argumentAppID = call.argument<String>("AppID")
// 設(shè)置右上角膠囊操作菜單
val item = MenuActionSheetItem("關(guān)于", "about")
val sheetItems: MutableList<MenuActionSheetItem> = ArrayList()
sheetItems.add(item)
// 初始化uniMPSDK
val config = DCSDKInitConfig.Builder()
.setCapsule(true)
.setMenuDefFontSize("16px")
.setMenuDefFontColor("#2D2D2D")
.setMenuDefFontWeight("normal")
.setMenuActionSheetItems(sheetItems)
.build()
DCUniMPSDK.getInstance().initialize(this, config)
// 打開小程序
val unimp: IUniMP = DCUniMPSDK.getInstance().openUniMP(this, argumentAppID)
// 監(jiān)聽膠囊菜單點(diǎn)擊事件
DCUniMPSDK.getInstance().setDefMenuButtonClickCallBack { argumentAppID, id ->
when (id) {
"about" -> {
Logger.e(argumentAppID + "點(diǎn)擊了關(guān)于")
}
}
}
// 監(jiān)聽小程序關(guān)閉
DCUniMPSDK.getInstance().setUniMPOnCloseCallBack { argumentAppID -> Log.e("unimp", argumentAppID + "被關(guān)閉了") }
} catch (e: Exception) {
e.printStackTrace()
}
}
else -> {
// 如果有未識別的方法名,通知執(zhí)行失敗
res.error("error_code", "error_message", null)
}
}
}
}
}
修改build.gradle配置文件
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
android {
namespace "com.example.life_app"
compileSdkVersion flutter.compileSdkVersion
ndkVersion flutter.ndkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.life_app"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
//uni小程序sdk配置
ndk {
abiFilters 'x86','x86_64','armeabi-v7a','arm64-v8a' // 不支持armeabi
}
}
buildTypes {
release { //發(fā)布
// 注意點(diǎn):minifyEnabled 混淆和shrinkResources移除無用資源需同時(shí)為true或false,否則可能導(dǎo)致編譯失??!
minifyEnabled true //是否進(jìn)行混淆
shrinkResources true //刪除無用資源
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'proguard.cfg' //指定混淆規(guī)則文件
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug //設(shè)置簽名信息
}
}
//uni小程序sdk配置(此處配置必須添加 否則無法正確運(yùn)行)
aaptOptions {
additionalParameters '--auto-add-overlay'
//noCompress 'foo', 'bar'
ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
}
}
flutter {
source '../..'
}
// uni小程序sdk配置(導(dǎo)入aar需要的配置)
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
//uni小程序sdk配置(導(dǎo)入SDK相關(guān)依賴jar、aar)
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation fileTree(include: ['*.aar'], dir: 'libs')
//uni小程序sdk配置(必須添加的依賴)
implementation 'androidx.recyclerview:recyclerview:1.0.0' //必須集成,android 自帶recyclerview支持
implementation 'androidx.legacy:legacy-support-v4:1.0.0' //必須集成,androidx support支持
implementation 'androidx.appcompat:appcompat:1.0.0' //必須集成,androidx appcompat支持
implementation 'com.alibaba:fastjson:1.2.83' //必須集成,fastjson功能需要
implementation 'com.facebook.fresco:fresco:2.5.0'//必須集成,圖片加載需要
implementation 'com.facebook.fresco:animated-gif:2.5.0'//必須集成,圖片加載需要
implementation 'com.github.bumptech.glide:glide:4.9.0'//必須集成,圖片加載需要
implementation 'androidx.webkit:webkit:1.3.0' //3.6.15版本之后 必須集成,用來支持暗黑模式
}
4.Flutter項(xiàng)目中調(diào)用
Widget build(BuildContext context) {
const channel = MethodChannel('UniMP_mini_apps');
Future callNativeMethod(String appID) async {
try {
// 通過通道,調(diào)用原生代碼代碼的方法
final future = await channel.invokeMethod("open", {"AppID": appID});
// 打印執(zhí)行的結(jié)果
print(future.toString());
} on PlatformException catch (e) {
print(e.toString());
}
}
return Scaffold(
body: Center(
child: GestureDetector(
onTap: () async {
await callNativeMethod("__UNI__359FE36");
},
child: const Text('打開uni小程序'),
),
));
}
本文參考以下鏈接:
https://amoshk.top/2022050801/文章來源地址http://www.zghlxwxcb.cn/news/detail-853584.html
到了這里,關(guān)于Flutter集成UniMPSDK(Uni小程序SDK)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!