Auto increment version code in Android app

后端 未结 15 1043
忘掉有多难
忘掉有多难 2020-12-07 08:22

is there a way to auto-increment the version code each time you build an Android application in Eclipse?

According to http://developer.android.com/guide/publishing/v

15条回答
  •  忘掉有多难
    2020-12-07 08:26

    All credit goes to ckoz, but I writed my own implementation in c#. I think it's a little faster and doesn't eat errors because If something goes wrong probably something is wrongly configured and I should know about it.

    namespace AndroidVersionCodeAutoIncrement
    {
        using System.IO;
        using System.Text.RegularExpressions;
    
        public class Program
        {
            private static readonly Regex VersionCodeRegex = new Regex("android:versionCode=\"(?.*)\"", RegexOptions.Compiled);
    
            public static void Main()
            {
                using (var manifestFileStream = File.Open("AndroidManifest.xml", FileMode.Open, FileAccess.ReadWrite))
                using (var streamReader = new StreamReader(manifestFileStream))
                {
                    var manifestFileText = streamReader.ReadToEnd();
    
                    var firstMatch = VersionCodeRegex.Match(manifestFileText);
                    if (firstMatch.Success)
                    {
                        int versionCode;
                        var versionCodeValue = firstMatch.Groups["version"].Value;
                        if (int.TryParse(versionCodeValue, out versionCode))
                        {
                            manifestFileText = VersionCodeRegex.Replace(manifestFileText, "android:versionCode=\"" + (versionCode + 1) + "\"", 1);
    
                            using (var streamWriter = new StreamWriter(manifestFileStream))
                            {
                                manifestFileStream.Seek(0, SeekOrigin.Begin);
                                streamWriter.Write(manifestFileText);
                                manifestFileStream.SetLength(manifestFileText.Length);
                            }
                        }
                    }
                }
            }
        }
    }
    

提交回复
热议问题