【问题标题】:Android: How to pass version info during ant command line build?Android:如何在 ant 命令行构建期间传递版本信息?
【发布时间】:2014-10-22 17:01:40
【问题描述】:

当我使用 Ant 在命令行中构建 Android 项目时,我想更新 AndroidManifest.xml 文件中的 android:versionCode 和 android:versionName。有没有办法可以使用属性文件注入版本号?

【问题讨论】:

    标签: android android-build


    【解决方案1】:

    您可以通过多种方式设置版本信息,

    1. 作为命令行参数传递
    2. 使用属性文件。

    作为命令行参数传递,

     <target name="set-version-using-commandline-args">
        <!-- Load properties from "version.properties" file -->     
        <replaceregexp file="AndroidManifest.xml" match="android:versionCode(.*)" 
            replace='android:versionCode="${Version.Code}"'/>
        <replaceregexp file="AndroidManifest.xml" match="android:versionName(.*)" 
            replace='android:versionName="${Version.Name}"'/>       
    </target>
    

    然后像这样运行ant构建,

    ant -DVersion.Code=100 -DVersion.Name=5.0.0.1201011 debug 
    

    如果要使用属性文件传递版本信息,请使用此目标,

     <target name="set-version-using-file">
        <!-- Load properties from "version.properties" file -->             
        <property file="version.properties" />
    
        <replaceregexp file="AndroidManifest.xml" match="android:versionCode(.*)"
            replace='android:versionCode="${Version.Code}"'/>
        <replaceregexp file="AndroidManifest.xml" match="android:versionName(.*)"
            replace='android:versionName="${Version.Name}"'/>       
    </target>
    

    有关详细说明,请参阅此博客文章。 Android: How to version command line build?

    【讨论】:

    • 嘿。太糟糕了,是你的编辑把这个冒泡到我看到它的顶部,而不是你第一次问它的时候。它会为您节省一些时间!
    • @Argyle 感谢您的帮助
    • 上面的正则表达式对我不起作用。 ".*" 太贪婪了,而且会破坏 XML。 Argyle 的回答更接近于解决我的问题。
    • 确保您在原始 AndroidManifest.xml 中的单独文件中分别拥有 android.versionCode="XXX"android.versionName="XXX",否则正则表达式会破坏您的 xml 语法。
    【解决方案2】:

    当然!首先,创建您的属性文件。说,spiffy.properties。接下来,您将使用 Android Ant 构建的 custom_rules.xml 文件。如果您还没有,请创建它。

    在该文件的顶部附近,添加如下所示的一行:

    <property file="spiffy.properties"/>
    

    现在,向-pre-build 目标添加一个依赖项来调用它:

    <target name="-set-manifest-values">
      <replaceregexp file="AndroidManifest.xml">
        <regexp pattern="android:versionName=&quot;.*&quot;"/>
        <substitution expression="android:versionName=&quot;${version.name}&quot;"/>
      </replaceregexp>
      <replaceregexp file="AndroidManifest.xml">
        <regexp pattern="android:versionCode=&quot;.*&quot;"/>
          <substitution expression="android:versionCode=&quot;${build.number}&quot;"/>
      </replaceregexp>
    </target>
    

    您的版本名称和内部版本号分别由${version.name}${build.number} 指定。由于它们是属性,因此您还可以在命令行中指定它们或作为持续集成设置的一部分。

    【讨论】:

    • 我能够让它工作,除了我必须添加一个?在 * 之后使其不那么贪婪。添加了上面的编辑。
    【解决方案3】:

    通过 Ruby 更新了 AndroidManifest,请参阅 https://gist.github.com/cybertk/24ce4d20d76f9d6a16c6

        File.open('AndroidManifest.xml', 'r+') do |f|
            manifest = f.read
    
            build = ENV['TRAVIS_BUILD_NUMBER'] || 0
            # Update version
            manifest = manifest.gsub(
                /android:versionCode=".*"/, "android:versionCode=\"#{build}\"")
    
            # Write back
            f.rewind
            f.truncate(0)
            f.write(manifest)
        end
    

    【讨论】:

      猜你喜欢
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      相关资源
      最近更新 更多