11

Unity has a default gradle.properties file that gets added during the build process. While its possible to change the build.gradle and the settings.gradle files as mentioned here https://docs.unity3d.com/Manual/android-gradle-overview.html there is no mention of being able to change gradle.properties within the unity docs. The file also gets recreated every build attempt so editing it within the temp/gradleOut after a build and building again doesn't work. I know exporting the project is possible as well, but I'm looking for a solution where the project can be run directly from unity.

Btw this question is NOT a duplicate of this question How to use Gradle in Unity The answer here has nothing to do with modifying the gradle.properties file.

This is a duplicate of this question that got incorrectly marked as a duplicate how to change default gradle.properties of Unity?

ChristianToMidiOut
  • 111
  • 1
  • 1
  • 3
  • 2
    Welcome to StackOverflow. I think so your question doesn't suit on this site. In my opinion, it should be asked on the [Game Development](https://gamedev.stackexchange.com) website – Gourav Jan 14 '19 at 17:12
  • Has this been asked somewhere else? – Yuri Heupa Jan 14 '19 at 19:18

5 Answers5

15

Maybe my answer is a bit outdated but in Unity 2020 you can do it in:

Player Settings -> Tab Android (with robot obviously) -> Publishing Settings -> Custom Gradle Properties Template (checkbox).

After enabling the checkbox you will see the path to gradleTemplate.properties (usually it appears in Assets/Plugins/Android directory) file which will be merged with final gradle.properties.

Everything you need you can write to the end of file after **ADDITIONAL_PROPERTIES** string.

Example:

org.gradle.jvmargs=-Xmx**JVM_HEAP_SIZE**M
org.gradle.parallel=true
android.enableR8=**MINIFY_WITH_R_EIGHT**
**ADDITIONAL_PROPERTIES**

android.useAndroidX = true // I added this property to fix error:  This project uses AndroidX dependencies, but the 'android.useAndroidX' property is not enabled. Set this property to true in the gradle.properties file and retry.

Also on screenshot:

How to modify gradle.properties file in Unity

Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54
12

This was something that was slightly hard to discover. I was going to do a regular post build processor like I had for my iOS build, but as I was searching for a manner to load and determine where the properties file was, I ran across the following interface in the documentation : IPostGenerateGradleAndroidProject.

According to the documentation:

Implement this interface to receive a callback after the Android Gradle project is generated.

So below is my initial brute force implementation for turning on androidX and jetifier.

public class AndroidPostBuildProcessor : IPostGenerateGradleAndroidProject
{
    public int callbackOrder
    {
        get
        {
            return 999;
        }
    }


    void IPostGenerateGradleAndroidProject.OnPostGenerateGradleAndroidProject(string path)
    {
        Debug.Log("Bulid path : " + path);
        string gradlePropertiesFile = path + "/gradle.properties";
        if (File.Exists(gradlePropertiesFile))
        {
            File.Delete(gradlePropertiesFile);
        }
        StreamWriter writer = File.CreateText(gradlePropertiesFile);
        writer.WriteLine("org.gradle.jvmargs=-Xmx4096M");
        writer.WriteLine("android.useAndroidX=true");
        writer.WriteLine("android.enableJetifier=true");
        writer.Flush();
        writer.Close();

    }
}

Theoretically you should be able to manipulate the generated gradle project in any manner to your choosing during the post build processor. Some additional tools might be helpful, like the PBXProject support on iOS, but until then, this will do.

JDL
  • 1,477
  • 21
  • 31
  • where do i put this file ? – Lena Bru Dec 25 '19 at 05:23
  • 1
    @LenaBru I believe you can place it anywhere you want, but since editor extensions generally go in a folder called "Editor" that is where I have placed mine. You can read up on the build pipeline and editor extensions here: https://docs.unity3d.com/Manual/AdvancedEditor.html – JDL Jan 06 '20 at 19:20
  • maybe you want to add `writer.WriteLine("org.gradle.parallel=true");` to that – Roberto Feb 18 '20 at 23:58
6

IPostGenerateGradleAndroidProject is a new Interface added after Unity2018.

As my project based on Unity2017, it's not a good solution. Then I found this. A solution with Gradle.

([rootProject] + (rootProject.subprojects as List)).each {
    ext {
        it.setProperty("android.useAndroidX", true)
        it.setProperty("android.enableJetifier", true)
    }
}
王剑虹
  • 61
  • 1
  • 3
0

Although this is not a perfect solution, you can use the "Export Project" option.

Build Settings

After exporting the project, you can modify gradle.properties and build using AndroidStudio or command line.

Heejae Kim
  • 519
  • 1
  • 4
  • 14
  • 1
    Not really useful. The poster explicitly mentions the solution is not sustainable. Just think how many times you would have to export the project, modify the file and install the APK to a device during development. – Clinkz May 26 '20 at 07:31
0

In the newer Unity versions (2019.4+) it is possible to generate a custom gradle properties template by going to Project Settings > Player > (Android Tab) > Other Settings > and marking "Custom Gradle Properties Template".

After selecting that a gradleTemplate.properties file is generated at "Assets/Plugins/Android/gradleTemplate.properties".

This is the best way of generating the file since it is git friendly and preserves other settings.