Cordova Plugin Development - Adding Aar

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="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 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'
    }
    }

How to import an android aar library in a cordova plugin

The framework property is iOS only in my experience at is also marked as an iOS property in the docs you linked.

As you already mentioned any user would expect the plugin to work without any setup after adding it which is why you don't want him to fiddle around in the platforms folder. To make this work you can extend cordovas gradle file. You have to add a framework tag to your plugin.xml which is of type gradleReference and links to your gradle extension (the path is relative to your plugin.xml):

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

The resulting gradle-extension will look something like this:

repositories {    
jcenter()
flatDir {
dirs 'libs'
}
}

dependencies {
compile(name: 'myNativeLib', ext: 'aar')
}

android {
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}

This assumes that you have put your .aar library in a subdirectory of your plugin named libs.

Add custom .aar library using AndroidX to Cordova plugin

Thanks to this documentation Flutter Doc I migrated my project to be compatible with Androidx. (Like Android Studio would have done using the Refactor > Migrate to AndroidX option) Yes, it's from Flutter and not Cordova, but since it's general information for any Android project, it still fits.

Main things I've done were:

  • Adding a file gradle.properties to platforms/android
android.enableJetifier=true
android.useAndroidX=true
  • Upgrading minSdkVersion from 15 to 19
  • Upgrading target & compile sdkVersion from 27 to 28
  • Updating gradle from 3.0.0 to 3.3.2

Since I created my project with Cordova CLI, I didn't think it would work like a normal Android project regarding those features, so I overlooked it.

I've ran some tests & the application seems to work OK now.

Edit #1

I had to solve that problem again, so I went through the steps that solved my problem before. It seems that the one that solved the issue was changing version in the platform/android/project.properties file from target=android-27 to target=android-28

How to correctly include 3rd party AAR file in Ionic Cordova build

For anyone else running across this here are the changes which fixed things for me:

  1. create a lib directory in the custom_plugins/MyCustomPlugin/src/android directory

  2. Move the aar file there

  3. Add compile(name:'3rdparty-sdk-4.1', ext: 'aar') to the dependencies section of the build.gradle file

  4. Add the following lines to android section of plugin.xml

     <source-file src="src/android/libs/3rdparty-sdk-4.1.aar" target-dir="libs"></source-file>
    <framework custom="true" src="src/android/build.gradle" type="gradleReference" />

cordova plugin add external .aar file (not .jar)

I found a working solution:

Add a new gradle file containing your dependency:

dependencies {
compile 'io.filepicker:filepicker-android:3.9.1'
}

In your plugin.xml, reference the extra gradle file:

<!-- android -->
<platform name="android">
...
<framework src="src/android/FilePickerIO.gradle" custom="true" type="gradleReference" />
</platform>

That's all.

Cordova Plugin - Use AAR

I was trying to figure out the proper build.gradle file that will do a proper build

Not sure what you mean by this, but the Gradle config should go in its own separate Gradle file which is included by the plugin, so you should have something like this:

plugin.xml:

...
<resource-file src="LibBlinkID.aar" target="libs/LibBlinkID.aar" />
<framework src="LibBlinkID.gradle" custom="true" type="gradleReference" />
...

LibBlinkID.gradle:

repositories{    
jcenter()
flatDir {
dirs 'libs'
}
}

dependencies {
compile (name:'LibBlinkID', ext:'aar')
}

android {
packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}

Note that I've changed repositories.flatDir.dirs from ./libs to libs.

Cordova App development - adding native aar

Plugins are created to perform native specific android/iOS/Windows tasks. For example if your hybrid app requires camera to set profile picture then you might need to write your own camera plugin or get existing one from github.

Now as you have added some aar file and you are planning to use some functionality present in that aar then you need to make your plugin class which should extend CordovaPlugin class and perform native specific operations in execute methos.

Answer to second question yes you need to make Java class for each plugin and you need to declear class location in pref.xml .

Can't start an activity from a cordova plugin using .aar files

nevermind, it was simple mistake. The library missed some imports and a theme. This fixed the issue.

Manifest:

android:theme="@style/Theme.AppCompat.Light"

Gradle:

dependencies {
compile(name:'mylittlelibrary-release', ext:'aar')
compile 'com.android.support:support-v13:23.1.0'
compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:gridlayout-v7:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'


Related Topics



Leave a reply



Submit