Is there a way to change the gradle.properties file in Unity

后端 未结 5 625
故里飘歌
故里飘歌 2021-01-05 07:45

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

相关标签:
5条回答
  • 2021-01-05 08:15

    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)
        }
    }
    
    0 讨论(0)
  • 2021-01-05 08:23

    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.

    0 讨论(0)
  • 2021-01-05 08:26

    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.

    0 讨论(0)
  • 2021-01-05 08:26

    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:

    0 讨论(0)
  • 2021-01-05 08:27

    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.

    0 讨论(0)
提交回复
热议问题