Creating Android JAR and AAR plugins for Unity

Jon Keon
Karman Interactive
Published in
4 min readSep 23, 2016

Previously when we wanted to make an Android plugin for Unity, we were using Eclipse which had a very easy “right click and export as JAR” option for any Java code that you wrote.

Now that Eclipse is no longer supported, Android Studio is the IDE of choice and instead of JARs, AARs are the ideal output format for Android plugins.

We ran into a couple confusing aspects updating our workflow and we felt it best to log them here to gather feedback and help anyone else making the transition.

By default, Android Studio expects that you’re making an app to be run on Android. Since our intent is to create a JAR or AAR for use in a Unity project, we need to take a few extra step.

Creating a JAR or AAR plugin (Android Studio 2.1.3)

Create a new Android Studio Project like you normally would. Note that you can select any of the presets here, No Activity, Basic Activity etc, it really depends on the kind of plugin you’re trying to make.

You’ll see your Android tab populated like the image below once complete. The app module is what Android Studio thinks your APK will eventually be.

Next, you’ll want to create a new module for your JAR or AAR file. Right click and select New > Module.

Choose Java Library or Android Library and configure as needed for your project.

Once complete, you’ll have a new module in your Android Studio project.

You can then add in your custom Java code for both modules and in the case of an AAR module, you also add in other assets for packaging with your AAR file.

When you’re ready to compile, the easiest way to do this is to first select the Gradle task to build the JAR or AAR file.

For JAR Builds

Open up the Gradle menu, choose your JAR module, expand the Tasks and locate the jar task.

Double clicking on the jar task will build your module into a JAR file which can be found in your module’s build folder.

For AAR Builds

For the AAR file, open the Gradle menu, choose your AAR module, expand the Tasks and locate the build task.

Double click on the build task to build your module into an AAR file which can be found in your module’s build folder.

Importing a JAR or AAR package into your project

When building a JAR or AAR file for Unity, it’s usually because you’re wrapping an existing android library in order to expose its functionality to your Unity scripts. This means you’re going to need to link an existing JAR or AAR into your project.

With your project, you can again simply right click and select New Module and this time choose to import an AAR or JAR file.

This will end up creating a new module for the AAR or JAR that you imported. You now need your JAR or AAR’s module to reference this imported module so you’ll need to edit your module’s gradle file.

Add in the following line substituting the name of your newly imported module.

compile project(':<your imported jar or aar module>')

You can repeat this for as many JAR’s or AAR’s that you want to link with your JAR. Just don’t forget that these referenced JAR or AAR files need to be included in your Android build for Unity!

Originally published on a previous version of our site on September 23, 2016.

--

--