uni-app calls java code (uniapp hybrid development integrates native plug-in aar)
Article directory
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
2. Open the UniPlugin-Hello-AS project
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.
4. Modify build.gradle
Copy all the build.gradle content in uniplugin_module and overwrite the build.gradle content of mylibrary
5. Modify AndroidManifest.xml
Delete 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
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"
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#
- Use HBuilder to open the project where you want to integrate the plug-in
- Create the directory nativeplugins under the project
(名称不可改)
- Make changes in the directory nativeplugins according to the official format requirements
- 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:
2. Add local plug-in configuration in manifest.json
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.
4. Test run
Click "Run" in the menu bar -> "Run to phone or emulator" -> "Run to Android app" and select the custom base
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.