使用 MSBuild 更改默认 APK 输出以在名称中包含版本

Changing default APK output to include version in the name using MSBuild

在使用 MSBuild 构建 APK 时,我想更改输出的 apk 名称以包含版本号

所以

android:versionName="3.1.5"

最终会像:

MyAndroidApp-3.1.5.apk

我尝试执行 post-构建步骤,我尝试将 apk 复制到不同的名称,但是 post 构建步骤中没有任何具有版本的宏(我能看到)

调用时

MSBuild .\trunk\TaxiTabletUniversal.Droid.MyAndroidProject /t:SignAndroidPackage  /p:Configuration=Release 

输出的 apk 以名称结尾:

company_name.mypackage_name-Signed.apk

在 Android 包签名设置中,我只能指定密钥库和密码,但不能指定输出名称。

我希望输出名称在

中获取版本名称

AndroidManifest.xml

文件

Changing default APK output to include version in the name using MSBuild

抱歉,答案可能是否定的,因为我知道 MSBuild 本身没有能力从 AndroidManifest.xml 读取版本号等数据。换句话说,msbuild 不支持它。

MSBuild 可以访问任何 property defined in project file or imported targets file, but it can't access AndroidManifest.xml file. And there's no official msbuild task can to this for us, so if you do need this behavior, we have to code ourselves to read the version info from that xml file. Topics about this: one, two, ...(网上有太多话题讨论这个,所以我在这里不多说,如果您遇到有关编码的问题,请告诉我:- ))

编码后有两种可能的方法:

1.Create 一个 .exe 文件,其中包含执行重命名作业的代码,并在 post-build 事件中调用 .exe

2.Write一个custom task名为CustomTask,并在项目文件中添加此脚本,在buildSignAndroidPackage目标后调用此任务。

  <UsingTask TaskName="CustomTask.CustomTask"
        AssemblyFile="path\CustomTask.dll"/>

  <!--Maybe it should be AfterTargets="SignAndroidPackage"-->
  <Target Name="CustomTarget" AfterTargets="Build">
    <CustomTask/>
  </Target>

希望它能有所帮助。