Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
793 views
in Technique[技术] by (71.8m points)

ionic framework - How to embed an Android library (AAR file) into a capacitor plugin?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Plugin folder showing all the files inside it

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.

Initial screen o android studio showing the option to open a project

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

Zoomed version of the files pane of Android Studio and gradle log showing that the project was successfully opened

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.

Menu showed by the action of right clicking the android java module

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.

Plus sign highlighted

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

new module screen showing the referred option

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

aar package imported

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

module added 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.

Plus sign of the declared dependencies highlighted

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

Result of the plus sign click

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.

add module dependency screen showing the dependencies to add

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.

module dependencies showing the new ones

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.

kotlin first steps to convert

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.

minimum kotlin implementation of 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.

Main activity of the app showing the add our newly created plugin

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: sdk add

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...