How to automatically increase the number of Android projects in Eclipse (prefer a portable solution)

Getting an IDE to automatically increase build numbers is a long-debated issue - I am always surprised that what seems so basic (to me) is so much work.

The highest question and answer (quite a lot) for this topic is given here: https://stackoverflow.com/a/212632/2/2/2/ My problem is that the solution depends on .net, and I would prefer to avoid the .net dependency for an Eclipse solution. I also want to avoid any dependency on VCS.

So, I'm looking for a solution that carries over and "Eclipse-ish". I think the best candidates are the Ant solution (which I will post) or the Java application similar to the dotNet application in the linked answer above.

(I did not add my answer to this question because it has an accepted answer and is already a very long post. If moderators prefer, I will delete this question and add my answer to the question above.)

I was hoping for something relatively simple, but I don't think the solution to this problem is really simple.

+5
source share
6 answers

Have you tried creating Maven-ise to build your Android project?

If you use the "maven-android-plugin" , you can track your project build using the pom.xml "version" tag.

In pom.xml you can increase the build version using "versions-maven-plugin"

AndroidManifest.xml, pom.

Android, "-maven-plugin" , . , Ant.

, , ( , ). , , - .

+7

"" script apk.

Builders . "", script. "". Builder, "". script "". " ". , (, ${build_project:/libs/update_version.sh} script lib).

, , "".

" " ( "" ) , , .

- Linux, Unix MacOS. grep , , , awk . Ruby, Python, Perl .. script, versionCode AndroidManifest.xml. , Windows , .

+8

, Ant. , Ant , , , Eclipse, , .

Ant Android SDK Eclipse ( ), ADT , , , Eclipse.

, Ant .

Ant , JDK ( JRE). Windows , JAVA_HOME JDK.

Ant - Eclipse , , , Ant . ,

> android update project --path .

build.xml . ,

> ant release

Ant . , .

build.xml ( , /)

 <target name="update.buildnum">
      <buildnumber/>
      <!-- this task creates (and increments) Ant property 'build.number' which I use below -->
      <echo>Current build number:${build.number}</echo>         
      <echoxml file="res/values/buildnum.xml">
                <resources>
                    <string name="buildnum">${build.number}</string>
                </resources>                    
      </echoxml>
 </target>

, , ( ), "build.number" . , :

String build = context.getString( com.your.app.R.string. buildnum );
int appBuild = ( int ) Integer.parseInt( build ); 

, AndroidManifest.xml. , , Ant (xmltask), , , ( ) . , Android :

String packageName = ctx.getPackageName();
PackageInfo pInfo = ctx.getPackageManager().getPackageInfo( packageName, 0);
int appVer = pInfo.versionCode;

, Eclipse Ant , .

, , Builder. , .

  • - , :.
  • Buildfile:. " ", , "$ {workspace_loc:/appname/build.xml}"
  • . "help" ( build.mxl, ), "build.number". Manual Build:.

Builder .

, , Eclipse () , , . , , . , , , Google , ADT .

() Ant 1.8.4, Eclipse Indigo SR2 (3.7.2), ADT 20 SDK Tools 20, Win7 32bit.

+3

Ant ( XMLTask)

<property name="version.file" value="version.properties"/>
<property name="manifest.file" value="AndroidManifest.xml"/>
<!--build.slot: 0.1.2.3.4.5-->
<property name="build.slot" value="2"/>

<target name="all">
    <antcall target="increment.build"/>
    <antcall target="update.manifest"/>
</target>

<target name="increment.build">
    <propertyfile file="${version.file}">
        <entry key="build.no" type="int" default="0" operation="+" value="1" />
    </propertyfile>
</target>

<scriptdef name="setBuildNo" language="javascript">
    <attribute name="verName" />
    <attribute name="buildNo" />
    <attribute name="buildSlot" />
    <attribute name="property" />
    <![CDATA[
        var verNums = attributes.get("vername").split(".");
        var buildNo = attributes.get("buildno");
        var buildSlot = attributes.get("buildslot");
        if (!(buildSlot > 1 && buildSlot < 10))
            buildSlot = 2;
        var newVer = "";
        for (var i = 0; i < Math.min(verNums.length, buildSlot); i++)
            newVer += (verNums[i].trim() || "0") + ".";
        for (var i = verNums.length; i < buildSlot; i++)
            newVer += "0" + ".";
        project.setProperty(attributes.get("property"), newVer + buildNo);
    ]]>
</scriptdef>

