Uniapp - use Android native plugin
- 1. Development environment
- 2. Decompress the SDK compressed package
- 3. Import the UniPlugin-Hello-AS project and switch to project display
- 4. The provided demo file can be deleted
- 5. Create a new module and add components to the build.gradle file
- 6. Modify the test-module project file
- 7. Create a new UniTestModule.java
- 8. Compile the project and generate the arr package
- 8. Create a new uniapp project and add a new plugin directory to the directory
- 9. Put the compiled arr file into the android folder and configure the package.json file
- 10. The page imports the test-module module and uses it
- 11. Use Android native debugging project
-
- (1) Generate local packaged APP resources
- (2) Click the file directory after generation, copy the __UNI__***** folder
- (3) After copying the __UNI__***** folder, put it into the apps directory of the Android APP project
- (4) Log in to the DCloud Developer Center, find a new project in My Apps, or create an app
- (5) Configure offline packaging Key management
- (6) Android certificate signature SHA1 is generated and filled in
- (7) Put the generated file into the Android project, and configure build.gradle--signingConfigs--config
- (8) After saving, copy the Android App Key to replace the dcloud_appkey in the project AndroidManifest.xml
- (9) Modify the appid in the dcloud_control.xml file
- (10) Modify dcloud_uniplugins.json
- (11) Add the test-module component to the build.gradle file
- (12) Run the project
- 12. Uniapp real machine debugging
-
- (1) Configure local plug-ins, click: manifest.json file -- App native plug-in configuration -- select local plug-ins
- (2) Check the plug-in and confirm
- (3) Open the custom debugging base, click: release -- native APP-cloud packaging
- (4) Fill in the certificate information, check the custom debugging base, and pack
- (5) After the packaging is successful, connect the Android phone, click: Run -- run to the phone or emulator -- run to the Android App dock
- (6) Check Run Custom Dock -- Run
1. Development environment
- JAVA environment jdk1.8
- Android Studio download address: https://developer.android.google.cn/studio/index.html
- App offline SDK download: https://nativesupport.dcloud.net.cn/AppDocs/download/android
- HBuilderX download link: https://www.dcloud.io/hbuilderx.html
- Uniapp official documentation: https://nativesupport.dcloud.net.cn/NativePlugin/README
2. Decompress the SDK compressed package
3. Import the UniPlugin-Hello-AS project and switch to project display
4. The provided demo file can be deleted
remove build.gradle plugin
Delete settings.gradle settings
Refresh the project
Load the dependencies
5. Create a new module and add components to the build.gradle file
6. Modify the test-module project file
test-module – build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compileOnly fileTree(dir: 'libs', include: ['*.jar'])
compileOnly fileTree(dir: '../app/libs', include: ['uniapp-v8-release.aar'])
compileOnly 'androidx.recyclerview:recyclerview:1.0.0'
compileOnly 'androidx.legacy:legacy-support-v4:1.0.0'
compileOnly 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.alibaba:fastjson:1.1.46.android'
implementation 'com.facebook.fresco:fresco:1.13.0'
/*implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'*/
}
test-module – AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_module" />
After modification, rewrite and load the dependencies
7. Create a new UniTestModule.java
public class UniTestModule extends UniModule {
@UniJSMethod(uiThread = false)
public void testAsyncFunc(JSONObject options, UniJSCallback callback) {
if(callback != null) {
JSONObject data = new JSONObject();
data.put("code", "success");
callback.invoke(data);
}
}
}
8. Compile the project and generate the arr package
8. Create a new uniapp project and add a new plugin directory to the directory
9. Put the compiled arr file into the android folder and configure the package.json file
package.json file
{
"name": "test-module",
"id": "test-module",
"version": "1.0.0",
"description": "内置插件",
"_dp_type": "nativeplugin",
"_dp_nativeplugin": {
"android": {
"plugins": [{
"type": "module", //module 或 component类型
"name": "test-module", //注册名称 和后续uniapp项目模块导入名字一致
"class": "com.example.test_module.UniTestModule" //原生项目实体类完整名称
}],
"integrateType": "aar",
"abis": []
}
}
}
10. The page imports the test-module module and uses it
<template>
<view>
<button @click="handleTest">测试按钮</button>
<view>
返回信息:{
{
result}}
</view>
</view>
</template>
<script>
const testModule = uni.requireNativePlugin('test-module')
export default {
data() {
return {
result:""
}
},
onLoad() {
},
methods: {
handleTest(){
const param = {
name:"张三"}
testModule.testAsyncFunc(param,(res)=>{
this.result = JSON.stringify(res)
})
}
}
}
</script>
11. Use Android native debugging project
(1) Generate local packaged APP resources
(2) Click the file directory after generation, copy the __UNI__***** folder
(3) After copying the __UNI__***** folder, put it into the apps directory of the Android APP project
(4) Log in to the DCloud Developer Center, find a new project in My Apps, or create an app
DCloud Developer Center: https://dev.dcloud.net.cn/
(5) Configure offline packaging Key management
The android package name is consistent with ios: com.android.UniPlugin
(6) Android certificate signature SHA1 is generated and filled in
Generation step tutorial: https://blog.csdn.net/qq812457115/article/details/126011332
(7) Put the generated file into the Android project and configure build.gradle–signingConfigs–config
(8) After saving, copy the Android App Key to replace the dcloud_appkey in the project AndroidManifest.xml
(9) Modify the appid in the dcloud_control.xml file
(10) Modify dcloud_uniplugins.json
{
"nativePlugins": [
{
"plugins": [
{
"type": "module",
"name": "test-module",
"class": "com.example.test_module.UniTestModule"
}
]
}
]
}
(11) Add the test-module component to the build.gradle file
Reload the dependencies
(12) Run the project
12. Uniapp real machine debugging
(1) To configure local plug-ins, click: manifest.json file – App native plug-in configuration – select local plug-ins
(2) Check the plug-in and confirm
(3) Open the custom debugging base, click: release – native APP-cloud packaging
(4) Fill in the certificate information, check the custom debugging base, and pack