How to automate version code incrementing in AndroidManifest?

后端 未结 4 1107
感情败类
感情败类 2021-02-14 07:47

I have application which uploaded into Subversion/SVN repo. Is it possible to use script to somehow change application version code stored in AndroidManifest? I mea

相关标签:
4条回答
  • 2021-02-14 08:00

    You could do it at build time using the xpath Ant target provided by the default Android build scripts to get the current value, then use Ant replace.

    0 讨论(0)
  • 2021-02-14 08:06

    SVN has keyword substitution which you can use to put the revision number in a file. However, this will result in the revision when the manifest file itself was last changed, which is not what you want.

    The best way to achieve what you want is to call svnversion from your build script, and embed the output in the manifest file from there. You could have a manifest.template file under version control which can be used by your build script to generate the real manifest file. Put a placeholder word in the template and replace it by the revision number obtained with svnversion.

    edit: in Eclipse you would probably use an Ant script, and invoke it before the normal build. The linked example generates a class file with the revision number, but you could also generate a manifest file in a very similar way.

    0 讨论(0)
  • 2021-02-14 08:07

    SVN approach is fine, but not necessarily suitable for everyone. I do prefer to have control on app versionName and versionCode increment so I did my own Java simple app to do that

    Features:

    • versionName is supposed to be major.minor.point (as advised by Android doc)
    • versionName can be preserved, reset to 1.0.0, or incremented (one single part of it and trailing part(s) is/are set to 0)
    • versionCode will be replaced by Unix Time

    Available here: android manifest build number

    0 讨论(0)
  • 2021-02-14 08:19

    I've done this by adding an extra build target - so the version code is incremented when you build the project, not when you commit to SVN. These are instructions to get this to work in IntelliJ IDEA.

    First, add a new file in the root of your project called svn-revision.build.xml with the following contents:

    <project default="svn-revision">
        <target name="svn-revision">
            <exec executable="sh" outputproperty="revision">
                <arg value="-c" />
                <arg value="svnversion | sed -e 's/^[^:]*://;s/[A-Za-z]//'" />
            </exec>
            <echo>Revision (app): ${revision}</echo>
            <replaceregexp file="AndroidManifest.xml" match='android:versionCode="([^".]+)(\.[^"]*)?"' replace='android:versionCode="${revision}"' />
        </target>
    </project>
    

    Then in the menu go to View > Tools > Ant Build, then press the little + button in the window and add the newly created file. Clicking the green 'run' button in the Ant Build window will run the script right away. To make the script run every time you build, you will need to set the 'svn-revision' target as a default target.

    0 讨论(0)
提交回复
热议问题