Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Build using Gradle and MultiDex support

Discussion in 'Android' started by 3gogames, Aug 31, 2017.

  1. 3gogames

    3gogames

    Joined:
    Jul 28, 2015
    Posts:
    31
    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:

    http://answers.unity3d.com/questions/1301593/android-gradle-build-error.html

    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?
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,130
    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.
     
    LayalFakih likes this.
  3. 3gogames

    3gogames

    Joined:
    Jul 28, 2015
    Posts:
    31
    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.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,130
    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.
     
  5. 3gogames

    3gogames

    Joined:
    Jul 28, 2015
    Posts:
    31
    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.
     
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,130
    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).
     
  7. 3gogames

    3gogames

    Joined:
    Jul 28, 2015
    Posts:
    31
    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?
     
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,130
    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.
     
    Last edited: Sep 3, 2017
  9. 3gogames

    3gogames

    Joined:
    Jul 28, 2015
    Posts:
    31
    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.
     
  10. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,130
    Does it work with older Unity versions? i reverse engineered their code and could not find any references to that.
     
  11. 3gogames

    3gogames

    Joined:
    Jul 28, 2015
    Posts:
    31
    Yes, it works fine with Unity 5.6.3f1.
     
  12. LayalFakih

    LayalFakih

    Joined:
    Apr 22, 2017
    Posts:
    10
    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
     
    pmesnildrey likes this.