Android Plugin with AAR dependency, Android studio + gradle, external libs w/ resources

Hi all,

I’ve got an SDK which is exported as an AAR, the jar file is packaged with resources, the manifest etc etc

The SDK requires external dependencies as follows, which I’ve added to my App gradle build file (in addition to the default stuff):

    compile ‘com.android.support:support-annotations:23.2.0’

    compile ‘com.android.support:support-v4:23.2.0’

    compile ‘com.mcxiaoke.volley:library:1.0.19’

    compile ‘com.google.android.gms:play-services-ads:9.4.0’

I’ve got it running in a test app by importing the AAR file to the Corona Enterprise App template and having it as a dependency on my plugin which is using it, but when I need to package this as a plugin is AAR acceptable ? I’ve read on some old post that I need to provide the plugin jar + the external lib jars + the SDK jar I am using.

So if I understood it correctly, it would be the jar file output of the plugin project, downloads of the dependency jars mentioned above (read somewhere that I should not use the google play lib, and use some Corona reference somehow), and a jar of the SDK (which I currently have as an AAR, but I could not get it to work via extracting the jar and copying resources the App project under java, always fails with classnotfound string.R errors). 

Can someone verify my approach above please, and point out any obvious errors I am making (not adept at Java/Android)?

If AAR is not acceptable, can someone from Corona kindly provide steps on how to get around this ? I’ve reviewed alot of guides online and the Corona docs, but there is a mix of guidelines and the latest set which I found about Android Studio integration do not mention AAR, neither do the Project structure pages mention it(they only mention Corona libs and External libs, nothing about external lib resources and where they go). I was working on ANT originally and barely got to grips with that before Android Studio came in, so learning it again and using gradle, SO ANY HELP would be greatly appreciated so I can swiftly get the changes through.

Also unfortunately not many source plugins available on Corona repo use the new gradle build, which I could use as example (if someone can point me to an example that I can refer to would be ACE as well)

Thanks again

Hassaan

The Corona build system doesn’t support ‘aar’ files, so you’ll need to export your plugin as a jar from Android Studio.

Having said that, if you are only going to use this plugin yourself you can use ‘aar’ files as Android Studio supports it. You only need to export to ‘jar’ in case you want to submit your plugin to the Corona Marketplace for other non-Enterprise Corona developers to use.

To export the plugin to ‘jar’ make sure the build.gradle of your plugin contains the exportPluginJar task:

task exportPluginJar (type: Copy) { from "$buildDir/intermediates/bundles/release/" into "$buildDir/outputs/jar" include "classes.jar" rename "classes.jar", "${pluginName}.jar" doFirst { println "== exportPluginJar ==" } } tasks.withType(JavaCompile) { compileTask -\> compileTask.dependsOn("exportPluginJar") }

Also make sure you have a pluginName variable defined. We usually use the following at the top of the plugin’s gradle script to define the variable:

def manifest = new XmlSlurper().parse(file("src/main/AndroidManifest.xml")) def pluginName = manifest.@package.text()

To make sure you get the release version of the ‘jar’ you can issue the following commands in the plugin’s project folder from the command line:

./gradlew clean assembleRelease ./gradlew :plugin:exportPluginJar

After the build completes you should see the plugin jar located in plugin/build/outputs/jar

Thank Ingemar for pitching in, sorry I think I wrote too much and wasn’t clear enough or maybe I’m still missing a key point.

I want to submit the plugin to the marketplace so everyone can use it.

I get this much that AARs for the plugins are a no go atleast so have to create a jar. Can I use AARs for the external libs that the PLUGIN uses or I need to extract the jars from them also or find a way to get the jar if extract is wrong?

I can create a plugin jar just fine using the task you mentioned, but does the plugin jar need to INCLUDE the SDK jar as it is custom (i can extract the jar from the aar, but i dont know where to put the rest of the stuff in the SDK aar, manifest/assets/resources) ? Should I even be thinking about this ? I was under the impression I need to extract the aar and merge the jar with the plugin jar and provide the resources as is, but I haven’t been able to get this working.

Are you aware of any example on Corona bitbucket or git that I can look at which is using gradle+Android studio for a plugin that uses external libs that have custom resources ? Most I’ve seen use just code and resources are being taken via references I think e.g for facebook.

I want to submit the plugin to the marketplace so everyone can use it.

Unfortunately if your plugin has dependencies on more than one library that are only distributed as ‘aar’, then there’s a problem. We’ve stumbled upon the same issues while making plugins, and in these cases where there are multiple ‘aar’ files needed for the plugin to work, we’ve had to abandon the project.

However if there’s only one aar it may work.

When you package your plugin do the following:

  1. Rename ‘classes.jar’ in the aar to something useful and copy it to the ‘android’ folder

  2. Copy the ‘res’ folder from the aar and put it in a ‘resources’ folder under ‘android’ (you’ll end up with android/resources/res)

  3. In the resources folder create a file called package.txt

  4. Edit package.txt and enter the name of the package that the resources should be associated with. Like: com.something.somethingelse

Aha, ok fortunately I only have 1 AAR, so this should work. 

So if I understand it correctly, given the following directory structure (using the android studio integration guide and gradle),

Android/

      App/

             …

      Plugin/

             build.gradle

             src

             libs

      …       

     

I’ll be putting everything under Plugin dir, and it should become

      Plugin/

           build.gradle

           src

           libs

           resources/

                res/

                assets/

                package.txt (with com.mycompany.awesome which is package name of awesomeSDK correct?)

           awesomeSDK.jar

           

I assume you were referring to the build.sh project templates for the plugins using ANT under corona enterprise folder? Reckon the above will work as well or should I just go for the build.sh template instead ? Just setup gradle builds and Android studio, so wanted to continue with that if can.

