Cordova plugin development - adding aar
There has to be a slight modification to Niko's answer
When gradle runs the compilation using Niko's answer, it searches for the libs in app/libs
which is not present in the project (it does in the standard Android Studio project, but in Cordova Android project structure the libs folder is different). However the plugin copies the aar library to app/src/main/libs
which can make us think it is copying the aar to app/libs
<resource-file src="yourDirContainingYourAAR/foo.aar" target="libs/foo.aar" />
Hence the gradle file should be
repositories{
jcenter()
flatDir {
dirs 'src/main/libs'
}
}
dependencies {
compile(name:'foo', ext:'aar')
}
android {
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
This will give you a successful compilation of the app
Cheers!!
Okay, because there are multiple answers here and none of them fully capture everything that needs to be done, I'm going to create an all inclusive answer. Hopefully it will save people some time and frustration.
It's pretty straight forward, you just have to update plugin.xml
to state where the .aar file should go inside the android project. Then you have to tell gradle where to look for it.
So I have this directory structure (you can use a different one):
-- my-plugin/
|-- deps/
|-- some-dependency.aar
|-- src/
|-- MyPlugin.java
|-- www/
|-- plugin.js
|-- package.json
|-- plugin-settings.gradle
|-- plugin.xml
Step 1
The first step is to tell cordova where to put the .aar file. You do that by including a line in plugin.xml
:
<resource-file src="deps/some-dependency.aar" target="libs/some-dependency.aar" />
That will grab the file from the deps
folder in your source and stick it into the the platforms/android/app/src/main/libs/
folder in the resulting android project. Apparently the target
path is relative to the platforms/android/app/src/main
folder. Not sure if that can be changed.
Before moving on to step 2, it might be a good idea to run cordova build android
to verify that the .aar file is being copied to the right place.
Step 2
So once that's working the next step is to tell gradle where the file is so it can resolve it correctly during the build. You do that by creating your own .gradle file that will be merged with the main gradle file that cordova creates for you.
Create a file called plugin-settings.gradle
and put the following line in it:
repositories{
flatDir {
dirs 'src/main/libs'
}
}
dependencies {
compile(name:'some-dependency', ext:'aar')
}
Note that the line dirs 'src/main/libs'
tells gradle where to look for the .aar file. It does not dictate where cordova puts that file, just where gradle looks for it. Also note that the path is relative to the platforms/android/app/
folder and that the resulting path must match where cordova is putting the .aar file.
So in my plugin.xml
file I have target="libs/some-dependency.aar"
therefore in my plugin-settings.gradle
file I have dirs 'src/main/libs'
. If these don't line up you're going to get build errors.
Step 3
Now tell your plugin to use the .gradle file:
<framework src="plugin-settings.gradle" custom="true" type="gradleReference" />
Note that you can put the pllugin-settings.gradle
file where ever you want and you can call it whatever you want. Just make sure the <framework>
entry matches.
Yay
That's pretty much it. You should be able to build and run your app at this point. You're welcome.
Here's what I've done to use a gradle reference with a Cordova plugin, I think this might help you.
Global structure :
pluginFolder/
build-extras.gradle
plugin.xml
yourDirContainingYourAAR/
src/
android/
yourFile.gradle
myPlugin.java
Put your library, say foo.aar
, in the yourDirContainingYourAAR
directory (create it if needed)
In the
plugin.xml
file :<platform name="android"> <!-- your configuration elements, references, source files, etc... --> <framework src="src/android/yourFile.gradle" custom="true" type="gradleReference" /> <resource-file src="yourDirContainingYourAAR/foo.aar" target="libs/foo.aar" /> </platform>
In the gradle file
yourFile.gradle
:repositories{ jcenter() flatDir { dirs 'libs' } } dependencies { compile(name:'foo', ext:'aar') } android { packagingOptions { exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' } }
In the root folder of your plugin (same level as
plugin.xml
) create abuild-extras.gradle
. If needed, add or removeminSdkVersion
andtargetSdkVersion
according to your project needs :android { defaultConfig { minSdkVersion 16 targetSdkVersion 22 } packagingOptions { exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' } }