Search code examples
androidcordovaionic-frameworkcordova-pluginsionic4

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


I am working with Cordova 9.0.1 and ionic 5.4.14

I built a custom plugin using plugman and was able to successfully add it and call it from my ionic project with ionic cordova run android, verified by showing a toast which said "cheers!" (get it?)

So now I am working in Android Java by adding a 3rd party SDK for which I am trying to write a JavaScript bridge.

My project structure looks as follows:

MyProjectRoot
 -custom_plugins
   -MyCustomPlugin
     -src
       -android
         3rdPartySDK.aar
         mycustomplugin.gradle
         MyCustom.java
      -www
         MyCustom.js
       package.json
       plugin.xml
 -src
   USUAL-WEB-APP-STUFF-HERE
 ionic.config.json
 package.json

Remainder of project is not included for clarity, I believe these are the salient pieces.

The Cordova section of my package.json looks like:

"cordova": {
 "plugins": {
  "cordova-plugin-whitelist": {},
  "cordova-plugin-statusbar": {},
  "cordova-plugin-device": {},
  "cordova-plugin-splashscreen": {},
  "cordova-plugin-ionic-webview": {
    "ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
  },
  "cordova-plugin-ionic-keyboard": {},
  "cordova-plugin-mycustomplugin": {}
},
"platforms": [
  "android"
]
}

I have added the mycustomplugin.gradle with the following:

 dependencies {
  compile(name:'3rdPartySDK', ext: 'aar') {
    transitive = true;
  }
 }

The android section of the plugin.xml looks like this:

 <platform name="android">
    <source-file src="src/android/MyCustom.java"
                 target-dir="src/cordova-plugin-mycustomplugin/MyCustomPlugin"/>
    <config-file parent="/*" target="res/xml/config.xml">
        <feature name="MyCustomPlugin">
            <param name="android-package" value="cordova.plugin.mycustomplugin.MyCustomPlugin"/>
        </feature>
    </config-file>
    <framework custom="true" src="src/android/mycustomplugin.gradle" type="gradleReference" />
    <lib-file src="src/android/3rdPartySDK.aar"/>
</platform>

My Java file looks something like this:

package cordova.plugin.mycustomplugin;

import com.3rd.party.sdk.MobileAPI;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

public class MyCustom extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

 if (action.equals("initSDK")) {
        try {
            MobileAPI mobileAPI = MobileAPI.getInstance(cordova.getActivity().getApplicationContext());
            mobileAPI.destroy();
            mobileAPI.initSDK(2);
        }catch (Exception e) {
            e.printStackTrace();
         }
       }
 return false;
 }

Before trying to work with the 3rd party SDK, ionic cordova run android worked as expected.

Now, the cordova.cmd build android step fails with the message:

 Could not resolve all files for configuration ':app:debugCompileClasspath'.
 > Could not find :3rdPartySDK:.
 Required by:
 FAILED
  project :app

And I only got this far from a lot of Stack Overflow and search engine research.

What knowledge am I missing?


Solution

  • 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" />