Is there a way to change the gradle.properties fil

2020-07-10 05:15发布

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?

4条回答
smile是对你的礼貌
2楼-- · 2020-07-10 05:53

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.

查看更多
forever°为你锁心
3楼-- · 2020-07-10 06:08

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.

查看更多
beautiful°
4楼-- · 2020-07-10 06: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.

查看更多
我想做一个坏孩纸
5楼-- · 2020-07-10 06:14

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)
    }
}
查看更多
登录 后发表回答