全部產品
Search
文件中心

Mobile Platform as a Service:Flutter 工程接入操作指南

更新時間:Nov 12, 2024

前提條件

  • Android Studio(最新版)的 mPaaS 外掛程式版本升級到 3.0.230524 及以上。

  • Android Studio 開啟 Flutter 工程中的 android 目錄作為 gradle 專案,並使用 mPaaS 外掛程式進行接入。

    P1.png

設定環境

  1. 在 Flutter 專案中使用 mPaaS 外掛程式,請參考 將 mPaaS 添加到您的專案中

  2. 設定 project 的 build.gradle 檔案。

    apply plugin: 'com.alipay.apollo.optimize'
    buildscript {
        ext.mpaas_artifact = "mpaas-baseline"
        ext.mpaas_baseline = "10.2.3-21"
        ext.kotlin_version = '1.6.10'
        repositories {
            google()
            mavenCentral()
            maven {
                url 'https://mvn.cloud.alipay.com/nexus/content/repositories/releases/'
                name 'alipay'
                credentials {
                    username 'mvn_read_ws'
                    password 'mrk8929'
                }
            }
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:7.1.3'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath 'com.android.boost.easyconfig:easyconfig:2.8.0'
        }
    }
    
    allprojects {
        repositories {
            google()
            mavenCentral()
            maven {
                url 'https://mvn.cloud.alipay.com/nexus/content/repositories/releases/'
                name 'alipay'
                credentials {
                    username 'mvn_read_ws'
                    password 'mrk8929'
                }
            }
        }
    }
    
  3. 設定 module 的build.gradle檔案。

    dependencies {
        implementation platform("com.mpaas.android:$mpaas_artifact:$mpaas_baseline")
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
        implementation 'com.mpaas.android:cdp'
        implementation 'com.mpaas.android:push'
        implementation 'com.mpaas.android:logging'
        implementation 'com.mpaas.android:lbs'
        implementation 'com.mpaas.android:media'
        //uc核心
        implementation 'com.mpaas.android:uccore'
        //容器
        implementation 'com.mpaas.android:nebula'
        //小程式
        implementation 'com.mpaas.android:tinyapp'
        implementation("androidx.multidex:multidex:2.0.1")
    }
  4. AndroidManifest.xml 添加如下配置。

    <meta-data android:name="com.mpaas.cdp.autoInit" android:value="true" />

初始化 mPaaS

package com.example.mpaas_demo

import android.content.Context
import android.util.Log
import androidx.multidex.MultiDex
import com.alipay.mobile.framework.quinoxless.QuinoxlessFramework
import com.alipay.mobile.nebula.provider.H5AppCenterPresetProvider
import com.alipay.mobile.nebula.util.H5Utils
import com.mpaas.mas.adapter.api.MPLogger
import com.mpaas.nebula.adapter.api.MPTinyHelper
import com.mpaas.tinyappcommonres.TinyAppCenterPresetProvider
import io.flutter.app.FlutterApplication

class MyApplication : FlutterApplication() {
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        Log.i("Android", "MyApplication attachBaseContext")
        MultiDex.install(this)
        QuinoxlessFramework.setup(this) {
            // 初始化小程式公用資源套件
            H5Utils.setProvider(
                H5AppCenterPresetProvider::class.java.name,
                TinyAppCenterPresetProvider()
            )
            //設定虛擬網域名稱
            val tinyHelper = MPTinyHelper.getInstance()
            tinyHelper.setTinyAppVHost("mpaas.com")
            //設定白名單
            MPLogger.setUserId("mPaaSTest")
        }
    }

    override fun onCreate() {
        super.onCreate()
        Log.i("Android", "MyApplication onCreate")
        QuinoxlessFramework.init()
    }
}