<target name="debug">
    <setBuildNo verName="1" buildNo="999" property="a"/>
    <setBuildNo verName="2." buildNo="999" property="b"/>
    <setBuildNo verName="3.3" buildNo="999" property="c"/>
    <setBuildNo verName="4.4." buildNo="999" property="d"/>
    <setBuildNo verName="5.5.5" buildNo="999" property="e"/>
    <setBuildNo verName="6.6.6." buildNo="999" property="f"/>
    <setBuildNo verName="7.7.7.7" buildNo="999" property="g"/>

    <echo>1 =&gt; ${a}</echo>
    <echo>2. =&gt; ${b}</echo>
    <echo>3.3 =&gt; ${c}</echo>
    <echo>4.4. =&gt; ${d}</echo>
    <echo>5.5.5 =&gt; ${e}</echo>
    <echo>6.6.6. =&gt; ${f}</echo>
    <echo>7.7.7.7 =&gt; ${g}</echo>

</target>

<target name="update.manifest">

    <fail message="File not found: &quot;${manifest.file}&quot;" status="1">
        <condition>
            <not>
                <available file="${manifest.file}" />
            </not>
        </condition>
    </fail>

    <!-- Reads build version -->
    <loadproperties srcfile="${version.file}">
        <filterchain>
            <linecontains>
                <contains value="build.no="/>
            </linecontains>
        </filterchain>
    </loadproperties>

    <!-- Reads versionName from AndroidManifest -->
    <xmlproperty file="${manifest.file}" collapseAttributes="true"/>
    <fail unless="manifest.android:versionName" message="Attribute &quot;android:versionName&quot; undefined into &quot;${manifest.file}&quot;" status="1"/>
    <property name="version.name" value="${manifest.android:versionName}"/>

    <!-- Create a new version -->
    <setBuildNo verName="${version.name}" buildNo="${build.no}" buildSlot="${build.slot}" property="new.version.name"/>

    <!-- Replaces using regexp -->
    <replaceregexp
        file="${manifest.file}"
        match="android:versionName.*=.*&quot;.*${version.name}.*&quot;"
        replace="android:versionName=&quot;${new.version.name}&quot;"/>

    <!-- Replaces for check and generates a exception (if not found version) -->
    <replace
        file="${manifest.file}"
        token="android:versionName=&quot;${new.version.name}&quot;"
        value="android:versionName=&quot;${new.version.name}&quot;"
        failOnNoReplacements="true"/>

    <!--<echo>${version.file}</echo>
    <echo>${manifest.file}</echo>-->
    <echo>Auto Increment Build: ${version.name} =&gt; ${new.version.name}</echo>

</target>

ant [-Dmanifest.file=<file>] [-Dversion.file=<file>] [-Dbuild.slot=<number>]
    -Dmanifest.file - path and filename AndroidManifest.xml. Default: AndroidManifest.xml in a folder with build.xml
    -Dversion.file - path and file name in which to store the number Build version. Default: version.properties folder with build.xml
    -Dbuild.slot - position of build number in version array. Default: 2, ie X.X.build.

Eclipse

Project | Properties | Builders | New | Ant Build
    Main
        Build file
            <path>/build.xml
        Arguments
            -Dmanifest.file=${workspace_loc://AndroidManifest.xml} -Dversion.file=${workspace_loc://version.properties}
    Refresh
        [x] Refresh resources upon completion
        (*) Specify resources
            [Specify resources] 
                <Project>
                    [x] AndroidManifest.xml
    Build Options
        [x] Specify working set of relevant resources
            [Specify resources] 
                <Project>
                    [x] res
                    [x] src
                    [x] AndroidManifest.xml
+1

, , XMLTask Groovy . git ​​ - .

 <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>


  <path id="groovy.classpath">
    <fileset dir="/Users/deepwinter1/.gvm/groovy/current/lib/">
      <include name="*.jar"/>
    </fileset>
  </path>

  <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy">
    <classpath refid="groovy.classpath"/>
  </taskdef>

  <target name="update.build.number">
    <xmltask source="AndroidManifest.xml">
      <copy path="manifest/@android:versionCode" property="buildNum"/>
    </xmltask>
    <groovy>
      buildNum = Integer.valueOf(properties["buildNum"])
      properties["buildNum"] = buildNum + 1
    </groovy>
    <xmltask source="AndroidManifest.xml" dest="AndroidManifest.xml">
      <replace path="manifest/@android:versionCode"
        withText="${buildNum}"/>
    </xmltask>
    <antcall target="commit.and.tag.build">
      <param name="buildNum" value="${buildNum}"/>
    </antcall>
  </target>

  <target name="commit.and.tag.build">
    <exec executable="git">
      <arg value="commit"/>
      <arg value="-a"/>
      <arg value="-m"/>
      <arg value="Build Number ${buildNum}"/>
    </exec>
    <exec executable="git">
      <arg value="tag"/>
      <arg value="b${buildNum}"/>
    </exec>
  </target>
0

"bit" is a better workaround, which you can find here ... fooobar.com/questions/88612 / ...

A Tiny C # tool that will run before each new build and increment the corresponding value in the manifest file.

0
source

All Articles