如何将我的电容器插件导入Ionic android应用程序



我需要在我的Ionic应用程序中执行一些android原生代码,所以我尝试创建一个支持android的电容器插件。我使用本指南为我的Ionic应用程序添加了电容器:https://capacitor.ionicframework.com/docs/getting-started/with-ionic/.我根据这个指南创建了一个插件:https://capacitor.ionicframework.com/docs/plugins/

以下是我使用插件的代码:

// home.page.ts
import {Component} from '@angular/core';
import {Plugins} from '@capacitor/core';
@Component({
selector: 'app-home',
template: `
<ion-button (click)="onEchoBtnClick()">Submit</ion-button>
`
})
export class HomePage {
src: string | undefined;
constructor() {
}
onEchoBtnClick() {
const {Echo} = Plugins;
const echoPlugin = Echo;
echoPlugin.echo({
value: '333'
}).then(res => {
alert('ok ' + JSON.stringify(res));
}).catch(err => {
alert('err ' + err);
});
}
}

然后我构建Ionic应用程序:ionic build并运行:

npx cap copy
npx cap sync
npx cap open android

并在Android Studio中运行该项目。当我运行项目时,我会收到以下错误:CCD_ 2。我认为这是因为我需要在MainActivity类中添加插件:

// Other imports...
import com.example.myapp.EchoPlugin;
public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initializes the Bridge
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
// Additional plugins you've installed go here
// Ex: add(TotallyAwesomePlugin.class);
add(EchoPlugin.class);
}});
}
}

问题是我得到错误"packagecom.example.myapp.EchoPlugin不存在"。我的插件在"/echo"目录中。android项目在"/android"目录中。如何将Echo插件导入android项目?

我使用了这个[https://capacitorjs.com/docs/android/custom-code][1]

android/app/src/main/javacom/example/myapp/路径中,我在MainActivity.java的同一级别中创建了我的PluginClass

非常适合调用customFunction!。[1] :https://capacitorjs.com/docs/android/custom-code

我也遇到了同样的问题,我解决了从Android Studio重建项目的问题,这样它就可以获得插件的新编译配置。

转到Android Studio构建菜单并单击">制作项目">

最新更新