Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Building an Android project with gradle

Discussion in 'Android' started by philhindlept, May 22, 2017.

  1. philhindlept

    philhindlept

    Joined:
    Dec 8, 2016
    Posts:
    8
    Hi,

    I am working on a project which has an Android version. As we've updated SDKs we're now hitting the 65536 method DEX limit in Android. (https://developer.android.com/studio/build/multidex.html). We therefore want to build the game using Gradle as this should allow us to use either multidex or ProGuard to fix this problem.

    I'm new to this particular project, Android build systems, Android Studio, and Gradle however I've used Unity for long enough to be reasonably competent. However my limited knowledge of the Android-specific build systems is cause me some problems. I've searched extensively but haven't found any solutions. I'll explain what I've done and the problem I am now facing.

    * Using Unity 5.6.0f3
    * Using Android Studio 2.3.2
    * java -version reports java version "1.8.0_131"
    • I exported the Android project from Unity 5.6 Build Settings window as Gradle (New).
    • Then imported this Gradle project into Android Studio 2.3.2
    • AS showed a variety of error messages, mostly relating to AndroidManifest.xml collisions, so I edited these xml files (they're in the project folder), until those errors were resolved.
    • I also had edited several build.gradle files in the exported Gradle project where it says: classpath 'com.android.tools.build:gradle:2.1.0', and changed 2.1.0 to 2.3.2, because of the recommendation on this page : https://developer.android.com/studio/releases/gradle-plugin.html
    • Making those changes eventually resulted in Android Studio reporting "BUILD SUCCESSFUL"
    • I then switched back to Unity
    • Go to File->Build Settings.. window and hit the 'Build' button. (Build System is still set to 'Gradle (New)' as it was when I exported the project from this window).
    • I am then presented with an error message as follows:
    CommandInvokationFailure: Gradle build failed.
    C:/Program Files/Java/jdk1.8.0_121\bin\java.exe -classpath "C:\Program Files\Unity5.6.0\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-2.14.jar" org.gradle.launcher.GradleMain "assembleRelease"

    stderr[

    FAILURE: Build failed with an exception.

    * Where:
    Build file 'C:\Sourcetree\game_Android\Unity_Game\Temp\gradleOut\build.gradle' line: 19

    * What went wrong:
    A problem occurred evaluating root project 'gradleOut'.
    > Failed to apply plugin [id 'com.android.application']
    > Minimum supported Gradle version is 3.3. Current version is 2.14. If using the gradle wrapper, try editing the distributionUrl in C:\Sourcetree\game_Android\Unity_Game\Temp\gradleOut\gradle\wrapper\gradle-wrapper.properties to gradle-3.3-all.zip

    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    ]

    So it looks like Unity is trying to use the version of Gradle that ships with Unity, which is in : C:\Program Files\Unity5.6.0\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib
    In this \lib folder, there are a bunch of .jar files, version number 2.14, so I'm assuming that when Unity is building, it's using that embedded version of Gradle rather than any other versions on my PC.

    I then changed the distributionUrl in the gradle-wrapper.properties file as suggested in the error message, so now in the exported Gradle project folder, my gradle-wrapper.properties now has a line reading:

    distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

    That makes no difference and building again gives the same error.

    Can anyone suggest how I can solve this?

    Thanks.
     
  2. philhindlept

    philhindlept

    Joined:
    Dec 8, 2016
    Posts:
    8
    So another, simpler way to phrase my question I guess is : Is it possible to ask Unity to use a different installed version of Gradle, other than the version which is installed into the Unity installation folder?
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    I don't think you can tell Unity which version of Gradle to use. That might work after some serious struggling, but you can just spend that time on doing what you'd like to do -- building your game for Android.

    I've switched our game recently from the Internal (ADT) legacy build system to Gradle (with ProGuard) to overcome the dex count issue. We build the game directly from Unity (not via Android Studio).

    I can share a few tips and pointers on how to set that up, or i can also help you with that (paid) to get a working build. you can PM me if you'd like that.
     
  4. K_Bay

    K_Bay

    Joined:
    May 2, 2017
    Posts:
    13
    You should instruct gradle to put your code in two dex classes. It is possible, google play services are packed in 5 dex files. 65536 limit is per dex. One APK can have many dex.
     
  5. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Just be aware that multi dex == larger APK download size.
    We chose to go with ProGuard to avoid the 65536 limit issue.
     
  6. philhindlept

    philhindlept

    Joined:
    Dec 8, 2016
    Posts:
    8
    Thanks liortal - I reached the same conclusion, and I guess Unity comes bundled with a fixed version of Gradle for good reason when I thought about it. But thank you for your help.

    I did manage to get the exported Gradle project building in Android studio, just had to ensure that when building in Android Studio, I was using the same version of Gradle as Unity uses. That gave me some errors, which I fixed, so I'm able to build from Unity now, but with the Unity Gradle builds turned on!