Learn how to add an android java dependency for a cordova plugin using the framework tag on your plugin.xml file.

If you're developing the android part of a cordova plugin to add a feature written in java (another third party api that doesn't offer a Javascript or cordova friendly api), you'll need to add dependencies to your plugin in order to use them in your Main Java class of your plugin.

Instead of download the source code of the api (or source code of something), wrap it in a .jar file and add it (which won't work in most of the cases) to your plugin, you can make it more dynamical and maintainable adding your dependency in the plugin.xml file.

Adding a dependency

As specified in the Cordova documentation How to add a dependency , the preferred way to achieve this task is using the <framework/> tag.

In this case , we are going to add the following dependency to our project : AWS SDK For Java » 1.10.75 , this dependency can be used adding the identifier 'com.amazonaws:aws-java-sdk:1.10.75' to the gradle (as we are working with a plugin, with the framework tag). So all that we need to do , is to add inside our plugin.xml file in the android platform the following line:

<platform name="android">
     <framework src="com.amazonaws:aws-java-sdk:1.10.75" />
</platform>

Now the dependency will be added on the build, and you can use for example an import in your plugin Java class :

import com.amazonaws.services.s3; // Now we can use amazon things ...

public myJavaClass extends CordovaPlugin{

}

Now add the plugin to your project, and try to build. If the repository is registered in mavenCentral, everything should work good.

If you're out of luck today and your project doesn't compile, it may be a repository problem because it isn't hosted in mavenCentral, so please keep reading.

Problem with JCenter repositories

If your dependency has not been found while you compiled, it may be that you didn't add the correct repository in your gradle. For example, in this case we are going to use a JCenter repository example, the following project android OneDrive filepicker specifies that you need to use 'com.microsoft.onedrivesdk:onedrive-picker-android:v2.0' repository id to add the dependency to your project, however if you add it to your plugin as usual, the app won't compile, but why?

repositories {
  jcenter() // Note that you need to add jcenter repository !!! that's it
}

dependencies {
  compile ('com.microsoft.onedrivesdk:onedrive-picker-android:v2.0')
}

As you can see, the repository specifies that you need to add jcenter repository to your project, that will solve the issue and the app will compile as usual.

jcenter() is similar to mavenCentral(). Have a look at JCenter for more details. The jCenter guys claim that they have a better performance than Maven Central.

Add a custom gradle only for your plugin

You can create a custom graddle for your plugin without modify the original graddle file of the project. Create a graddle file (plugin.gradle) in your plugin with your custom content, in this case, if our plugin add a native filepicker, and we need access to the jcenter repository, the content of the plugin.gradle file should be:

repositories {
    jcenter()
}

dependencies {
    compile 'com.nononsenseapps:filepicker:3.1.0'
}

Finally, register the custom graddle in your plugin.xml file (change the path according to the location of the plugin.gradle file): 

<framework src="src/android/plugin.gradle" custom="true" type="gradleReference"/>

Now try to compile again and your library (retrieved from jcenter) should be available in your Java class.

Add repository manually

In case you want to modify manually the .gradle file (bad practice, but if you really need to add another repository that isn't maven without the custom gradle file because you're in a hurry).

Navigate to project/platforms/android and open build.gradle file. Approximately on the line 48 - 50 , you'll find the repositories property :

// Allow plugins to declare Maven dependencies via build-extras.gradle.
repositories {
 mavenCentral()
}

You'll find only the mavenCentral(), therefore you need to add jcenter() and everything will work as expected :

// Allow plugins to declare Maven dependencies via build-extras.gradle.
repositories {
 mavenCentral()
 jcenter()
}

Now try to compile again, then everything should work now. May the force be with you, have fun !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors