Documentation in book form is available with the Android chapter of the free book Maven: The complete Reference in HTML and PDF format: http://www.sonatype.com/books/mvnref-book/reference/android-dev.html
Getting Started
Prerequisites
- JDK 1.5+ installed as required for Android development
- Android SDK (r07 or later) installed, preferably with all platforms, see http://developer.android.com/sdk/index.html
- Maven 3.0.3+ installed, see http://maven.apache.org/download.html
- Set environment variable ANDROID_HOME to the path of your installed Android SDK and add $ANDROID_HOME/tools as well as $ANDROID_HOME/platform-tools to your $PATH. (or on Windows %ANDROID_HOME%\tools and %ANDROID_HOME%\platform-tools).
- MacOS users: Note that for the path to work on the commandline and in IDE's started by launchd you have to set it in /etc/launchd.conf and NOT in .bashrc or something else
Recommended: Test your development environment
If you want to test your environment, download and run the Samples*.
Create your own Android application (apk) easily
The easiest way to create you own Android application project for Maven, is to use these Maven Archetypes:
https://github.com/akquinet/android-archetypes/wiki
Create your own Android application (apk) manually
Alternatively, these are the steps to create an Android project manually:
- Create a project using the android tool:
- Create a pom.xml in your project, using the sample helloflashlight/pom.xml as template. (See Samples*.)
- Make these changes:
- Change <groupId>, <artifactId> and <name> to your own. (Some tips: Maven - Guide to Naming Conventions)
- Set a <version> tag for your project, for example <version>0.1.0-SNAPSHOT</version>.
- Update the Android dependency to the platform version you want to be compatible with as a minimum:
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>1.5_r4</version>
<scope>provided</scope>
</dependency>The currently valid dependencies versions are 1.5_r4, 1.6_r2, 2.1.2 and 2.2.1. Use one of these depending on the platform version you are developing against.
- In the android-maven-plugin configuration, set the correct platform number which corresponds to the version you declared for the android dependency, for example: <platform>7</platform>. You can use API level (e.g. 7) or platform version (e.g. 2.1)
- You won't need these files/directories with Android Maven Plugin, so you can remove them:
rm -r bin build.xml build.properties libs
The default.properties file is currently not used by Android Maven Plugin, but will be, so leave that in place for now. default.properties and the gen folder are used by the ADT plugin in Eclipse, if you use that.
(Also leave the "test" folder for now. You can later move it out to its own project. If you want, you can then look at the apidemos-15-instrumentationtest sample project.)
- To build your apk, simply:
mvn install
- To deploy your apk to the connected device:
mvn android:deploy
See TipsAndTricks for instruction on how to use the android plugin name even when not in a android project directory.
Notes:
- Do not put tests in src/test/java that you want to be run by the Android Maven Plugin. If you put your tests there, Maven runs them as normal JUnit tests in the JVM, with the JAR files that are accessible on the classpath, which includes the android.jar. Because the android.jar only contains empty methods that throw exceptions, you get exceptions of the form java.lang.RuntimeException: Stub!. Instead, the tests need to go in src/main/java, where they will not be run by Maven as tests during compilation, but will be included in the apk and deployed and run on the device (where the android.jar has method implementations instead of stubs).
- If you have JUnit tests that don't call any Android APIs (directly or transitively), put them in src/test/java so JUnit runs them locally and more quickly than if run on the device.
- Make sure the Android Maven goal is set to <goal>jar-no-fork</goal> instead of <goal>test-jar-no-fork</goal>. Do this even for projects that only contain tests.
Run instrumentation tests on device
Check out the apidemos-15-instrumentationtest project in Samples*.
You can create a project like that, by moving your "test" folder out and renaming it to for example "myproject-instrumentationtest". Then rearrange the folders within it like above, and use the pom.xml from apidemos-15-instrumentationtest as template.
For a sample project with more feature features like jarsigning, zipaligin, release profile and proguard look at the morseflash example.
More documentation
There is some more documentation on the Documentation page.
(*) Samples
Always use the version of samples linked from the Samples page, so that you get a usable version of its configuration.
Please feel free to update this page with more details / corrections! (Just ask hugo@josefson.org for edit permissions, and you will get it.)
I want to demo this process at a talk I'm giving this week, and it would be fantastic to have a "create-project" target that would to all of this for you
Yes, please do demos! I have edited this page now and added a link to the Maven Archetypes which Clement Escoffier created for creating projects. I think using them is the easiest way to get started. Maven Archetypes are the standard way projects are created with Maven, so using them for Android projects is simply the right thing to do.
I am developing for android in eclipse, so I have downloaded the maven eclipse plugin. Where can i find environment variable ANDROID_HOME
@xardox69 You probably don't have the environment variable already, so you need to create it (set it). If you for example are using Windows 7, this is how you can find out how to set an environment variable: http://www.google.com/search?q=set+environment+variable+in+windows+7
If you don't use Windows 7, replace "Windows 7" in the Google search with the name of your operating system, for example OSX, Ubuntu or Windows XP.
After you have set the environment variable, you probably need to restart the application, in this case Eclipse. If that doesn't help, also try restarting the computer. That usually does the trick :)
I have a property file in asset folder, i want to override the values in this property file at project build time. mvn clean install -Durl=https://xyx.xom?
EX: assets/my_prop.properties #my_server_url=http://www.test.com/ change to my_server_url=${url}
i want to replace the my_server_url value at the build time what i did: mvn clean install -Durl=http://xys.com
but it's not replacing,how can i replace the my_server_url when doing the buid
Look at the morseflash example on how to do that..
is anyone working on the fixing the src/main/test issue when running it as Junit test in maven and failing because of the stubbed out implementation. Is there a way to package the src/main/test and deploying it to the emulator and run it in automated fashion without having to create a separate project for test as suggested ???? It would be really nice if we could still have the unit test inside the src/main/test and run it in automated fashion without failing it.
No.. since the testing code is a completely separate application that approach does not make sense. The SDK itself also needs a separate project.
Can we run code coverage(emma) on the set up above where we have two projects , one for the instrumentation test and one for the source code.
This plugin is trying to find my aapt in the wrong place. It is looking under android-sdk-versionXX/platforms/android-XX/tools/aapt, but the tools folder is now under the root android folder: android-sdk-versionXX. I have set my ANDROID-HOME environment variable and ANDROID-HOME/tools to my path...so what is the problem here?
+1 same question: Can we run code coverage(emma or cobertura or whatever) on the set up above where we have two projects , one for the instrumentation test and one for the source code.
+1same question: Can we run code coverage(emma or cobertura or whatever) on the set up above where we have two projects , one for the instrumentation test and one for the source code.
Hi isn't the variable ANDROID_SDK_HOME? This is the naming when you set up the android-sdk / eclipse environment with googles instructions.