Question

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?

Was it helpful?

Solution

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/

OTHER TIPS

Yes, you can use Android Studio to generate your jars from a Library. In my case the jar file was located very well hidden in the app build folder, under

<ProjectRootDir>/<AppModuleName>/build/exploded-aar/<ProjectName>/<LibraryModuleName>/unspecified/classes.jar

My example setup for this:

  • A app module containing nothing but a minimal Android App (Including a MainActivity)
  • A library module that contains the library code
  • Add a dependency from your app to your library e.g. add: compile project(':library')

If you build your app now it will also build the library and your apps build folder will contain the jar file under the location I explained above (in my case: PROJECT_ROOT/app/build/exploded-aar/SampleLibrary/library/unspecified/classes.jar).

You can create a jar file using Android Studio. Just add these tasks in your app's build.gradle file.

//task to delete the old jar
task deleteOldJar(type: Delete) {
    delete 'build/libs/AndroidPlugin.jar'
}

//task to export contents as jar
task exportJar(type: Copy) {
    from('build/intermediates/bundles/release/')
    into('release/')
    include('classes.jar')
    ///Give whatever name you want to give
    rename('classes.jar', 'AndroidPlugin.jar')
}

exportJar.dependsOn(deleteOldJar, build)

Once you add the above lines you can export the jar using the task of the same name. You can go through this post on TGC here on how to create an Android Plugin For Unity using Android Studio

Simple way:

  1. Create new Android Studio Project
  2. File > New > New Module and select Android Library
  3. Copy <UnityPath>\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar to <project path>\<Android Library Module name>\compileOnly (create this folder before)
  4. Add this jar as Library enter image description here
  5. Open this Module Settings enter image description here
  6. Set Compile Only enter image description here
  7. Build > Make Module <Android Library Module name>

Result

Jar-path: <project path>\<Android Library Module name>\build\intermediates\intermediate-jars\debug\classes.jar

Aar-path: <project path>\<Android Library Module name>\build\outputs\aar

Change apply plugin: 'com.android.application' to apply plugin: 'com.android.library'. In the build.gradle under app.

I'm not really sure about Android Studio but in ADT I use the export functionality by right-clicking on the project. With this you can create your .jar and place it wherever you want. Unity needs it to be on a specific folder to see it when running on a device (unfortunately I don't remember it well, but it should be something like Assets/Plugins/Android).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top