uni-app中如何打包WCDB SQLite数据依赖并作为插件使用?
uni-app中如何打包WCDB SQLite数据依赖并作为插件使用?
打包之后,使用会报错:
```javascript
callModuleMethod >>> invoke module:WCDB, method:testInsertUsers failed. java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.taobao.weex.bridge.MethodInvoker.invoke(Unknown Source:2)
at com.taobao.weex.bridge.NativeInvokeHelper.invoke(Unknown Source:134)
at com.taobao.weex.bridge.WXModuleManager.dispatchCallModuleMethod(Unknown Source:10)
at com.taobao.weex.bridge.WXModuleManager.callModuleMethod(Unknown Source:111)
at com.taobao.weex.bridge.WXBridgeManager.callModuleMethod(Unknown Source:85)
at com.taobao.weex.bridge.WXBridgeManager.callNativeModule(Unknown Source:89)
at com.taobao.weex.bridge.WXBridge.callNativeModule(Unknown Source:183)
at com.taobao.weex.base.SystemMessageHandler.nativeRunWork(Native Method)
at com.taobao.weex.base.SystemMessageHandler.handleMessage(Unknown Source:2)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:211)
at android.os.Looper.loop(Looper.java:300)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/tencent/wcdb/core/Database;
at cn.com.bjprdhd.wcdb.WCDBModule.testInsertUsers(WCDBModule.java:132)
at java.lang.reflect.Method.invoke(Native Method)
at com.taobao.weex.bridge.MethodInvoker.invoke(Unknown Source:2)
at com.taobao.weex.bridge.NativeInvokeHelper.invoke(Unknown Source:134)
at com.taobao.weex.bridge.WXModuleManager.dispatchCallModuleMethod(Unknown Source:10)
at com.taobao.weex.bridge.WXModuleManager.callModuleMethod(Unknown Source:111)
at com.taobao.weex.bridge.WXBridgeManager.callModuleMethod(Unknown Source:85)
at com.taobao.weex.bridge.WXBridgeManager.callNativeModule(Unknown Source:89)
at com.taobao.weex.bridge.WXBridge.callNativeModule(Unknown Source:183)
at com.taobao.weex.base.SystemMessageHandler.nativeRunWork(Native Method)
at com.taobao.weex.base.SystemMessageHandler.handleMessage(Unknown Source:2)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:211)
at android.os.Looper.loop(Looper.java:300)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.tencent.wcdb.core.Database" on path: DexPathList[[dex file "InMemoryDexFile[cookie=[0, -5476376631492974784]]", zip file "/data/app/~~3nYa-yaAau3070NTPnRgjA==/cn.com.bjprdhd.iot.GpZYJdrqETBAL5fBkS7Q==/base.apk"],nativeLibraryDirectories=[/data/app/~~3nYa-yaAau3070NTPnRgjA==/cn.com.bjprdhd.iot.GpZYJdrqETBAL5fBkS7Q==/lib/arm64, /data/app/~~3nYa-yaAau3070NTPnRgjA==/cn.com.bjprdhd.iot.GpZYJdrqETBAL5fBkS7Q==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
1 回复
在uni-app中打包WCDB SQLite数据依赖并作为插件使用,可以通过以下步骤实现。这里假设你已经熟悉uni-app的开发流程和原生插件的创建。
1. 准备WCDB SQLite依赖
首先,你需要获取WCDB SQLite的源码或者预编译库。WCDB是腾讯开源的一个高效的SQLite封装库,支持多种平台。
2. 创建uni-app原生插件
在uni-app项目根目录下创建native-plugins
文件夹,并在其中创建一个新的插件文件夹,例如my-wcdb-plugin
。
mkdir -p native-plugins/my-wcdb-plugin
在my-wcdb-plugin
文件夹中,创建以下文件结构:
my-wcdb-plugin/
├── Android/
│ └── src/
│ └── main/
│ ├── java/
│ │ └── com/
│ │ └── example/
│ │ └── mywcdbplugin/
│ │ └── MyWCDBPlugin.java
│ ├── res/
│ └── AndroidManifest.xml
├── iOS/
│ └── Classes/
│ └── MyWCDBPlugin.m
├── plugin.json
3. 实现Android插件
在Android/src/main/java/com/example/mywcdbplugin/MyWCDBPlugin.java
中:
package com.example.mywcdbplugin;
import android.content.Context;
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;
public class MyWCDBPlugin extends UniModule {
@UniJSMethod(uiThread = false)
public void openDatabase(String name, String version, UniJSCallback callback) {
// 使用WCDB打开数据库的逻辑
// ...
callback.invoke("Database opened successfully");
}
}
4. 实现iOS插件
在iOS/Classes/MyWCDBPlugin.m
中:
#import <Foundation/Foundation.h>
#import <UniApp/UniModule.h>
#import <WCDB/WCDB.h>
@interface MyWCDBPlugin : UniModule
@end
@implementation MyWCDBPlugin
- (void)openDatabase:(NSString *)name version:(NSString *)version resolve:(UniJSCallback *)resolve reject:(UniJSCallback *)reject {
// 使用WCDB打开数据库的逻辑
// ...
resolve(@"Database opened successfully");
}
@end
5. 配置plugin.json
在plugin.json
中配置插件信息:
{
"id": "my-wcdb-plugin",
"version": "1.0.0",
"name": "My WCDB Plugin",
"description": "A plugin for using WCDB SQLite in uni-app",
"platforms": {
"android": {
"package": "com.example.mywcdbplugin.MyWCDBPlugin",
"plugin": "src/main/java/com/example/mywcdbplugin/MyWCDBPlugin.java"
},
"ios": {
"class": "MyWCDBPlugin",
"header": "Classes/MyWCDBPlugin.h"
}
}
}
6. 在uni-app中使用插件
在manifest.json
中引入插件:
"nativePlugins": [
{
"plugins": [
{
"id": "my-wcdb-plugin",
"version": "1.0.0",
"provider": "wxxxxxxxxxx"
}
]
}
]
然后在页面中调用:
uni.requireNativePlugin('my-wcdb-plugin').openDatabase('test.db', '1.0', (res) => {
console.log(res);
});
这样,你就成功地在uni-app中打包并使用WCDB SQLite数据依赖作为插件了。