Archive for the ‘Uncategorized’ Category.

Solving the Android R.java problem

R.java is one of the most annoying things I’ve come across recently. If you want to create and deploy multiple android apps from the same source, it must be done with each app having a unique package name, and there in lies the rub. A unique package name means a unique R.java with the same package name for each instance, but your source want’s to use a single R.java, and you certainly don’t want to be changing all your source to update the package name every time you release a different version of your app, for example a free trial vs. the paid version.

So the solution is to create two R.java files, one with your internal package name and the other with the package name of the release which comes from AndroidManifest.xml, at this point I would point out that you should probably generate your AndroidManifest.xml.

To generate the required R.java files, you must customize the Google Ant build scripts,  copy android_rules.xml from the SDK into your project. The second exec stanza generates the internal R.java which you specify with the property “package.internal”

<!-- Generates the R.java file for this project's resources. -->
 <target name="-resource-src" depends="-dirs">
 <echo>Generating R.java / Manifest.java from the resources for ${package.destination}</echo>
 <exec executable="${aapt}" failonerror="true">
 <arg value="package" />
 <arg line="${v.option}" />
 <arg value="-m" />
 <arg value="-J" />
 <arg path="${gen.absolute.dir}" />
 <arg value="-M" />
 <arg path="AndroidManifest.xml" />
 <arg value="-S" />
 <arg path="${resource.absolute.dir}" />
 <arg value="-I" />
 <arg path="${android.jar}" />
 <arg value="--custom-package" />
 <arg value="${package.destination}" />
 </exec>

 <echo>Generating R.java / Manifest.java from the resources for ${package.internal}</echo>
 <exec executable="${aapt}" failonerror="true">
 <arg value="package" />
 <arg line="${v.option}" />
 <arg value="-m" />
 <arg value="-J" />
 <arg path="${gen.absolute.dir}" />
 <arg value="-M" />
 <arg path="AndroidManifest.xml" />
 <arg value="-S" />
 <arg path="${resource.absolute.dir}" />
 <arg value="-I" />
 <arg path="${android.jar}" />
 <arg value="--custom-package" />
 <arg value="${package.internal}" />
 </exec>        
 </target>

In your build file, tweak the android setup as follows

<setup import="false" />
<import file="${basedir}/build/android_rules.xml"/>

import false says don’t use the standard android_rules and the following import references your custom rules.

A good reference, but didn’t go far enough: http://java.dzone.com/articles/building-two-versions-same?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+javalobby/frontpage+(Javalobby+/+Java+Zone)

Java Code Coverage

I haven’t done an exhaustive analysis by any stretch of the imagination, but after trying out EMMA – http://emma.sourceforge.net/ and Cobertura – http://cobertura.sourceforge.net/index.html

The clear winner is Cobertura for the following reasons:

  • Ant targets are easier to use and don’t generate errors in Intellij (a minor thing but highly annoying)
  • It is much easier to exclude test classes
  • The output generated is vastly superior if not more voluminous
  • Better but still sparse documentation

Sun’s Java VisualVM

VisualVM is an interesting tool, and the fact you don’t have to instrument your applications to use it is rather nice. Real-time monitoring of you application is very nice and the ability to take snapshots or heap dumps is also very useful, but their heap analysis tool needs some work.

Compared to some other heap analysis tools I’ve used VisualVM needs a lot of work. Having said that being able to take heap dumps as you manipulate a system in real-time is very useful, and then using another analysis tool seems like the best option (note I haven’t tried using another analyzer with visualVM’s heap dump.)

Technical Details has moved…

As we’ve been slowly winding down BlogMatrix for sometime, I decided to move my own blogs to my own domain and server. Having access to everything allowed me to write a BlogMatrix to Wordpress converter.

I also had the relevant addresses redirected to my server, so techdetails.blogmatrix.com -> techdetails.agwego.com, which posed an issue with respect to permalinks, so with a little Wordpress hacking I was able to write a pseudo BlogMatrix to Wordpress permalink converter so most links will now work as expected. I have a complete duplicate site setup at home replete with DNS for testing purposes.

I’m still getting use to Wordpress but so far it’s not too bad.

My other blog http://dinner.agwego.com/