uni-app calls java code (uniapp hybrid development integrates native plug-in aar)

uni-app calls java code (uniapp hybrid development integrates native plug-in aar)


Preface

I was researching uniapp recently, and the leader had a request. To call java method in uniapp. In fact, the Java method is written in Android and finally packaged into an aar native plug-in, and then the aar plug-in is called in uniapp.

1. Development environment and documentation

1.Java environment download

I am using JDK1.8 here, configure the environment variables by yourself.
Official download address: https://www.oracle.com/java/technologies/javase/javase8u211-later-archive-downloads.html

2.App offline SDK download

Official download address: https://nativesupport.dcloud.net.cn/AppDocs/download/android.html#

3.AndroidStudio download

Official download address: https://developer.android.google.cn/studio/index.html

4.HBuilderX download

Official download address: https://www.dcloud.io/hbuilderx.html

5.uni-app extends native plug-in documentation

Official address: https://uniapp.dcloud.net.cn/plugin/native-plugin.html

6. Plug-in official example

Official address: https://ext.dcloud.net.cn/plugin?id=36

2. Package Java code into aar file

1. Download the decompression SDK

Download and decompress the App offline SDK

Insert image description here

2. Open the UniPlugin-Hello-AS project

Insert image description here

3. Create a new Module (Android Library)

Right-click the project name>New>Module>Android Library. Configure the Module name, package name, Android SDK version, etc.
Insert image description here
Insert image description here

4. Modify build.gradle

Copy all the build.gradle content in uniplugin_module and overwrite the build.gradle content of mylibrary
Insert image description here

5. Modify AndroidManifest.xml

Insert image description hereDelete androidTest and test under mylibrary path src

6. Write Java code

Code notes:
1. The class must inherit UniModule
2. Add the annotation @UniJSMethod(uiThread = false) to the method
3. The method must be a public method
4. The method return type must be returned in the form of JSONObject

Create a new class MyTest under the src package

Insert image description here

import com.alibaba.fastjson.JSONObject;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.common.UniModule;

public class MyTest extends UniModule {
    
    

    @UniJSMethod(uiThread = false)
    public JSONObject getTestData(){
    
    
        JSONObject object = new JSONObject();
        object.put("test_data","成功返回调用数据");
        return object;
    }
}

代码中涉及到com.sun.crypto.provider.SunJCE()报错问题,可在C:\Program Files\Java\jdk1.8.0_72\jre\lib\ext拷贝sunjce_provider.jar至libs中,并通过右键-add as library 引用即可

7. Package into aar file

Select mylibrary and package it through build-makemodule "*.mylibrary"
Insert image description here

After packaging, you can find the aar file in build>outputs

3. uni-app calls native plug-ins

1. Change project structure

Official document on native plug-in development format requirements: https://nativesupport.dcloud.net.cn/NativePlugin/course/package.html#

  1. Use HBuilder to open the project where you want to integrate the plug-in
  2. Create the directory nativeplugins under the project(名称不可改)
  3. Make changes in the directory nativeplugins according to the official format requirements
  4. Write package.json

package.json

{
    
    
	"name": "mylibrary-debug",
	"id": "mylibrary-debug",
	"version": "1.1.1",
	"description": "测试使用",
	"_dp_type": "nativeplugin",
	"_dp_nativeplugin": {
    
    
		"android": {
    
    
			"plugins" :[
				{
    
    
					"type": "module",
					"name": "mylibrary-debug",
					"class": "uni.dcloud.io.mylibrary.MyTest"
				}
			],
			"integrateType": "aar"
		}
	}
}

The final result is as follows:
Insert image description here

2. Add local plug-in configuration in manifest.json

Insert image description here

3. Pack a custom base

Click "Run" in the menu bar -> "Run to mobile phone or emulator" -> "Make Custom Base" to generate a custom base installation package.
Insert image description here

4. Test run

Click "Run" in the menu bar -> "Run to phone or emulator" -> "Run to Android app" and select the custom base
Insert image description here

5. Front-end code call

Use uni.requireNativePlugin to reference the plugin

	const PluginName = uni.requireNativePlugin(PluginName); // PluginName 为原生插件名称

Example:
Two different plug-ins are called here. The one called above is the demo of the article.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_43548590/article/details/132333148