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
- 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’
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 🙂
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.
Scratch that, I just spotted the big dependency I accidentally included, It is 86KB.
Thanks for the article
You’re welcome 🙂 Hope it helps.
thanks! very useful!
Nice blog, very easy to follow step by step, really help me a lot, thanks !!!
You’re welcome. I probably need to refresh it a bit as it may be out of date now.
finally someone wrote its in correct way ! u mate are one amazing motherfucker !
Good stuff, Thanks!
Couple of years old now but I still found this tutorial very useful. Thanks!