1

I have to build an aar module for Unity3D, but there are too many issues to do so.

My working environment

Unity3D: 5.6.f1
Android Studio: 2.3.3
build tools: 25.0.2
SDK tool: 24.4

I have built an aar file and unziped it to removed the classes.jar in libs, afterwards I copied the aar file and AndroidManifest.xml to asset\Plugins\Android. The package name of both, the android project and the unity project, are the same.

Building the APK File afterwards, resulted in the following exception:

IOException: Failed to Move File / Directory from 'Temp/StagingArea/android-libraries/app-debug/classes.jar' to 'Temp/StagingArea/android-libraries/app-debug/libs/classes.jar'.
UnityEditor.Android.PostProcessor.Tasks.ProcessAAR.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context)
UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry)
UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (BuildPostProcessArgs args)
UnityEditor.PostprocessBuildPlayer.Postprocess (BuildTargetGroup targetGroup, BuildTarget target, System.String installPath, System.String companyName, System.String productName, Int32 width, Int32 height, System.String downloadWebplayerUrl, System.String manualDownloadWebplayerUrl, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.BuildReporting.BuildReport report) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:186)
UnityEditor.HostView:OnGUI()

I've been looking for an answer for 3 days and I've tried everything I have found online, please help!

I'm pretty sure there is nothing inside the aar files libs folder

Nico
  • 1,727
  • 1
  • 24
  • 42
Wei
  • 11
  • 2
  • Two questions, 1.Why did you remove the classes.jar in libs from the aar file? 2.Why is the package name of both Android project and unity project are same? Before you reply, note that you don **not** need to extract the `AndroidManifest` file if the plugin is .arr. You only need to if it is a .jar plugin. So don't modify the .arr file. Just put it in your `Assets/Plugins/Android` folder. – Programmer Jun 28 '17 at 13:33
  • 1.almost every article about this issue says that Unity will add it's classes.jar automatically to aar file's libs folder, so if you don't remove it, it will cause some problems. 2. I've noticed that there is a AndroidManifest file in the aar, but lots articles says that I have to copy it. I'v tried your way, build an aar file and copy to Assets/Plugins/Android, the issue is still. – Wei Jun 28 '17 at 14:16
  • 2.You don't have to if it's an arr plugin. In noticed that you are using Unity's classes.jar in your plugin. Is there any reason you included it in your Android Project? You don't need it if you don't use `UnityPlayer.UnitySendMessage` function. Also can you create a new project and try this again with the way I mentioned. – Programmer Jun 28 '17 at 23:36
  • Also, check out my answer – Programmer Jun 28 '17 at 23:58

2 Answers2

5

Find your Build.gradle file from Android Studio, look for any command that bundles the classes.jar file.

Depending on how your Android Studio project is generated, it should look like this:

compile files('libs/jars/classes.jar')

or

compile fileTree(dir: 'libs', include: ['*.jar'])

Just replace the "compile" with "provided" keyword

provided files('libs/jars/classes.jar')

Or

provided fileTree(dir: 'libs', include: ['*.jar'])

This will make Android Studio exclude the .jar plugin from the final arr plugin build. You can now build the plugin again and transfer it into the Assets/Plugins/Android folder. I suggest you do so with a new project to make sure there is no duplicate anywhere.

You can also go the Module Settings by right-clicking on the classes.jar plugin-in and set it to Provided in the Dependencies tab.

Note:

You don't need to manually add the AndroidManifest.xml file to the Unity project because this will automatically be done by Unity if this is an arr plugin. You only need to do so if this is a jar plugin. Although, if you still run into permission problems then do that manually.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 2
    This works perfectly and elegantly. Thank you. In newer gradle version, we can now use `compileOnly` to avoid `Configuration 'provided' is obsolete and has been replaced with 'compileOnly'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html` warning. – ming060 Dec 07 '18 at 08:54
0

You just need to unzip the classes.jar file inside Assets/Plugins/Android and call whatever function you need by an object of AndroidJavaClass or AndroidJavaObject.

Only the .jar file is needed if you want to access the classes in the android-module. I've used this technique numerous times.

PS: Unity takes care of merging manifests automatically with Google's manifest merger tool

Suraj S
  • 1,019
  • 7
  • 18