如何将 Android 库(AAR 文件)嵌入到电容器插件中? [英] How to embed an Android library (AAR file) into a capacitor plugin?

查看:24
本文介绍了如何将 Android 库(AAR 文件)嵌入到电容器插件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 已经解决了这个问题:

执行上述命令后,您生成的插件文件夹中将有一个 ios 和 android 文件夹.如果选中,请打开您的 android studio 并选择打开 android studio 项目的选项,选择 android 文件夹以完成此过程.您可以在初始屏幕上选择此选项,如下图所示.

项目打开后,如果一切顺利,您的 android studio 将显示这样的屏幕

现在,让我们添加插件文件.为此,请右键单击 android java 模块并选择打开模块设置"选项,如下图所示.

打开以下屏幕后,我们需要在此处添加一个包含 jar/aar 依赖项的模块.为此,只需点击上面显示的 + 号.

在新模块屏幕上,如果向下滚动,将出现Import .JAR/.AAR package",如上所示.

之后,找到你的aar包.结果如下.

点击完成,将出现下一个屏幕.如果没有,请在同一屏幕中点击应用按钮.

由于我的模块有一个依赖项,我将重复本教程的这一部分.

接下来,我们需要添加我们最近添加的模块作为android模块的依赖项.在同一屏幕上,单击依赖项选项和声明的依赖项"窗格的加号.

将显示以下屏幕.我们需要选择第三个选项,因为我们之前所做的是添加一个模块.

现在,让我们选择我们的模块.很简单,只需标记并单击确定即可.既然我必须这样做,如前所述,我会检查它们.

如果这一步进展顺利,您现在应该拥有一个多依赖项(或在我的情况下为两个)的 android 模块.下图显示了这一点.

点击应用并关闭此窗口.Gradle 将在后台重建您的插件项目,之后,由于我更了解 kotlin,我会将我的项目转换为 kotlin.如果您愿意,可以按照下图所示进行,但不是强制性的.

正确完成上述所有操作后,您现在可以在 TestPlugin.kt(或 .java,如果您没有使用 kotlin)上导入您的库.作为参考,下图显示了我的插件的最低实现.

在插件文件夹中,我们完成了.使用 npm 打包并安装在您的应用上.

接下来,为了使插件正常工作,我们需要在您将使用它的应用程序的 MainActivity 上添加它的类.

现在,为了使它起作用,我不能 100% 确定这是正确的方法,但既然它起作用了,我就这样做.我做的方法是通过脚本在应用程序的 android 模块上添加插件模块.我想我在 gradle 插件实现上遗漏了一些东西,但因为它有效......

为此,请在您的应用上访问 android/settings.gradle 文件并附加以下内容:

请注意,根据您所做的更改,settings.gradle 文件将被重写.执行脚本以始终保留信息或在构建之前检查它.

就是这样,如果您知道下一步的另一种方法,或者如果有更好的方法,请发表评论.而且,如果这个答案有用,请点赞,因为这个实现,在完成和工作后,让我有点哭了(当然是幸福的).

The issue is already answered for iOS: How to embed framework in capacitor plugin for iOS

For Android, I get issues when I add an aar file to the capacitor plugin by creating a new module. After publishing my capacitor plugin via npm and integrating it into an ionic app, the aar library cannot be found anymore.

解决方案

As done on iOs, we do practically the same thing here. But, using Android Studio and gradle instead of xcode and cocoapods. Ok, but first things first.

When the command npx @capacitor/cli plugin:generate is issued, what the CLI does is to start a folder containing a cocoa pod for iOs and a gradle plugin for Android. You can see this on the image below.

With the above command issued, there will be a ios and android folders inside you generated plugin folder. If this checks, open you android studio and select the option to open a android studio project, selecting the android folder to fulfil this procedure. You can select this option on the initial screen, like showed on the next image.

With the project opened, if all went well you'll se your android studio showing a screen like this

Now, lets add the plugin files. To do this, right click the android java module and select the "Open Module settings" option, as shown on the next image.

With the following screen opened, we need to add a module here that contains our jar/aar dependencies. To do that, just click on the + sign that is showed above.

On the new module screen, if you scroll down there will be a "Import .JAR/.AAR package", like shown above.

After that, locate you aar package. The result will be the following.

Click finish and the next screen will appear. If not, hit the apply button in this same screen.

Since my module has a dependency, I'll repeat this part of the tutorial.

Next, we need to add our recently added modules as dependencies of the android module. On the same screen, click on the dependencies option and on the plus sign of the "declared dependencies" pane.

The following screen will show. We need to select the 3rd option, since what we did early was adding a module.

Now, lets select our module. Its simple, just mark it and click ok. Since I have to, as said before, I'll check both of them.

If this step went well, you should have now you android module with one more dependency (or two in my case). The image bellow show that.

Hit apply and close this window. Gradle will rebuild your plugin project on the background and after that,since I know kotlin better, I'll convert my project to kotlin. If you wish, this can be done as shown on the next image, but is not mandatory.

With all the above done correctly, you can now import your lib on the TestPlugin.kt (or .java if you didnt use kotlin). For reference, the image bellow show the minimum implementation for my plugin.

In the plugin folder, we are done. Pack it using npm and install on you app.

Next, to the plugin works properly, we need to add its class on the MainActivity of the app that you'll be using it.

Now, to make it work, i'm not 100% sure that this is the correct way to do it, but since it works I do this way. The way I do is to add the plugin modules on the android module of the app via script. I think i am missing something on the gradle plugin implementation, but since it works.....

To do this, access, on your app, the android/settings.gradle file and append this:

Please note that depending on the changes that you do, the settings.gradle file will be rewritten. Do a script to keep the info always there or check it before build.

Thats it, please comment if you know another way to this next step or in any another if there is a better approach. And, if this answer is useful, please thumbs it up, since this implementation, when finished and working, made me cry a little (of happiness, of course).

这篇关于如何将 Android 库(AAR 文件)嵌入到电容器插件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