阅读84 返回首页    go 阿里云 go 技术社区[云栖]


maven-android-plugin

原文:https://code.google.com/p/maven-android-plugin/wiki/GettingStarted


Documentation in book form is available with the Android chapter of the free book Maven: The complete Reference in HTML and PDF format:https://www.sonatype.com/books/mvnref-book/reference/android-dev.html

Getting Started

Prerequisites

  1. JDK 1.6+ installed as required for Android development
  2. Android SDK (r17 or later, latest is best supported) installed, preferably with all platforms, see https://developer.android.com/sdk/index.html
  3. Maven 3.0.3+ installed, see https://maven.apache.org/download.html
    1. 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).
    2. 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>

Check https://search.maven.org/ for available versions. 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 theapidemos-15-instrumentationtest sample project.)
To build your apk, simply:
     mvn clean 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:

  1. 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).
  2. 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.
  3. 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. 



最后更新:2017-04-04 07:03:39

  上一篇:go android 关于EditTextPreference光标位置
  下一篇:go Core Data浅谈系列之七 : 使用NSFetchedResultsController