Home / Article / Gradle Tasks: Export an Android native plugin into Unity with gradle

Gradle Tasks: Export an Android native plugin into Unity with gradle

/
/
/
570 Views

I’m developing some Android Native Plugins for Unity and decided to implement some Gradle Tasks to export the plugin into Unity as a  .jar, .aar or a library android project.

Each native functionality is in a separate android module, as you can see in the picture. IMHO, this way it’s possible to import only the necessary modules into each project and it’s also easier to test, maintain and update (I would appreciate your input about this question).

Android Studio Project Structure
Android Studio Project Structure

I needed a Gradle Task that could be written once and used on each module as necessary. So all my tasks are inside the build.gradle file for the entire project.

This is the build.gradle file on the project root. You also have build.gradle files for each individual module.

Here is the complete build.gradle file. Next, we’ll see how to use it.

Gradle Tasks


The easiest way to use the new Gradle Tasks is selecting them on the Gradle Window.

Open the Gradle window on the right side of the screen or if it’s not present open it by selecting View – Tool Windows – Gradle.

Gradle Tasks on the Gradle Window
Gradle Tasks on the Gradle Window

Each one of the modules will have a group called plugins with the three tasks inside, exportAAR, exportJAR and exportLibrary. You can double-click on the task name to run it.

However, this will run the selected task on all modules!

This is not the intended behaviour when trying to export a single module into the desired project. So far, I was not able to understand why it happens when using the gradle window. So I’ve decided to run the tasks using the Terminal window.

Open the Terminal on the bottom of the screen or if it’s not present open it by selecting the View – Tool Windows – Terminal.

To run a task for a single module type the following:

./gradlew <modulename>:<taskname>

If the task succeeds you will have a BUILD SUCCESSFUL message on the Terminal Window.

Just as an example, the next image is the result when you export the same module using all the different tasks.

You need to choose only one of the methods when exporting your module.

Unity Project with the exported files
Unity Project with the exported files

To know more about Android Native Plugins, check my previous tutorial on the subject and to get really deep into it, I highly recommend this great tutorial by eppz.

I will post a later article on building each one of this modules and on building a Unity Project including plugins and resolving dependencies issues, merging manifests, using the Google Play Resolver, etc (I’m still fighting with all this).

Stay tuned 🙂

Happy Coding

Leave a Comment

Your email address will not be published. Required fields are marked *