Can Android Studio Be Used To Create Unity-plugin Compatible Jars Out Of Library Projects?
Solution 1:
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/
Solution 2:
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).
Solution 3:
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 giverename('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
Solution 4:
Simple way:
- Create new Android Studio Project
- File > New > New Module and select Android Library
- Copy <UnityPath>\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes\classes.jar to <project path>\<Android Library Module name>\compileOnly (create this folder before)
- Add this jar as Library
- Open this Module Settings
- Set Compile Only
- 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
Solution 5:
Change apply plugin: 'com.android.application'
to apply plugin: 'com.android.library'
. In the build.gradle under app.
Post a Comment for "Can Android Studio Be Used To Create Unity-plugin Compatible Jars Out Of Library Projects?"