Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

[BUG] Unity 2018.1.6f1 can't set versionCode & versionName to custom AndroidManifest.xml

Discussion in 'Android' started by OldKing_Wang, Jul 4, 2018.

  1. OldKing_Wang

    OldKing_Wang

    Joined:
    Jan 25, 2015
    Posts:
    14
    Hi Unity Team


    i have a custom AndroidManifest.xml in

    Plugins/Android/AndroidManifest.xml

    it only define the xmlns:android and package property


    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xxxxxx">




    in Unity 2017.4.6f1

    if i build an apk . it will generate the correct versionCode and versionName for me






    but when i upgrade to Unity 2018.1.6f1

    it can't set the value correctly



    if i fill the android:versionCode and android:versionName proerty in AndroidManifest.xml.


    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xxxxxx" android:versionCode="1" android:versionName="1.1">


    it will use the property . not change it at all . ( the result will be versionCode=1 versionName=1.1)

    ============

    it also happen in my Gradle template. previously i don't need to set minSdkVersion in defaultConfig. but it 2018 version , if not set minSdkVersion, it will set as

    minSdkVersion 1


    defaultConfig {
    minSdkVersion 16
    targetSdkVersion **TARGETSDKVERSION**
    applicationId '**APPLICATIONID**'
    }


    ============

    is this a bug or i need change something in my code?

    Thanks

    Eran
     
  2. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,071
    Hi Eran!

    We cleaned up the manifest template, and moved more things to build.gradle, meaning that gradle will set the necessary values when building.

    Please make sure you use build.gradle template from your current Unity version (and not older ones). Let us know if you still have any issues.
     
  3. OldKing_Wang

    OldKing_Wang

    Joined:
    Jan 25, 2015
    Posts:
    14
    Hi

    i have tested my project with new gradle templete. it's working :)


    Thanks :)
     
    Yury-Habets likes this.
  4. SamiSdd

    SamiSdd

    Joined:
    Mar 12, 2015
    Posts:
    8
    Hi

    We're using 2018.4.18f1, The mainTemplate.gradle for this version doesn't contain versionCode & versionName in defaultConfig Section. When building it doesn't seems to be changing the versionCode & versionName in AndroidManifest.xml. Causing it be blank in generated builds.
     

    Attached Files:

  5. Deleted User

    Deleted User

    Guest

    Yes. Exactly the same here. (Unity 2017.4.18f1 not Unity2018) I've got this in my gradle file
    Code (CSharp):
    1. defaultConfig {
    2.         minSdkVersion **MINSDKVERSION**
    3.         targetSdkVersion **TARGETSDKVERSION**
    4.         applicationId '**APPLICATIONID**'
    5.         ndk {
    6.             abiFilters **ABIFILTERS**
    7.         }
    8.         versionCode    **VERSIONCODE**
    9.         versionName    '**VERSIONNAME**'
    10.  
    11.         multiDexEnabled true
    12.     }
    The output is converted to this
    Code (CSharp):
    1. defaultConfig {
    2.         minSdkVersion 16
    3.         targetSdkVersion 28
    4.         applicationId 'com.help.stressfree'
    5.         ndk {
    6.             abiFilters 'armeabi-v7a', 'x86'
    7.         }
    8.         versionCode  
    9.         versionName    ''
    10.  
    11.         multiDexEnabled true
    12.     }
    I thought LTS branch was meant to be stable? You can't just replace the Android build system on a release branch that's not meant to have any new features.
     
    Last edited by a moderator: Jan 24, 2019
    plarium-kyiv511 and andrew210 like this.
  6. andrew210

    andrew210

    Joined:
    Apr 23, 2014
    Posts:
    153
    Same issue for me as @RealWorld (I think, using the LTS 2017.4.18f1). I can no longer upload new versions of my sdk because the version code is always set to 1 by unity, regardless of what I set in Player Settings.
     
  7. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,071
    EDIT: this is a known issue; the fix has landed yesterday and will be available in the next version. Sorry for the inconvenience caused!
     
  8. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,071
    @RealWorld versionCode and versionName params seem to be missing in 17.4 template at all, but they are present in latest 18.3 versions.
     
  9. Deleted User

    Deleted User

    Guest

    @Yury-Habets Yes. I have a 2018 project that I copied them from assuming that would fix the issue but it didn't. The issue exists whether you include those lines in the gradle file or not
     
  10. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,071
    Right, so the issue with the manifests was fixed and will make it into 2017.4.20f1.
     
    jerome_play and Deleted User like this.
  11. Deleted User

    Deleted User

    Guest

    Do you know why this was done on the LTS branch when its a breaking change?
     
  12. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    383
    This was not done intentionally.
     
  13. Deleted User

    Deleted User

    Guest

    I get that. I'm not suggesting you intentionally broke the LTS branch but what I'm saying is that upgrading the gradle version - even if it worked, required me to regenerate my gradle script which was not communicated at all and cost me many hours of dev time to work out why my Android builds were failing. My understanding was that LTS would not introduce new features for this very reason.
    If nothing else, please consider making upgrade instructions more visible when upgrading to a newer version of Unity.
     
    u546342 likes this.
  14. tessellation

    tessellation

    Joined:
    Aug 11, 2015
    Posts:
    102
    Dang, I just ran into this after upgrading to 2017.4.19f1 that was just released. Can you please add this in BOLD in the release notes that this is a know breaking issue?
     
    codestage and Voxel-Busters like this.
  15. tessellation

    tessellation

    Joined:
    Aug 11, 2015
    Posts:
    102
    Is there a work-around fix for this bug? For example, can we edit the default gradle template and add the appropriate lines to make sure the versionCode and versionName get set?
     
    codestage and DarekRusin like this.
  16. kkopanski

    kkopanski

    Joined:
    Mar 24, 2017
    Posts:
    1
    When 2017.4.20f1 will be released? Do we have to wait another 2 weeks?
     
  17. anton-nesterenko

    anton-nesterenko

    Joined:
    Oct 3, 2014
    Posts:
    2
    Hi everyone,
    I had no time to wait for 2017.4.20f1, and I had to make temporary solution.
    It totally works for me and I hope it helps you too.

    I had the same problems as you guys. All of them:
    • versionCode,
    • versionName,
    • platformBuildVersionCode,
    • platformBuildVersionName,
    • and also with signature
    [INSTALL_FAILED_UPDATE_INCOMPATIBLE: Package com.xxx.yyy signatures do not match the previously installed version; ignoring!]


    First of all, I analyzed the changes that gradle makes incorrectly in the mainTemplate.gradle file.
    I found out that the versionCode, versionName, storePassword and keyPassword parameters were filled with empty values.
    So, my solution is to populate these parameters from code instead of Unity.

    Code (CSharp):
    1. private static void FixGradleTemplate(string bundleVersionCode, string bundleVersionName)
    2. {
    3.     var gradleFile = Application.dataPath + "/Plugins/Android/mainTemplate.gradle";
    4.     var gradleData = File.ReadAllText(gradleFile);
    5.  
    6.     var signTemplate = "\tsigningConfigs { release {\r\n\t\tstoreFile file(\'_0_\')\r\n\t\tstorePassword \'_1_\'\r\n\t\tkeyAlias \'_2_\'\r\n\t\tkeyPassword \'_3_\'\r\n\t} }";
    7.     var signData = signTemplate.Replace("_0_", PlayerSettings.Android.keystoreName)
    8.                                .Replace("_1_", PlayerSettings.Android.keystorePass)
    9.                                .Replace("_2_", PlayerSettings.Android.keyaliasName)
    10.                                .Replace("_4_", PlayerSettings.Android.keyaliasPass);
    11.  
    12.     gradleData = gradleData
    13.                  .Replace("**VERSIONCODE**", bundleVersionCode)
    14.                  .Replace("**VERSIONNAME**", bundleVersionName)
    15.                  .Replace("**SIGN**", signData);
    16.  
    17.     File.WriteAllText(gradleFile, gradleData);
    18. }
    p.s. Shame on you, Unity.
     
  18. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,071
    We apologize for the inconvenience. We are humans and sometimes make mistakes.
     
    optimise and Deleted User like this.
  19. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    459
    Have u guys writing more and more unit tests to avoid regression?
     
  20. anton-nesterenko

    anton-nesterenko

    Joined:
    Oct 3, 2014
    Posts:
    2
    You are absolutely right, we are not machines and any person is prone to err.
    The fact of the mistake is not a claim. It just happened.

    Again, you are professionals and I have no doubt about that.

    I am saddened by the reaction to error messages.
    For example, as a system user, I expect to be told how to resolve the issue as soon as possible. As a workaround, until the patch version is released.
     
  21. tessellation

    tessellation

    Joined:
    Aug 11, 2015
    Posts:
    102
    The way I solved it for now until 2017.4.20 is released: add these attributes to the <manifest> element in the AndroidManifest.xml: android:versionCode="???" android:versionName="???"
    Fill in the ??? as appropriate for your app. Unfortunately whenever you change the version or build number, you need to also edit the manifest file.

    Mistakes are of course understandable, but I think Unity QA deserves some constructive criticism for this bug in 3 key areas:
    1. Unity hasn't offered an official work-around (or link to an issue tracker with one).
    2. They have not posted a warning to users about this major problem in the release notes at https://unity3d.com/unity/qa/lts-releases. Again, providing a work-around here would be essential.
    3. Please add Unity Tests to make sure any release can build and install the game on an Android device using Gradle with many variations like scripting backend, ABIs, app bundle/APK, etc.
     
    andrew210 likes this.
  22. Prince_of_Persia

    Prince_of_Persia

    Joined:
    Oct 20, 2012
    Posts:
    29
    I am using Unity 2017.4.19f1 . I came up with this solution that works good enough for me. A pre process action that modifies the android manifest pre build:

    Code (CSharp):
    1. using System.Xml.Linq;
    2.  
    3. public class ManifestHelper
    4. {
    5.     private XDocument doc;
    6.     private XNamespace ns = @"http://schemas.android.com/apk/res/android";
    7.  
    8.     public ManifestHelper(string path)
    9.     {
    10.         doc = XDocument.Load(path);
    11.     }
    12.  
    13. public void Save(string path)
    14.     {
    15.         doc.Save(path);
    16.     }
    17.  
    18. public void SetVersions(string versionName,int versionCode)
    19.     {
    20.         doc.Root.SetAttributeValue(ns + "versionCode", versionCode);
    21.         doc.Root.SetAttributeValue(ns + "versionName", versionName);
    22.     }
    23. }
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.Build;
    4. using System.IO;
    5.  
    6. public class StorePreProcessor: IPreprocessBuild
    7. {
    8.   public const string AndroidManifestPath = "Assets/Plugins/Android/AndroidManifest.xml";
    9.  
    10. public int callbackOrder
    11.     {
    12.         get
    13.         {
    14.             return 0;
    15.         }
    16.     }
    17.  
    18. public void OnPreprocessBuild(BuildTarget target, string path)
    19.     {
    20.         if(target==BuildTarget.Android)
    21.         {
    22.             ManifestHelper manifest = new ManifestHelper(AndroidManifestPath);
    23.  
    24.             manifest.SetVersions(PlayerSettings.bundleVersion,PlayerSettings.Android.bundleVersionCode);
    25.             manifest.Save(AndroidManifestPath);
    26.         }
    27.     }
    28. }
     
  23. Dryn27

    Dryn27

    Joined:
    Dec 10, 2016
    Posts:
    8
    Same problem here. Using 2017.4.19 LTS the bundle version will always be 1.
     
  24. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,276
    As a temporary workaround, try switching to the Internal build system (solves issue for me):

    upload_2019-2-7_16-25-46.png
     
  25. tessellation

    tessellation

    Joined:
    Aug 11, 2015
    Posts:
    102
    Yury-Habets and Deleted User like this.
  26. ShayKrainer

    ShayKrainer

    Joined:
    Feb 19, 2019
    Posts:
    16
    I seem to have a similar problem in Unity 2018.3.6f1. But only for the version number and name. Can someone confirm if it's the same issue or not?

    I've tried to build with the internal (and now deprecated) build system. The build has failed but the manifest file was correct.
     
    Last edited: Mar 4, 2019
  27. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    383
    Did you upgrade your project from an older Unity version and do you use a custom build.gradle template? If so, please update your build.gradle template to be similar to what your Unity version creates by default.
     
  28. young-xyz

    young-xyz

    Joined:
    Feb 23, 2015
    Posts:
    35
    It appears again in 2017.4.23f1 and 2017.4.24f1.
     
    Tx and DarekRusin like this.
  29. ShayKrainer

    ShayKrainer

    Joined:
    Feb 19, 2019
    Posts:
    16
    I had not the most current build.gradle template and after getting it (or not using it at all, I don't remember) everything went back to normal.
     
  30. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    105
    I'm using Unity 2017.4.24f1, and am still having the issue where Gradle builds end up with the following in AndroidManifest.xml:

    platformBuildVersionCode is set to the version code of my app and platformBuildVersionName is equal to the version name of my app.

    Expected results are that platformBuildVersionCode is set to the target SDK version (example: "28") and the platformBuildVersionName is set to the target SDK name (example: "9").

    My "Build System" is set to Gradle. I do not use a custom build.gradle (so the project should be using whatever template Unity 2017.4.24f1 ships with).

    I noticed that young-xyz mentions having the same experience in 2017.4.23f1 and 2017.4.24f1 - was the bug re-introduced in these versions?
     
  31. DarekRusin

    DarekRusin

    Joined:
    Nov 15, 2013
    Posts:
    30
    This bug came back to me in 2017.4.24 as well, but I was able to fix it following JuliusM's advice above. Just let Unity create a fresh mainTemplate.gradle and then use it as a base. More info, including variable codes, here (Section: Providing a custom build.gradle template):
    https://docs.unity3d.com/Manual/android-gradle-overview.html
     
  32. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    105
    Is it enough to just check the "Custom Gradle Template" checkbox in Player Settings? Or did you have to then customize the template as well, to fix this bug?
     
  33. DarekRusin

    DarekRusin

    Joined:
    Nov 15, 2013
    Posts:
    30
    I'm not sure. In my case, I was just editing my old template, adding variables from the linked Unity document and it did the trick.
     
  34. Otto_Oliveira

    Otto_Oliveira

    Joined:
    Jul 24, 2014
    Posts:
    33
    Updating from Unity 2017.4.18 to 2017.4.25 solved the issue. In my case, I don't use the Custom Gradle Template.
     
  35. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    105
    I recently realized that the values set for platformBuildVersionCode and platformBuildVersionName in AndroidManifest.xml were actually incorrect this entire time, up to and including Unity 2017.4.16f1.

    For example, this is what I had in Unity 2017.4.16f1, after doing a Gradle build (without using any Gradle template):
    platformBuildVersionCode="28" platformBuildVersionName="9"

    which is the target Android SDK version and target Android SDK name, respectively...which is wrong/not what those values should be set to.

    After updating to Unity 2017.4.26f1, I have the following:
    platformBuildVersionCode="1.3.2" platformBuildVersionName="10302"

    which is actually what should be set for those values.

    Just leaving this here in case anyone else was confused about this, like I was. Threw me for a loop since it's been incorrect for years.

    To be clear: everything is working properly now and being set correctly in Unity 2017.4.26f1.
     
    Last edited: Apr 26, 2019
  36. webing

    webing

    Joined:
    May 9, 2016
    Posts:
    1
    I got this problem too in Unity 2017.4.30f1 and 2017.4.27f1.
    And, i build the same test project with Unity 2018.3.14f1, it works right.