安卓中集成UniMP(uni小程序)SDK的教程

2,082 阅读4分钟

下载

Android SDK 下载:nativesupport.dcloud.net.cn/UniMPDocs/S…

集成方法

打开Android Studio加载已有android项目或新建android项目。然后按照以下步骤配置您的项目。

添加基础依赖库及资源文件

解压uni小程序SDK包。得到uniMPSDK文件夹。 uniMPSDK文件夹目录结构说明:

image.png

image.png

image.png

|-- uniMPSDK/SDK	//uni小程序SDK
	|-- assets		// assets资源文件
	|-- Libs		//依赖库
	|-- res			// 资源文件
	|-- src			//微信分享支付需要的activity
	|-- AndroidManifest.xml //模块配置信息
	|-- proguard.cfg  //混淆配置(这个一会儿后面重点说明的)
/-- uniMPSDK/DEMO	//uni小程序SDK示例DEMO
	|-- libs //原生功能依赖库

uniMPSDK/SDK/Libs 依赖库说明

Libs文件夹中的依赖库是不用全部都集成到宿主项目中。可根据功能增删修改。具体可阅读 模块配置在线文档 参考修改添加等操作。

除视频、地图、分享、支付、登录、直播pusher等SDK,只集成基础模块如下:

image.png

uniMPSDK-V2-release.aar  //必须集成,uni小程序sdk引擎需要
uniapp-v8-release.aar //必须集成,uni-app引擎需要
breakpad-build-release.aar //必须集成,breakpad用于采集系统
base_oaid_sdk.aar //可选集成,用来获取设备的oaid唯一标示 注意(3.3.8版本的SDK及以下版本请集成oaid_sdk_1.0.25.aar)
android-gif-drawable-release@1.2.23.aar //必须集成

app/build.gradle文件 配置如下

如果你的项目是Kotlin DSL(build.gradle.kts)而不是Groovy DSL(build.gradle),那么看下一个

Groovy DSL 项目(build.gradle)
// 1: 我是Groovy DSL项目(build.gradle)
//必须配置
def mfph = [
    //宿主包名
    "apk.applicationId" : "xxx.xxx.xxxxx",
]
android {
	defaultConfig {
		targetSdkVersion 26 //最优26 2.8.11开始支持29~30
		ndk {
            abiFilters 'x86','armeabi-v7a',"arm64-v8a" //不支持armeabi
        }
		manifestPlaceholders = mfph
	}
	//此处配置必须添加 否则无法正确运行
    aaptOptions {
        additionalParameters '--auto-add-overlay'
        //noCompress 'foo', 'bar'
        ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
    }
}
//导入aar需要的配置
repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    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版本之后 必须集成,用来支持暗黑模式
}
Kotlin DSL 项目(build.gradle.kts)
android {  
    defaultConfig {
        // uniMP-Android 配置  
        ndk {  
            abiFilters.add("x86") //平板、模拟器(一般不需要,要的话,会增加apk大小)  
            abiFilters.add("armeabi-v7a")// 32位手机  
            abiFilters.add("arm64-v8a") //不支持armeabi 64位手机
        }  
        // 必须配置(uniMP-Android 配置)  
        val mfph: Map<String, String> = mapOf(  
            // 宿主包名  
            "apk.applicationId" to "com.withhim.cc"  
        )  
        // 必须配置(uniMP-Android 配置)  
        manifestPlaceholders.putAll(mfph)
    }
    aaptOptions {  
        additionalParameters += listOf("--auto-add-overlay")  
        ignoreAssetsPattern += listOf("!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~")  
    }
}

/**
 * 导入aar需要的配置(注意,咋们项目是Kotlin DSL,所以build.gradle.kts里不能使用repositories>flatDir>dirs,需要迁移代码,看如下注释说明)
repositories {// 这部分代码要迁移至项目根目录的settings.gradle.kts中,然后不需要repositories包裹,直接复制flatDir { dirs 'libs'} 这一段到settings.gradle.kts中,看下一步说明
    flatDir {
        dirs 'libs'
    }
}
*/

dependencies {
    // 以下为uniMP-Android必须导入的SDK相关依赖arr  
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar")))) //导入libs目录下的所有arr  
    implementation(fileTree(mapOf("include" to arrayOf("*.jar"), "dir" to "libs")))  
    // implementation(files("libs/xxx.aar")) //只想导入一个特定的 .aar 文件,可以直接使用文件路径:  
    // 以下为uniMP-Android的依赖-官方说必须集成:https://nativesupport.dcloud.net.cn/UniMPDocs/UseSdk/android.html  
    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版本之后 必须集成,用来支持暗黑模式
}

Kotlin DSL 项目 settings.gradle.kts 需要增加的代码
pluginManagement {  
    repositories {
        // 在这里增加以下代码
        flatDir {
            dirs 'libs'
        }
    }
}
dependencyResolutionManagement {  
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)  
    repositories {
        // 在这里增加以下代码
        flatDir {
            dirs 'libs'
        }
    }
}

// 图片如下所示

image.png

image.png

复制SDK资源到android项目目录

1、将assets目录放到你的Android项目app/src/main目录下

image.png

image.png

2、将proguard.cfg文件的代码全部复制到proguard-rules.pro文件里,不需要proguard.cfg文件 (作者第一次接入时,由于官方没说,只说把proguard.cfg文件放到app目录下,导致我这边运行小程序,提示:找不到uni小程序资源,经过折腾了老半天,才发现是因为新版本的Android中,混淆代码必须放在proguard-rules.pro文件里,所以我们现在要做的就是把proguard.cfg文件的代码复制到proguard-rules.pro文件里,这是关键一步)

image.png

image.png

4、AndroidManifest.xml配置

<application>
      <activity>
        这是你的入口activity
      </activity>
      // 在此处添加以下代码:
      <!-- uniMP-Android所需配置 start -->  
        <provider  
        android:name="io.dcloud.common.util.DCloud_FileProvider"  
        android:authorities="${apk.applicationId}.dc.fileprovider"  
        android:exported="false"  
        android:grantUriPermissions="true">  
        <meta-data  
        android:name="android.support.FILE_PROVIDER_PATHS"  
        android:resource="@xml/dcloud_file_provider" />  
        </provider>  

        <activity  
        android:name="io.dcloud.PandoraEntryActivity"  
        android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard"  
        android:hardwareAccelerated="true"  
        android:launchMode="singleTask"  
        android:permission="com.miui.securitycenter.permission.AppPermissionsEditor"  
        android:screenOrientation="user"  
        android:theme="@style/DCloudTheme"  
        android:windowSoftInputMode="adjustResize"  
        tools:ignore="IntentFilterExportedReceiver">  

        <intent-filter>  

        <category android:name="android.intent.category.DEFAULT" />  

        <category android:name="android.intent.category.BROWSABLE" />  

        <action android:name="android.intent.action.VIEW" />  

        <data android:scheme="h56131bcf" />  
        </intent-filter>  
        </activity>  
        <!-- uniMP-Android所需配置 end -->

</application>

以上步骤完成后,就可以测试启动小程序了,这时候文档直接看官方的即可。

初始化uniMPSDK

nativesupport.dcloud.net.cn/UniMPDocs/U…

启动内置uni小程序

nativesupport.dcloud.net.cn/UniMPDocs/U…

启动uni小程序携带启动参数

nativesupport.dcloud.net.cn/UniMPDocs/U…

启动uni小程序直达二级页面

nativesupport.dcloud.net.cn/UniMPDocs/U…