Friday, October 14, 2011

Starting up the Android project with android-maven plug-in


For some people (like me) all software projects must be Mavenized. Android projects are no exception from this rule.


The motivation behind using Maven with Android is:
  • dependency management 
  • smooth integration with Maven lifecycle
  • support for exisitng tools (Continious Integration, ProGuard obfuscator, Check Style, Find Bugs, JDepend, etc) via Maven plug-ins system
I decided to try out android-maven plug-in since it is recommended by the Sonatype.

At first I created the Android project from the archetype...

mvn archetype:generate \
  -DarchetypeArtifactId=android-quickstart \
  -DarchetypeGroupId=de.akquinet.android.archetypes \
  -DarchetypeVersion=1.0.6 \
  -DgroupId=helloCompany \
  -DartifactId=helloAndroid

...and then built it...

hekonsek@Drone:~$ cd tmp/helloAndroid
hekonsek@Drone:~/tmp/helloAndroid$ mvn clean install
...
[INFO] Installing /home/hekonsek/tmp/helloAndroid/target/helloAndroid-1.0-SNAPSHOT.apk to /home/hekonsek/.m2/repository/helloCompany/helloAndroid/1.0-SNAPSHOT/helloAndroid-1.0-SNAPSHOT.apk
[INFO] Installing /home/hekonsek/tmp/helloAndroid/target/helloAndroid-1.0-SNAPSHOT.jar to /home/hekonsek/.m2/repository/helloCompany/helloAndroid/1.0-SNAPSHOT/helloAndroid-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13 seconds
[INFO] Finished at: Wed Oct 12 20:47:26 CEST 2011
[INFO] Final Memory: 42M/493M
[INFO] ------------------------------------------------------------------------

At first glance everything went fine so we expect to have Android APK artefact packaged and ready to be deployed to the device. However when I try to do the latter I get the following error message:

hekonsek@Drone:~/tmp/helloAndroid$ mvn clean install android:deploy 
...
[INFO] 279 KB/s (14872 bytes in 0.051s)
 pkg: /data/local/tmp/helloAndroid-1.0-SNAPSHOT.apk
Failure [INSTALL_FAILED_INVALID_APK]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------

I also tried to deploy the APK file manually (using the ADB tool) but Android emulator still claims that the package is broken. I assume then that the current android-quickstart archetype is broken.

My workaround for the broken android artefact is to download zipped samples from the android-maven plug-in site and use Scala example as a template for my new project. As a bonus, Scala sample comes with tests, Scala language support and ProGuard obfuscator configured.

Now just start your Android emulator, go to the directory of the project of yours, build the project and deploy it to the running emulator.

hekonsek@Drone:~$ cd jayway-maven-android-plugin-samples/scala
hekonsek@Drone:~/jayway-maven-android-plugin-samples/scala$ mvn clean install android:deploy
...
[INFO] [android:deploy {execution: default-cli}]
...
[INFO] 170 KB/s (8959 bytes in 0.051s)
 pkg: /data/local/tmp/Scala-1.0.0-SNAPSHOT.apk
Success
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------

Now enjoy your new Android application deployed and running on the emulator.

2 comments:

  1. Hello, i have the same problem. Can you explain more detail what exactly should change in my project to compile valid apk?

    ReplyDelete
  2. Actually my post is a little out of date date. From what I know, the latest Maven Android archetype works like a charm. I would be grateful if you let me know if the rumors are true :) .

    ReplyDelete