Cordova plugin development - adding aar
Solution 1
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' } }
Solution 2
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!!
SleepingMole
Young Developer for Adobe Air (Starling) and HTML5 Games
Updated on July 09, 2022Comments
-
SleepingMole almost 2 years
I am new to the cordova plugin development stuff. I want to write a plugin which is able to open a new android activty and showing some advertisement.
So I followed a simple tutorial here. That works very well and as expected.
Next step is to include this Android Studio Gradle project to my plugin.
My first try: Adding the gradle project to a subfolder of my cordova plugin and adding the following line to the plugin.xml file:
<framework src="libs/Broper/build.gradle" custom="true" type="gradleReference" />
also I tried:
<framework src="libs/Broper/app/build.gradle" custom="true" type="gradleReference" />
doesn't work. So I can't import the classes of that android studio project to my plugin java files.
Then a better solution (I thought so) is to add an AAR instead. But there I don't even have a clue what to do to add that AAR in my cordova plugin.
So, the question is: How do I add an android studio project (or library) to my cordova plugin the right way?
-
SleepingMole almost 9 yearsIs this for a AAR or a normal Android Studio Project? And what else is in the src/android dir? the whole gradl android Studio Project? Or the AAR? or thje "app" dir from the Android Studio Project?
-
Niko almost 9 yearssrc/android dir constains all things necessary for your plugin. You can put a whole gradle Android Studio project
-
SleepingMole almost 9 yearsThat answer confuses me a bit more. Cause if i put my Android Studio Project in there, i will orverwirte the build.gradle from that one with the "empty" one you providet. so i dont see the connection between the ASP and my plugin. Or is just dont understand somthething right.
-
Niko almost 9 yearsYou are right, I forgot some elements. I'll edit my answer ASAP
-
SleepingMole almost 9 yearsokay you changes helping. but one more question: in the build.gradle file dir "dirs" param is it relative to the gradlefile? is it "dirs '.' or dirs "" if the aar is in the same dir as the gradle file?
-
SleepingMole almost 9 yearsfinaly it works! one thing we forgotin the plugin config.xml: <resource-file src="libs/foo.aar" target="libs/foo.aar" />
-
Zarokka almost 9 yearsI can confirm that the <resource-file src="libs/foo.aar" target="libs/foo.aar" /> is also required. Note also that the flatDir path is the target path of that resrouce and not the src path.
-
Niko almost 9 yearsThanks for pointing the missing resource-file, I've updated answer. I hope this will save some people from troubles :-)
-
Khang Tran about 8 yearsThanks @Niko , you save my date.
-
ben_joseph over 7 years@Niko yourFile.gradle is this copied to the android project or merged with project level gradle file?
-
DaveAlden about 7 years@Niko how is
build-extras.gradle
copied from the plugin root folder to the android platform folder if it's not explicitly referenced in theplugin.xml
? Does Cordova implicitly look for this file and deploy itplatforms/android
? And if so, where does it copy it to? -
Aravindhan Gs over 5 yearsIn my case i have created .aar file and successfully integrated. But I'm getting error in .aar 's style attr error like ..\platforms\android\build\intermediates\res\merged\debug\values\values.xml :9: AAPT: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.NoActionBar'. ..\platforms\android\build\intermediates\res\merged\debug\values\values.xml :13:21-32: AAPT: No resource found that matches the given name: attr 'colorAccent'. but resources are found in my project
-
Aravindhan Gs over 5 yearsIn my case i have created .aar file and successfully integrated. But I'm getting error in .aar 's style attr error like ..\platforms\android\build\intermediates\res\merged\debug\values\values.xml :9: AAPT: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.NoActionBar'. ..\platforms\android\build\intermediates\res\merged\debug\values\values.xml :13:21-32: AAPT: No resource found that matches the given name: attr 'colorAccent'. but resources are found in my project
-
executor about 5 years@AravindhanGs were you able to solve the error you got? I'm getting a similar error.
-
Aravindhan Gs about 5 years@executor Finally i solved the above error by removing all colors and styles file in .aar and use all your class as Activity instead of AppCompatActivity. Use theme as android:theme="@android:style/Theme.DeviceDefault.NoActionBar" in manifest
-
axetroll about 5 yearsAfter several tests, mine worked with: dirs 'src/main/libs'
-
pritam001 almost 5 yearsI am still having an issue ERROR: Failed to resolve: :apprelease: Affected Modules: app
-
pritam001 almost 5 yearsI am having an issue ERROR: Failed to resolve: :apprelease: Affected Modules: app
-
Joshua F. Rountree over 4 yearsThis was required after upgrading to Cordova 9 for me. Was fine without
src/main
for some reason on Cordova 8 -
Pallavi Tapkir over 3 years@Sathis Kumar In your answer's first line you have mention 3 steps, 1. Cordova project creation 2. platform adding and 3. adding plugin. I have completed with first two steps will you please help me out how to add plugin ? I am not able to find any plugin.xml in project structure.