码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 自动增加 Android App 的版本号


    一般的 C# 应用程序中都有一个 AssemblyInfo.cs 文件,其中的 AssemblyVersion attribute 就可以用来设置该应用程序的版本号。譬如,

    ?
    1
    [assembly: AssemblyVersion("1.0.*")]

      

    这样设置的 AssemblyVersion attribute,其版本号中的构建编号(Build Number),在每次编译(Build)该应用程序时,就会自动加1。这样,版本号中的主、次版本号由手动设置,而构建编号由编译程序(MSBuild)自动管理,省去了很多麻烦。

    但 Android App 的版本号却无法使用这种方式,因为 Android App 的版本号存在于 AndroidManifest.xml 中:

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              android:versionCode="3" android:versionName="1.0"strong>
              package="mypackage"
              android:installLocation="auto">
        <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
        <application android:label="MyPackage.Android"
                     android:theme="@style/MainTheme"
                     android:allowBackup="false">
        application>
    manifest>

      

    在这个 AndroidManifest.xml 文件中,App 的版本号(versionName)是“1.0”,构建编号(versionCode)是“3”。我们希望能够像 C# 程序一样,由编译程序自动管理构建编号。但似乎还没有这样实现自动管理的编译程序,所以只能自己动手实现类似的功能。

    网上找到了一段C#代码,可以完成自动增加 versionCode 的功能:

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    ////////////////////////////////////////////////////////
    // AutoVersion
    //      Increment the Android VersionCode automatically
    // Version
    //      1.0
    // Author
    //      prowyh@hotmail.com
    // Date
    //      2022-11-22
    // Curtesy
    //      9to5answer.com/auto-increment-version-code-in-android-app
    ////////////////////////////////////////////////////////
     
    using System.Text.RegularExpressions;
     
    namespace AutoVersion
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                string file = "AndroidManifest.xml";
                if (args.Length > 0) { file = args[0]; }
     
                try
                {
                    string text = File.ReadAllText(file);
                    Regex regx = new(@"(?android:versionCode="")(?\d+)(?"")", RegexOptions.IgnoreCase);
                    Match match = regx.Match(text);
                    int verCode = int.Parse(match.Groups["VER"].Value) + 1;
                    string ntext = regx.Replace(text, "${A}" + verCode + "${B}", 1);
     
                    File.WriteAllText(file, ntext);
                }
                catch (Exception exp)
                {
                    using StreamWriter sw = new("AutoVersion.log");
                    sw.Write(exp.Message);
                }
            }
        }
    }

      

    将此代码编译为 AutoVersion.exe,将其包括在 Visual Studio 的 pre-build 事件所执行的命令行中(如下图),即可。

     

    这样,每次点击 “Build Solution” 进行编译时,都会先执行 AutoVersion.exe,完成对 AndroidManifest.xml 中 versionCode 的自动增1 操作。

    下面是 AutoVersion.cs 的 PowerShell 版本:

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <#
     .SYNOPSIS
        AutoVersion.ps1
     .DESCRIPTION
        PowerShell script for automatically incrementing the Android VersionCode.
     .VERSION
        1.0
     .AUTHOR
        prowyh@hotmail.com
     .DATE
        2022-11-22
    #>
    $content = Get-Content AndroidManifest.xml
    [regex]$rx = "(?android:versionCode="")(?\d+)(?"")"
    $m = $rx.Matches($content)
    $nv = $([System.Int32]$m[0].Groups["VER"].Value + 1)
    $nvCode = $m[0].Groups["A"].Value, $nv, $m[0].Groups["B"].Value -join ""
    $content -replace "android:versionCode=""(\d+)""", $nvCode | Out-File -FilePath AndroidManifest.xml

      

    2022-11-24 更新

    对于 .NET MAUI 项目,Android.Manifest.xml 与 Xamarin.Forms 的一样,但位置变了。可以通过 mauiApp.csproj 项目文件进行设置,如下所示:

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    "Microsoft.NET.Sdk">
    ......
     
        "PreBuild" BeforeTargets="PreBuildEvent" Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">
          "D:\AutoVersion\AutoVersion.exe $(ProjectDir)\Platforms\Android\AndroidManifest.xml" />
        
     

      

    需要注意的是,因为一个MAUI解决方案中包含了若干项目,在编译(Build)时,每个项目都要 Build 一遍,导致 AutoVersion 执行多次。解决的办法是使用条件(Condition) attribute 将 AutoVersion 的执行限制在编译 Android 项目时。

     

  • 相关阅读:
    CentOS7 部署 k8s
    04 Opencv图像操作
    vue导出excel使用xlsx、file-saver、xlsx-style、yxg-xlsx-style 遇到的坑
    Bootstrap禁止点击空白处关闭模态框
    大模型lora微调-chatglm2
    无感验证案例:工商联人才中心
    树洞外链网盘系统php源码去除底部版权优化版
    万宾科技智能井盖传感器,预防城市道路安全
    Python——函数
    python 正则表达式
  • 原文地址:https://www.cnblogs.com/prowyh/p/16915355.html
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号