Tuesday, September 1, 2015

AAR file with PhoneGap/Cordova Plugin

STEPS TO CREATE AN PHONEGAP PLUGIN
Note: This steps are specific to include aar file, I assume you have created a phoneGap plugin before if not go here after that follow these steps.

Step 0: 
       This is just a structure of how your plugin folder should look like. 
pluginFolder/
  build-extras.gradle
  plugin.xml
  aar/yourAarFile.aar
  src/
    android/
      yourFile.gradle
      myPlugin.java
Step 1:
Create plugin folder with this sub-folders: srcwww and aar (if there is one).

Step 2:
Create these files in the plugin folder plugin.xmlbuild-extras.gradle and package.json (this is only need it if is going to be public).

Step 3: 
In your plugin.xml file insert the following lines:
<platform name="android”>
<config-file target="res/xml/config.xml" parent="/*">
             <feature name=“myPlugin.java” > <!—add this without the .java extension just the name —>
                 <param name="android-package" value=“myPlugin.java"/>
            </feature>
         </config-file>
<!-- your configuration elements, references, source files, etc... —>
<framework src="src/android/yourFile.gradle" custom="true" type="gradleReference" />
<resource-file src=“aar/yourAarFile.aar" target=“aar/yourAarFile.aar" />
 </platform>

Step 4:
In yourFile.gradle put the following lines:
repositories{
jcenter()
flatDir{
dirs ‘aar’
}
 }
dependencies {
compile(name:'yourAarFile', ext:'aar’)
 }
 android {
    packagingOptions {
      exclude 'META-INF/NOTICE'
      exclude 'META-INF/LICENSE'
    }
 }
Step 5:
 In build-extras.gradle put the following lines:
 android {
      defaultConfig {
          minSdkVersion 16
          targetSdkVersion 22
      }
     packagingOptions {
          exclude 'META-INF/NOTICE'
          exclude 'META-INF/LICENSE
  }
 }
Step 6:
 Create your java file myPlugin.java and put it in the src/android/ folder
Step 7:
 Now you can use the aar file from your plugin java Class. 

18 comments:

  1. But .class file is not found at runtime

    ReplyDelete
    Replies
    1. Can you describe the issue you are having a little more so I can try to help you.

      Delete
  2. please, where should i put the plugin folder from my root project folder ? I have www, hooks, platform, plugins folders and a file "config.xml".

    ReplyDelete
    Replies
    1. You should install the plugin you want using the terminal(mac) or command(windows), example:

      mac:
      1-go to your application folder from terminal and do
      sudo cordova plugin add ~/path/where/your/plugin/is

      That should do it for you. Hope it helps

      Delete
  3. Hi
    I have been given a .aar file as part of android sdk. I need to convert it to cordova plugin. Guide me

    ReplyDelete
    Replies
    1. Hi Vag, the first thing you need to do is follow this https://cordova.apache.org/docs/en/dev/guide/hybrid/plugins/index.html tutorial on how to do your plugin without the .arr file after that come here and follow this steps and if you have any more questions feel free to ask me. :D

      Delete
  4. i have done ionic 1 project, may i know where to put the myPlugin.java exactly, platforms/android or platforms/android/src/com/...

    ReplyDelete
    Replies
    1. Hi ombalakumar, this is a plugin and not an application project, in the plugin directory structure there is a path like this src/android/ and there is where your java file should go. Hope it help.

      Delete
    2. yes i got it already, anyways thanks for the fast reply, continue with your great work.

      Delete
    3. can you please elaborate the myPlugin.java file or post the coding in this example. It will be helpful for me and other developers too thanks.

      Delete
    4. Everything is working perfect but when i try to add plugin with aar file, i am getting
      Failed to install 'cordova-plugin-sdk':Error: ENOENT:
      no such file or directory, open 'C:\Users\user27\Documents\sdk_plugin_test\platforms
      \android\cordova-plugin-sdk\filename.aar\build.gradle'

      Delete
  5. Can you share github url to sample project

    ReplyDelete
  6. Add AAR file in ionic 3 application by creating custom cordova plugin

    at build time it show error


    ReplyDelete
  7. I've error "failed to resolve 'yourAarFile'".
    Can someone help?

    ReplyDelete
    Replies
    1. fix it changing dirs field to src/main/libs and using 'libs' as name folder instead of 'aar' (maybe this is the same):

      repositories{
      jcenter()
      flatDir{
      dirs 'src/main/libs'
      }
      }

      Delete
  8. How would you know if the AAR plugin is included inside the project?

    ReplyDelete
  9. How to Play Baccarat in the United States - Wolverione
    Baccarat is the first to make use of the fun88 vin traditional 52-card 1xbet korean deck of cards. 바카라 사이트 This deck of playing cards is also called the Blackjack.

    ReplyDelete