Skip to content
This repository

Set the android.manifest.versionCode as a maven property #115

Merged
merged 3 commits into from almost 2 years ago

3 participants

Richard Guest Manfred Moser tprochazka
Richard Guest

I was trying to find a way to use the <versionCodeAutoIncrement> option and get the resulting versionCode out for use later in the build process.

Specifically, I was trying to use maven-resources-plugin to filter a json file with name, versionName and versionCode for use with HockeyKit.

It's entirely possible I've missed something but this seemed like the simplest way to accomplish that goal and I thought it might be of use to others.

Manfred Moser
Collaborator

Sounds good. How about for consistencies sake also exposing the other changed properties (at least the version ones) and updating the javadoc for the config parameters to reflect that property export with the full property names so we have some sort of documentation beyond the source code?

Richard Guest

Yup, good plan. My initial pull request was trying to sound-out whether this was a good idea...

I've added some javadoc and exposed versionName, versionCode, versionCodeAutoIncrement, versionCodeUpdateFromVersion, debuggable and sharedUserId.

Manfred Moser mosabua merged commit 4654bfe into from May 01, 2012
Manfred Moser mosabua closed this May 01, 2012
tprochazka

Maybe will be usefull add the example how to use it directly in the plugin configuration section:

<configuration>
  <manifestVersionCodeAutoIncrement>true</manifestVersionCodeAutoIncrement>
  or
  <manifestVersionCodeUpdateFromVersion>true</manifestVersionCodeUpdateFromVersion>
<configuration>

I use different solution. I have android:versionCode set to the "0". My application automatically detect it as testing mode and when I build it in the release mode it replace dev with SVN revision number or by commits count in GIT.

I use buildnumber plugin and replacer with this configuration:

            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>./AndroidManifest.xml</file>
                    <outputFile>${project.build.directory}/AndroidManifest.xml</outputFile>
                    <regex>true</regex>
                    <replacements>
                        <replacement>
                            <token>android:versionCode="\d*"</token>
                            <value>android:versionCode="${buildNumber}"</value>
                        </replacement>
                        <replacement>
                            <token>android:versionName="[^"]*"</token>
                            <value>android:versionName="${project.version}"</value>
                        </replacement>
                        <replacement>
                            <token>-SNAPSHOT"</token>
                            <value>-${buildNumber}"</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>

and in android plugin configuration I have this <androidManifestFile>${project.build.directory}/AndroidManifest.xml</androidManifestFile>

It provide really unique versionCode which does not depend who or where build the project.
Only problem is that buildnumber can do support this http://jira.codehaus.org/browse/MBUILDNUM-93 now, so I must use own forked version which contain this patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
32  src/main/java/com/jayway/maven/plugins/android/standalonemojos/ManifestUpdateMojo.java
@@ -149,21 +149,25 @@
149 149
 
150 150
 	/**
151 151
 	 * Update the <code>android:versionName</code> with the specified parameter. If left empty it
152  
-     * will use the version number of the project.
  152
+	 * will use the version number of the project. Exposed via the project property
  153
+	 * <code>android.manifest.versionName</code>.
153 154
 	 *
154 155
 	 * @parameter expression="${android.manifest.versionName}" default-value="${project.version}"
155 156
 	 */
156 157
 	protected String manifestVersionName;
157 158
 
158 159
 	/**
159  
-	 * Update the <code>android:versionCode</code> attribute with the specified parameter.
  160
+	 * Update the <code>android:versionCode</code> attribute with the specified parameter. Exposed via
  161
+	 * the project property <code>android.manifest.versionCode</code>.
160 162
 	 *
161 163
 	 * @parameter expression="${android.manifest.versionCode}"
162 164
 	 */
163 165
 	protected Integer manifestVersionCode;
164 166
 
165 167
 	/**
166  
-	  * Auto increment the <code>android:versionCode</code> attribute with each build.
  168
+	  * Auto increment the <code>android:versionCode</code> attribute with each build. The value is
  169
+	  * exposed via the project property <code>android.manifest.versionCodeAutoIncrement</code> and
  170
+	  * the resulting value as <code>android.manifest.versionCode</code>.
167 171
 	  *
168 172
 	  * @parameter expression="${android.manifest.versionCodeAutoIncrement}" default-value="false"
169 173
 	  */
@@ -173,21 +177,25 @@
173 177
 	 * Update the <code>android:versionCode</code> attribute automatically from the project version
