Build using Gradle and MultiDex support

Hi,

we’re using Unity 5.6.3f1 and we have an Android project that uses the following plugins:

FacebookSDK 7.10.0
Heyzap 9.12.5 integrated with AdMob, AppLovin, Chartboost, Vungle and UnityAds
GameAnalytics

Recently we have switched to Gradle build to avoid the DEX-limit error returned by the default Internal build. After some conflict resolutions, all was ok.

We’ve tried to add Fabric Crashlytics to the project, and now we have the following error with Gradle:

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:transformClassesWithDexForRelease’.

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

I’ve found this interesting thread:

Here they suggest to Export an Android Proect using Gradle, copy build.gradle file, enable multidex and save it under Plugins/Android/mainTemplate.gradle.

This approach seems to solve the problem, but is it a safe solution?

If you’re already building using Gradle, why should you export to Android studio and build your project there?
With Unity, you can define your custom build.gradle file (place that under Plugins/Android) and i think it should work.

Please note that Multidex is not a recommended thing from what i know. In our project, we used ProGuard to overcome the dex method ref count.

2 Likes

We always build using Unity (and Gradle). I’ve exported the project only in order to copy and edit build.gradle file and use it as mainTemplate.gradle.

I’ve tried to enable ProGuard using

minifyEnabled true

but I’ve a lot of build error related to Heyzap. I suppose that each library (Facebook, Heyzap, etc) should specify a set of rules for ProGuard, but they don’t.

How do you handle ProGuard with third-party libraries? We have no idea on what classes must be kept.

It’s a bit of an art and a mix of expertise and black magic. Please note that i do provide professional consulting services and i plan to offer a new service for configuring Gradle + ProGuard. You can PM me if you’re interested in that, as it might save you some time and pain resolving this.

Ok, I’ll do some test and if I will in trouble I’ll contact you. Thanks!

I’ve only a question: where should I put a custom proguard rules file? I can’t find this detail in Unity documentation.

The option to add your own provided file was only added in Unity 2017.1 if I recall.

You have this option under the Player Settings (where you can also set a custom Gradle file).

Really? We’re using Unity 5.6.3f1. There’s no way to manually edit our mainTemplate.gradle and add a custom proguard rules file?

The only option i’m aware of with 5.6.3 is a customized gradle file. You just place it under Assets/Plugins/Android/mainTemplate.gradle. There’s no UI or anything fancy for that under Player Settings.

Starting with 2017.1, they’ve enabled support for this under Player Settings.

After a lot of tests I’ve found that you can create a file named ‘proguard-user.txt’ and it will be copied into the temporary Android project during the build process. So, if you must specify some custom rules for ProGuard you can use that file.

Does it work with older Unity versions? i reverse engineered their code and could not find any references to that.

Yes, it works fine with Unity 5.6.3f1.

you can use the mainTemplate.gradle in unity without exporting the project , unity added a checkbox and checking it creates a mainTemplate Gradle for you to use. player settings → publishing settings → custom Gradle template

1 Like