Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Generating launcher gradle file from launcherTemplate.gradle template file

Discussion in 'Android' started by sungkukshawnpark, Oct 8, 2019.

  1. sungkukshawnpark

    sungkukshawnpark

    Joined:
    Aug 7, 2015
    Posts:
    1
    Hi,

    I'm currently working on Gradle template workflow to generate a custom launcher gradle file from launcherTemplate.gradle file.

    However, even I put the launcherTemplate.gradle file in "Assets/Plugins/Android" directory, it isn't copied and parsed into "[AndroidBuildFolder]/launcher/build.gradle" file which makes the process much harder to maintain.

    Compared to this template, mainTemplate.gradle and settingsTemplate.gradle behave as expected. They are copied from the "Assets/Plugins/Android" directory under "[AndroidBuildFolder]" directory so I can make use of them.

    Is there any way I can use a custom launcher gradle file and replace it with the default launcher gradle file? Or do you have a plan to make the launcherTemplate.gradle file behave as expected as mainTemplate and settingTemplate? When will it be if it is planned already?

    Thanks for reading the post!
    I'd expect to have the answer soon :D
    It would be appreciated if someone has the same problem and share any solution if you have any of them.
     
    Tiger_2k and Foxtrotter like this.
  2. JKasten

    JKasten

    Joined:
    Apr 11, 2013
    Posts:
    23
    Same, my custom mainTemplate.gradle creates gradle compile errors for me starting with 2019.3.0f1. Due to the fact I need to add my gradle changes to the app / launcher project instead. However adding launcherTemplate.gradle to my project isn't getting picked up.

    I tried Unity 2019.3.0f1 AND 2020.1.0a14 it isn't exporting it nor using it when doing a build. In addition to the launcherTemplate.gradle file I tried to add baseProjectTemplate.gradle to "Assets/Plugins/Android" but no go.

    When going to "Player Settings" > "Publishing Settings" > Build I see "Custom Gradle Template" as however just like older version of Unity it just states mainTemplate.gradle. I suspect Unity hasn't completed this part in the beta and it can't be enabled yet.

    I see the following files exist in Unity itself but it always seems to the built in one instead of the project one.

    C:\Program Files\Unity\Hub\Editor\2019.3.0f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\GradleTemplates
    Code (csharp):
    1.  
    2. Mode                 LastWriteTime         Length Name
    3. ----                 -------------         ------ ----
    4. -a----        11/21/2019   8:18 PM            597 baseProjectTemplate.gradle
    5. -a----        11/21/2019   8:18 PM           1734 launcherTemplate.gradle
    6. -a----        11/21/2019   8:18 PM            630 libTemplate.gradle
    7. -a----        11/21/2019   8:18 PM           1021 mainTemplate.gradle
    8. -a----        11/21/2019   8:18 PM             50 settingsTemplate.gradle
    9.  
    I have already looked over the current documentation for this and it doesn't provide any details on how to enable these new .gradle files for a project.
    https://docs.unity3d.com/2019.3/Documentation/Manual/android-gradle-overview.html
    There is also a post about the feature that you can try following up as well under.
    https://forum.unity.com/threads/using-unity-as-a-library-in-native-ios-android-apps.685195
     
    Last edited: Nov 30, 2019
    Tiger_2k and Foxtrotter like this.
  3. aj-fix

    aj-fix

    Joined:
    Sep 4, 2018
    Posts:
    7
    Just wanted to say I'm having the same problem. OneSignal for Unity requires a manual override to the mainTemplate.gradle file, but it looks like this needs to happen in the launcherTemplate.gradle file now (as far as I understand). Hope this gets supported soon.
     
    Tiger_2k likes this.
  4. bsawyer

    bsawyer

    Joined:
    May 6, 2014
    Posts:
    37
    I need to be able to customize the filename of the resulting .apk file based on environment variables that exist on the build server at build time. Up through Unity 2018 I could have something like this in
    defaultConfig
    of my mainTemplate.gradle:

    Code (gradle):
    1. applicationVariants.all { variant ->
    2.     variant.outputs.all { output ->
    3.         def relativeRootDir = output.packageApplication.outputDirectory.toPath()
    4.                                 .relativize(rootDir.toPath()).toFile()
    5.         outputFileName = new File(
    6.             "$relativeRootDir/build/outputs/apk/" + variant.buildType.name,
    7.             "$archivesBaseName-" + variant.buildType.name + "-" + versionName + ".$jenkinsBuildNumber" + ".apk")
    8.    }
    9. }
    But now in Unity 2019(.3) the above no longer works. I get build errors saying:
    Could not get unknown property 'applicationVariants' for DefaultConfig_Decorated. By going back over the Unity documentation for gradle I surmise what would previously have been a single monolithic template.gradle file has now been broken up into multiple templates, each with their own role in the build process. Judging from the explanations, what I'm trying to do (walking through applicationVariants and setting the outputFileName) does not belong in mainTemplate.gradle anymore and should be in launcherTemplate.gradle.

    But I have no idea what else I need to put in that file to make it "valid"!
     
  5. Tiger_2k

    Tiger_2k

    Joined:
    Jan 16, 2016
    Posts:
    4
    I have the same problem, too.

    I have to modify the gradle file for the launcher but my launcherTemplate.gradle file is not used.
    However, the mainTemplate.gradle file is used for the library project.
    I'm experiencing this issue under Unity2019.3.3
     
  6. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    Seriously, this seem like a bug. Now I think I can't use cloud build because I have migrate project to 2020. And why no unity people ever look into this bug since 2019???
     
  7. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    It seem this feature is available in latest beta version

    Android: Allow to overwrite gradle.properties file, as well as main and launcher manifests, base, launcher and main gradle templates. Default templates can be added to project by enabling checkboxes in Player Settings > Publishing Settings.​

    https://unity3d.com/unity/beta/2020.1.0b2
     
  8. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    BowlingPin likes this.
  9. bleachfan700

    bleachfan700

    Joined:
    Aug 9, 2019
    Posts:
    3
    I'm new to unity but where can I find the file path for Assets\Plugins\Android\mainTemplate.gradle
     
  10. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,049
    There was a setting screenshot you can see around the header
    Customizing your Gradle build


    https://docs.unity3d.com/Manual/android-gradle-overview.html

    You need to enable this. And unity will create those files you enabled into your project