Steps to create a native Android plugin for Unity in Java using Android Studio – Part 1 (of 2)

I’ve created a number of Android / Java plugins for Unity using Android Studio so thought I’d document the process.

Please let me know if there are better ways of doing any this 🙂

Update: Download Code for this

Assumptions:

  • I’m assuming that you already have Unity3D installed, if not, get it from here (Choose the most up to date version): Unity Downloads
  • I’m assuming you already have Android Studio and the relevant SDK’s installed?  If not you might want to start here and here.
  • I’m assuming you have a real Android device attached via USB or you are using the emulator and are used to building Unity Projects to your device. If not, you probably want to start here.

Fire up Android Studio and Create a new project…

  • Open Android Studio a choose File -> New -> New Project

 

Give your project a name and choose a location to save it. If you need C++ support, check that checkbox :-)….

(I’ve called my project “TextureHelper” but you can call yours whatever you wish.  I’ll hopefully be adding some more parts to the tutorial later that show how to update Unity Textures from Java/Android.. watch this space).

 

I normally use Unity on GearVR so I can safely choose API 22 as my minimum SDK – change this if you need to but remember what you set as you’ll need to set the same in Unity in Part 2 ….

 

We need to create a plugin without any activities…

 

This is what you should see once you’ve hit the finish button and Android Studio has created the project…

 

Locate the “build.gradle” file under “Gradle Scripts”. The first thing we need to do is change

apply plugin: ‘com.android.application’

…to…

apply plugin: ‘com.android.library’

This will ensure that Android Studio creates a .arr Android Archive file rather than a full Android application…

 

If you try to sync gradle at this point it complains….

 

This is easily fixed by removing the applicationId line from the defaultConfig section…. like so…

 

Now right click on your Java Namespace and create a new Java Class, we’re just going to create the traditional hello world…

 

Name your class….

 

Now add a simple static method with a String return type….

 

Next, open up the styles.xml file in res/values and comment out the whole AppTheme section (or delete it).  It’s not needed in Android Libraries (and it can cause issue, which took me a while to figure out using the Android Device Monitor and looking through logfiles.. so best to remove that potential issue now).

 

Then open the AndroidManifest.xml file under app/manifests….

 

Delete the unneeded android:icon, android:roundIcon and android:theme items…..

 

Save those files. then hit Build > Make Project

 

In Windows Explorer navigate to: app/build/outputs/arr and you should find your android archive that you can drop into Unity.

You can rename this file of course, or you can get gradle to automatically rename it for you (more on that later).

In Part Two, I’ll show how to add the plugin into Unity3D and use it 🙂

Continue to Part Two

  10 comments for “Steps to create a native Android plugin for Unity in Java using Android Studio – Part 1 (of 2)

  1. Mike
    August 13, 2018 at 8:11 pm

    Thanks for taking the time to make the tutorial, very helpful. One thing that surprises me for a ‘hello world’ plugin is the size of the arr (43MB). If it were .net/mono, it would be less than 10KB. There seems to be a lot of redundant libraries that Google is bundling with the functioning code.

    Cheers
    Mike

  2. Mike
    August 13, 2018 at 8:44 pm

    Scratch that, I just spotted the big dependency I accidentally included, It is 86KB.

    Thanks for the article
    Mike

    • admin
      September 11, 2018 at 2:33 pm

      You’re welcome 🙂 Hope it helps.

  3. September 30, 2018 at 4:28 pm

    thanks! very useful!

  4. petchan liu
    July 12, 2019 at 11:31 am

    Nice blog, very easy to follow step by step, really help me a lot, thanks !!!

    • admin
      August 8, 2019 at 12:12 pm

      You’re welcome. I probably need to refresh it a bit as it may be out of date now.

  5. GRINCH
    November 20, 2019 at 9:32 am

    finally someone wrote its in correct way ! u mate are one amazing motherfucker !

  6. Wayne
    March 3, 2020 at 12:26 pm

    Good stuff, Thanks!

  7. Mike
    May 27, 2020 at 8:17 pm

    Couple of years old now but I still found this tutorial very useful. Thanks!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.