Cordova plugin development – adding aar

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="https://stackoverflow.com/questions/30757208/src/android/yourFile.gradle" custom="true" type="gradleReference" />
    
        <resource-file src="https://stackoverflow.com/questions/30757208/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 a build-extras.gradle.
    If needed, add or remove minSdkVersion and targetSdkVersion according to your project needs :

    android {
        defaultConfig {
            minSdkVersion 16
            targetSdkVersion 22
        }
    
       packagingOptions {
           exclude 'META-INF/NOTICE'
           exclude 'META-INF/LICENSE'
       }
    }
    

Leave a Comment