Though with above I think will need to do something with the gradle scripts right for this to be picked up ? Or the default ones will just work?

No, the folders I’m taking about are not supposed to be in the Android Studio project. You keep the Android Studio project as-is.

The rest of the steps are something you’ll need to do manually to prepare the distribution.

The distribution folders are outlined here:

https://docs.coronalabs.com/native/plugin/submission.html

If you have Enterprise Unlimited, you can test your plugin by setting up a Self-Hosted Plugin which will use the same processes a developer will use when performing a Corona Simulator build with your plugin.

https://docs.coronalabs.com/native/hostedPlugin.html

Aha ok, thanks Ingemar, ill try that out n revert back with results later today

The client does not have enterprise unlimited so I guess ill have to submit the plugin for review directly and just structure it correctly. I’ll organize the files as you described, then lets see how it goes.

Great thread. Thanks @ingemar_cl for the clarification.  The Android Enterprise docs/guide really forgot to mention on how to handle AAR.

The Corona build system doesn’t support ‘aar’ files, so you’ll need to export your plugin as a jar from Android Studio.

Having said that, if you are only going to use this plugin yourself you can use ‘aar’ files as Android Studio supports it. You only need to export to ‘jar’ in case you want to submit your plugin to the Corona Marketplace for other non-Enterprise Corona developers to use.

To export the plugin to ‘jar’ make sure the build.gradle of your plugin contains the exportPluginJar task:

task exportPluginJar (type: Copy) { from "$buildDir/intermediates/bundles/release/" into "$buildDir/outputs/jar" include "classes.jar" rename "classes.jar", "${pluginName}.jar" doFirst { println "== exportPluginJar ==" } } tasks.withType(JavaCompile) { compileTask -\> compileTask.dependsOn("exportPluginJar") }

Also make sure you have a pluginName variable defined. We usually use the following at the top of the plugin’s gradle script to define the variable:

def manifest = new XmlSlurper().parse(file("src/main/AndroidManifest.xml")) def pluginName = manifest.@package.text()

To make sure you get the release version of the ‘jar’ you can issue the following commands in the plugin’s project folder from the command line:

./gradlew clean assembleRelease ./gradlew :plugin:exportPluginJar

After the build completes you should see the plugin jar located in plugin/build/outputs/jar

Thank Ingemar for pitching in, sorry I think I wrote too much and wasn’t clear enough or maybe I’m still missing a key point.

I want to submit the plugin to the marketplace so everyone can use it.

I get this much that AARs for the plugins are a no go atleast so have to create a jar. Can I use AARs for the external libs that the PLUGIN uses or I need to extract the jars from them also or find a way to get the jar if extract is wrong?

I can create a plugin jar just fine using the task you mentioned, but does the plugin jar need to INCLUDE the SDK jar as it is custom (i can extract the jar from the aar, but i dont know where to put the rest of the stuff in the SDK aar, manifest/assets/resources) ? Should I even be thinking about this ? I was under the impression I need to extract the aar and merge the jar with the plugin jar and provide the resources as is, but I haven’t been able to get this working.

Are you aware of any example on Corona bitbucket or git that I can look at which is using gradle+Android studio for a plugin that uses external libs that have custom resources ? Most I’ve seen use just code and resources are being taken via references I think e.g for facebook.

I want to submit the plugin to the marketplace so everyone can use it.

Unfortunately if your plugin has dependencies on more than one library that are only distributed as ‘aar’, then there’s a problem. We’ve stumbled upon the same issues while making plugins, and in these cases where there are multiple ‘aar’ files needed for the plugin to work, we’ve had to abandon the project.

However if there’s only one aar it may work.

When you package your plugin do the following:

  1. Rename ‘classes.jar’ in the aar to something useful and copy it to the ‘android’ folder

  2. Copy the ‘res’ folder from the aar and put it in a ‘resources’ folder under ‘android’ (you’ll end up with android/resources/res)

  3. In the resources folder create a file called package.txt

  4. Edit package.txt and enter the name of the package that the resources should be associated with. Like: com.something.somethingelse

Aha, ok fortunately I only have 1 AAR, so this should work. 

So if I understand it correctly, given the following directory structure (using the android studio integration guide and gradle),

Android/

      App/

             …

      Plugin/

             build.gradle

             src

             libs

      …       

     

I’ll be putting everything under Plugin dir, and it should become

      Plugin/

           build.gradle

           src

           libs

           resources/

                res/

                assets/

                package.txt (with com.mycompany.awesome which is package name of awesomeSDK correct?)

           awesomeSDK.jar

           

I assume you were referring to the build.sh project templates for the plugins using ANT under corona enterprise folder? Reckon the above will work as well or should I just go for the build.sh template instead ? Just setup gradle builds and Android studio, so wanted to continue with that if can.

Though with above I think will need to do something with the gradle scripts right for this to be picked up ? Or the default ones will just work?

No, the folders I’m taking about are not supposed to be in the Android Studio project. You keep the Android Studio project as-is.

The rest of the steps are something you’ll need to do manually to prepare the distribution.

The distribution folders are outlined here:

https://docs.coronalabs.com/native/plugin/submission.html

If you have Enterprise Unlimited, you can test your plugin by setting up a Self-Hosted Plugin which will use the same processes a developer will use when performing a Corona Simulator build with your plugin.

https://docs.coronalabs.com/native/hostedPlugin.html

Aha ok, thanks Ingemar, ill try that out n revert back with results later today

The client does not have enterprise unlimited so I guess ill have to submit the plugin for review directly and just structure it correctly. I’ll organize the files as you described, then lets see how it goes.

Great thread. Thanks @ingemar_cl for the clarification.  The Android Enterprise docs/guide really forgot to mention on how to handle AAR.