How to Add a Whale of Plugins to Your Android App
READ WHOLE ARTICLE
Is there a limit on how complex an android application can be? Most often, as a response, you can hear the opinion that such a restriction is the limitation of google play to the size of the added apk.
However, Multi-Programming Solutions has many popular applications, especially games, which have long overstepped this barrier and make no problems whatsoever. The point is that most of the space is occupied by resources (raster graphics, video and music), which are not necessary to be placed directly in the installation file (they can be placed in extension files or stored on their own servers, downloading as needed). Also if you want to have Log in function with Google, notifications and purchases, you should consider that you will meet the limitation in any way.
Can you imagine an Android game made in Unity that uses more than 64K Java methods? If your game exceeds the limit of 64K methods on a DEX file, you will have to poke around in your native plugins and / or build process. This post is an attempt to show different ways to solve the problem.
Understand your plugins
The most likely way to exceed this limit in Unity is to use native plugins. Native Android plugins are required in almost all Unity games. Unfortunately, some plugins are quite large. For example, Google Play Game Services itself contains almost 25K methods. This is a significant chunk of 64K that you are limited to.
Android Unity plugins usually consist of Unity C# code and native Android code and resources. Native code and resources are packaged either as an Android library project (Library Project), or as an Android Archive resource (AAR) in the Assets / Plugins / Android / directory. Project libraries are the old way to transfer components to the Android system, and AAR is a newer one. You will encounter plugins using both methods.
Classes in both project libraries and AAR exist in JAR files, which are simple zip files from compiled Java class files. The AAR file is also a simple zip of various Android resources. Some of them will become libs / *. Jar (also known as Java class archives). Library projects are simple directory structures, and the JAR, again, will be in libs / *. Jar.
Approaches of minimizing the number of methods
All the developers split up into two camps: those who still work with traditional approach and those who use new Unity features and mostly save their time. First, let’s take a look at the usual approaches of avoiding limitations.
The way to reduce the number of Java methods contained in the game’s APK using the standard Unity build system is to delete or modify the JAR files included with the native Android plugins. An alternative way is to export the Unity project as an Android project, in which more powerful technologies can be applied.
Try each of the following techniques in turn:
Google has broken Play Services into a set of modules. Use only those that you really need.
Use the Jar Jar Links tool with the zap rule to remove unnecessary classes from JAR files of plugins.
ProGuard is a tool used to obfuscate and remove unused classes and methods. MultiDex is a technology that allows you to use multiple DEX files in your APK, thus removing the limit of 64K methods in the game. Unity does not directly support these technologies, but you can use them by exporting a project as an Android project.
If nothing else helps, ProGuard can help lower the maximum limit. If this fails, use MultiDex. MultiDex has another limitation – it works only in API Level 14 (4.0) and higher. It is natively supported in Android (5.0) and higher. For versions 4.X you need to use support libraries. In addition, MultiDex has a list of known limitations.
If you need ProGuard or MultiDex, the first step is to export the Unity project as an Android project. If your project is complex enough, this in itself can be a daunting task. Most likely, it will also mean the unavailability of Unity Cloud Build.
However, with the right process, it may look like exporting to Xcode for iOS. After export, you need to set up an Android Studio or Gradle project, but this will be a one-time task. Re-exporting the project does not require new configuration of the Android assembly configuration.
The good side of this approach is that after setting up an Android Studio project, you can re-export a Unity project to the same directory. In this case, in general, an update of the Android Studio project is not required.
The bad side of this method is that the Android project will be associated with the files of the Android Studio project. Configuring dependencies will be challenging.
Gradle is an Android build system which makes a number of build processes automatic. This feature implies that numerous basic mistakes are less inclined to happen. Most strikingly in Unity, it decreases the methods reference, included in DEX (Dalvik Executable configuration) documents, implying that you are more averse to go over DEX limit issues. However, because of the distinctions between Gradle and the default Unity Android build system, some current tasks might be difficult to change over toGradle.
You can either build the output (APK) utilizing the Gradle construct system in Unity, or fare the Gradle task and assemble it in an outside apparatus (for example, Android Studio).
The Gradle feature gives you extra option to export your Unity project into Android Studio one if the Export Project option is checked. This option must be checked when Gradle is selected. Gradle option is generally helpful for the Android platform only. It can configure such issues as:
Another pro of Gradle is that it enables you to indicate your own gradle file to construct your Unity Project. Simply name the gradle file “mainTemplate.gradle” and place it in the <ProjectName>AssetsPluginsAndroid directory.
Beforehand, to utilize gradle file in your project, you should trade the Unity Project as Android Project then utilize an outsider programming like Android Studio or Eclipse to add gradle backing to your application. But now it is made that simpler. With upgrading Unity to version 5.5, you can now use Gradle to build for Android. Gradle is not as strict about errors compared with the existing Unity Android build system, meaning that some existing projects may be hard to convert to Gradle.
You would now be able to do that without sending out the project at all. It will save your time while building a mobile app and give you an opportunity to add as many apks as you need.
Building a complex Android app is a project which needs a professional view and a pack of experience. There were the best approaches to reduce the APK and, fortunately, now you have a better knowledge how to solve the most common Android problem. Multi-Programming Solutions deals with such issues daily and open to new ideas and tasks.