Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Android Studio be used to create Unity-plugin compatible JARs out of Library projects?

When using Unity3d, "native" (pure-java) Android functionality can be accessed via Plugin. As described in the documentation,

There are several ways to create a Java plugin but the result in each case is that you end up with a .jar file containing the .class files for your plugin

When I create an Android Application Project in eclipse, and set it is a library, it automatically generates a .jar file in the bin folder. I understand that this is a temporary file in the context of the Android toolchain, but this is the file that Unity needs in order to see the classes and build up it's own internal JNI magic. (The entire process is outlined very well here in case more clarity is needed on that process.)

Unfortunately, as near as I can tell, Android Studio does not generate this file. Is there a way I can tell it to give me this file, or perhaps some other way to generate code that Unity will be able to use?

like image 263
GrandOpener Avatar asked May 05 '14 22:05

GrandOpener


People also ask

Can we use Unity in Android Studio?

During the installation of the Unity Editor, make sure to include the Android Build Support module by checking the box next to it. Expand the Android Build Support module. If you are using Unity 2019 or later, add the Android SDK & NDK Tools module. On the Projects tab, click New to start a new Unity project.

How do I import an android studio project into unity?

In Unity open File | Build Settings... , check Google Android Project and click Export. Open Android Studio and click Import Project (Eclipse ADT, Gradle, etc.) Within the file selection dialog, navigate to the folder where you exported the project and select the folder named after your app.


1 Answers

With Unity 5.3+ you no longer need to build a .jar, instead you can use the .aar file that is automatically created by an Android Studio Android Library module. I found this a lot easier than getting the jar method to work:

  • In your Android Studio project, create an Android Library module: File > New > New Module > Android Library
  • make sure your minSdkVersion and targetSdkVersion match your Unity project setup
  • Add your classes etc to the module and build
  • A successful build should create a .aar file in build/outputs/aar/ e.g. my module creates mkunityplugin-release.aar
  • Open your Unity project, then drag the .aar file directly onto Assets/Plugins/Android in your Project view. This creates an asset which you can view in the inspector; it will show info on the plugin and the Android platform should be checked
  • Build your Unity project for Android. My first attempt failed because I had a different targetSdkVersion so I got a build error saying the manifests could not be merged; I fixed that in my Android Studio build.gradle and then it worked

This is where I first heard that .aar could be used directly https://www.reddit.com/r/Unity3D/comments/3eltjz/how_to_add_an_android_library_project_folder_to/

like image 151
mwk Avatar answered Oct 04 '22 16:10

mwk