174 178
 	 * e.g 3.0.1 will become version code 301. As described in this blog post
175 179
 	 * http://www.simpligility.com/2010/11/release-version-management-for-your-android-application/
176  
-	 * but done without using resource filtering.
  180
+	 * but done without using resource filtering. The value is exposed via the project property
  181
+	 * property <code>android.manifest.versionCodeUpdateFromVersion</code> and the resulting value
  182
+	 * as <code>android.manifest.versionCode</code>.
177 183
 	 *
178 184
 	 * @parameter expression="${android.manifest.versionCodeUpdateFromVersion}" default-value="false"
179 185
 	 */
180 186
 	protected Boolean manifestVersionCodeUpdateFromVersion = false;
181 187
 
182 188
 	/**
183  
-	 * Update the <code>android:sharedUserId</code> attribute with the specified parameter.
  189
+	 * Update the <code>android:sharedUserId</code> attribute with the specified parameter. If
  190
+	 * specified, exposes the project property <code>android.manifest.sharedUserId</code>.
184 191
 	 *
185 192
 	 * @parameter expression="${android.manifest.sharedUserId}"
186 193
 	 */
187 194
 	protected String manifestSharedUserId;
188 195
 
189 196
 	/**
190  
-	 * Update the <code>android:debuggable</code> attribute with the specified parameter.
  197
+	 * Update the <code>android:debuggable</code> attribute with the specified parameter. Exposed via
  198
+	 * the project property <code>android.manifest.debuggable</code>.
191 199
 	 *
192 200
 	 * @parameter expression="${android.manifest.debuggable}"
193 201
 	 */
@@ -362,6 +370,15 @@ public void updateManifest(File manifestFile) throws IOException, ParserConfigur
362 370
 					"Please specify either versionCodeAutoIncrement, versionCodeUpdateFromVersion or versionCode!");
363 371
 		}
364 372
 
  373
+		// Expose the version properties and other simple parsed manifest entries
  374
+		project.getProperties().setProperty("android.manifest.versionName", parsedVersionName);
  375
+		project.getProperties().setProperty("android.manifest.versionCodeAutoIncrement", String.valueOf(parsedVersionCodeAutoIncrement));
  376
+		project.getProperties().setProperty("android.manifest.versionCodeUpdateFromVersion", String.valueOf(parsedVersionCodeUpdateFromVersion));
  377
+		project.getProperties().setProperty("android.manifest.debuggable", String.valueOf(parsedDebuggable));
  378
+		if (parsedSharedUserId != null) {
  379
+			project.getProperties().setProperty("android.manifest.sharedUserId", parsedSharedUserId);
  380
+		}
  381
+
365 382
 		if (parsedVersionCodeAutoIncrement) {
366 383
             Attr versionCode = manifestElement.getAttributeNode(ATTR_VERSION_CODE);
367 384
             int currentVersionCode = 0;
@@ -370,6 +387,7 @@ public void updateManifest(File manifestFile) throws IOException, ParserConfigur
370 387
             }
371 388
             currentVersionCode++;
372 389
             manifestElement.setAttribute(ATTR_VERSION_CODE, String.valueOf(currentVersionCode));
  390
+            project.getProperties().setProperty("android.manifest.versionCode", String.valueOf(currentVersionCode));
373 391
             dirty = true;
374 392
         }
375 393
 
@@ -382,6 +400,7 @@ public void updateManifest(File manifestFile) throws IOException, ParserConfigur
382 400
 					         Integer.toString(artifactVersion.getIncrementalVersion());
383 401
 			getLog().info("Setting " + ATTR_VERSION_CODE + " to " + verCode);
384 402
 			manifestElement.setAttribute(ATTR_VERSION_CODE, verCode);
  403
+			project.getProperties().setProperty("android.manifest.versionCode", String.valueOf(verCode));
385 404
 			dirty = true;
386 405
 		}
387 406
 
@@ -396,6 +415,7 @@ public void updateManifest(File manifestFile) throws IOException, ParserConfigur
396 415
 				manifestElement.setAttribute(ATTR_VERSION_CODE, String.valueOf(parsedVersionCode));
397 416
 				dirty = true;
398 417
 			}
  418
+			project.getProperties().setProperty("android.manifest.versionCode", String.valueOf(parsedVersionCode));
399 419
 		}
400 420
 
401 421
 		if (!StringUtils.isEmpty(parsedSharedUserId)) {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.