Uniapp - use Android native plugin

Uniapp - use Android native plugin

1. Development environment

2. Decompress the SDK compressed package

insert image description here

3. Import the UniPlugin-Hello-AS project and switch to project display

insert image description here

4. The provided demo file can be deleted

insert image description here

remove build.gradle plugin

insert image description here

Delete settings.gradle settings

insert image description here

Refresh the project

insert image description here

Load the dependencies

insert image description here

5. Create a new module and add components to the build.gradle file

insert image description here

insert image description here
insert image description here

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

insert image description here

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

insert image description here
insert image description here

8. Create a new uniapp project and add a new plugin directory to the directory

insert image description here

9. Put the compiled arr file into the android folder and configure the package.json file

insert image description here

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

insert image description here

(2) Click the file directory after generation, copy the __UNI__***** folder

insert image description here
insert image description here

(3) After copying the __UNI__***** folder, put it into the apps directory of the Android APP project

insert image description here

(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/

insert image description here

(5) Configure offline packaging Key management

The android package name is consistent with ios: com.android.UniPlugin

insert image description here

(6) Android certificate signature SHA1 is generated and filled in

Generation step tutorial: https://blog.csdn.net/qq812457115/article/details/126011332

insert image description here

(7) Put the generated file into the Android project and configure build.gradle–signingConfigs–config

insert image description here

(8) After saving, copy the Android App Key to replace the dcloud_appkey in the project AndroidManifest.xml

insert image description here
insert image description here

(9) Modify the appid in the dcloud_control.xml file

insert image description here
insert image description here

(10) Modify dcloud_uniplugins.json

insert image description here

{
    
    
  "nativePlugins": [
    {
    
    
      "plugins": [
        {
    
    
          "type": "module",
          "name": "test-module",
          "class": "com.example.test_module.UniTestModule"
        }
      ]
    }
  ]
}

(11) Add the test-module component to the build.gradle file

insert image description here
Reload the dependencies

(12) Run the project

insert image description here
insert image description here

12. Uniapp real machine debugging

(1) To configure local plug-ins, click: manifest.json file – App native plug-in configuration – select local plug-ins

insert image description here

(2) Check the plug-in and confirm

insert image description here

(3) Open the custom debugging base, click: release – native APP-cloud packaging

insert image description here

(4) Fill in the certificate information, check the custom debugging base, and pack

insert image description here
insert image description here

(5) After the packaging is successful, connect the Android phone, click: Run – run to the phone or emulator – run to the Android App dock

insert image description here

(6) Check Run Custom Dock – Run

insert image description here

Guess you like

Origin blog.csdn.net/qq812457115/article/details/127808751