uni-app 原生插件制作,android端

发布于 1周前 作者 sinazl 来自 Uni-App

uni-app 原生插件制作,android端

开发环境 版本号 项目创建方式
Mac Mac 13.6.1 (22G313) HBuilderX

示例代码:

"dependencies": [
  "com.google.android.material:material:1.8.0",
  "androidx.constraintlayout:constraintlayout:2.1.4",
  "androidx.recyclerview:recyclerview:1.2.1",
  "androidx.legacy:legacy-support-v4:1.0.0",
  "androidx.appcompat:appcompat:1.0.0",
  "com.alibaba:fastjson:1.2.83",
  "com.facebook.fresco:fresco:1.13.0",
  "com.google.android.gms:play-services-location:18.0.0"
]
{  
  "id": "com.google.android.libraries.navigation:navigation",  
  "source": "implementation ('com.google.android.libraries.navigation:navigation:6.0.0',{\r\nexclude group: 'com.google.android.gms'\r\n})"
},
{
  "id":"com.google.android.libraries.places:places",  
  "source":"implementation ('com.google.android.libraries.places:places:3.3.0',{\r\nexclude group: 'com.google.android.gms'\r\n})"
}
dependencies {
  implementation project(path: ':uniplugin_component')
  implementation 'com.google.android.material:material:1.8.0'
  implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
  compileOnly fileTree(dir: 'libs', include: ['*.jar'])
  //uniapp配置
  compileOnly fileTree(dir: '../app/libs', include: ['uniapp-v8-release.aar'])
  compileOnly 'androidx.recyclerview:recyclerview:1.2.1'
  compileOnly 'androidx.legacy:legacy-support-v4:1.0.0'
  compileOnly 'androidx.appcompat:appcompat:1.0.0'
  implementation 'com.alibaba:fastjson:1.2.83'
  implementation 'com.facebook.fresco:fresco:1.13.0'
///ditu  
implementation ('com.google.android.libraries.navigation:navigation:6.0.0',{
  exclude group: 'com.google.android.gms', module: 'play-services-maps'
})
implementation ('com.google.android.libraries.places:places:3.3.0',{
  exclude group: 'com.google.android.gms'
})
implementation 'com.google.android.gms:play-services-location:18.0.0'

2 回复

插件依赖的库com.google.android.libraries.navigation:navigation:6.0.0和com.google.android.gms:play-services-maps:18.0.2冲突导致,关键错误信息如下: Duplicate class com.google.android.gms.maps.CameraUpdate found in modules jetified-navigation-6.0.0-runtime (com.google.android.libraries.navigation:navigation:6.0.0) and jetified-play-services-maps-18.0.2-runtime (com.google.android.gms:play-services-maps:18.0.2)
Duplicate class com.google.android.gms.maps.CameraUpdateFactory found in modules jetified-navigation-6.0.0-runtime (com.google.android.libraries.navigation:navigation:6.0.0) and jetified-play-services-maps-18.0.2-runtime (com.google.android.gms:play-services-maps:18.0.2)


在uni-app中开发原生插件可以极大地扩展应用的功能,特别是对于需要深度访问系统API或硬件功能的情况。以下是一个简单的示例,展示如何在Android端创建一个uni-app原生插件。

步骤1:创建插件项目

首先,在你的uni-app项目根目录下,创建一个native-plugins文件夹,并在其中创建你的插件目录,例如MyPlugin

uni-app-project/
├── native-plugins/
│   └── MyPlugin/
│       ├── android/
│       │   └── src/
│       │       └── main/
│       │           ├── java/
│       │           │   └── com/
│       │           │       └── example/
│       │           │           └── myplugin/
│       │           │               └── MyPlugin.java
│       │           └── AndroidManifest.xml
│       └── manifest.json

步骤2:编写Android原生代码

MyPlugin.java中,定义一个简单的插件类:

package com.example.myplugin;

import android.widget.Toast;
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class MyPlugin extends UniModule {

    @UniJSMethod(uiThread = true)
    public void showToast(String message, UniJSCallback callback) {
        Toast.makeText(mUniSDKInstance.getContext(), message, Toast.LENGTH_SHORT).show();
        if (callback != null) {
            callback.invoke("Toast shown");
        }
    }
}

步骤3:配置插件信息

manifest.json中,配置插件的基本信息:

{
  "id": "com.example.myplugin",
  "name": "MyPlugin",
  "version": "1.0.0",
  "description": "A simple uni-app native plugin for Android",
  "android": {
    "package": "com.example.myplugin",
    "class": "com.example.myplugin.MyPlugin"
  }
}

步骤4:在uni-app中使用插件

在uni-app的页面中,通过plus.bridge.exec方法调用原生插件:

export default {
  methods: {
    callNativePlugin() {
      plus.bridge.exec('MyPlugin', 'showToast', ['Hello from uni-app!'], (res) => {
        console.log(res); // Should log "Toast shown"
      });
    }
  }
}

总结

以上是一个基本的uni-app原生插件制作流程,通过定义Java类实现插件功能,并在uni-app中通过JS接口调用。根据实际需求,你可以进一步扩展插件的功能,例如访问设备传感器、文件系统或自定义视图等。记得在开发过程中测试插件在不同Android版本上的兼容性。

回到顶部