Maven开发Android指南 3 使用Android Maven Archetypes 创建新项目
在看完Maven开发Android指南 2 配置android-maven-plugin后,有人会疑惑怎么没有讲android-maven-plugin。这是Maven的特性之一,不需要手工下载plugin,配置pom.xml文件,只要是Maven Central?中有的,maven会自动下载对应的插件。
mvnarchetype:generate -DarchetypeArtifactId=android-quickstart-DarchetypeGroupId=de.akquinet.android.archetypes-DarchetypeVersion=1.0.8 ??-DgroupId=com.study.android -DartifactId=HelloMavenAndroid
<?xmlversion="1.0" encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
????????xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">
???<modelVersion>4.0.0</modelVersion>
???<groupId>com.study.android</groupId>
???<artifactId>HelloMavenAndroid</artifactId>
??? <version>1.0-SNAPSHOT</version>
??? <packaging>apk</packaging>
??? <name>HelloMavenAndroid</name>
?
??? <properties>
??????? <platform.version> 2.3.3
??????????? </platform.version>
??? </properties>
?
??? <dependencies>
??????? <dependency>
???????????<groupId>com.google.android</groupId>
???????????<artifactId>android</artifactId>
??????????? <version>${platform.version}</version>
??????????? <scope>provided</scope>
???????</dependency>
??? </dependencies>
?
??? <build>
??????? <plugins>
??????????? <plugin>
??????????????? <groupId>com.jayway.maven.plugins.android.generation2</groupId>
???????????????<artifactId>android-maven-plugin</artifactId>
???????????????<version>3.1.1</version>
??????????????? <configuration>
???????????????????<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
?????????????????? ?<assetsDirectory>${project.basedir}/assets</assetsDirectory>
???????????????????<resourceDirectory>${project.basedir}/res</resourceDirectory>
???????????????????<nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
??? ????????????????<sdk>
?????????????????????? ?<platform>10</platform>
??????????????????? </sdk>
???????????????????????????????????????<undeployBeforeDeploy>true</undeployBeforeDeploy>
??????????????? </configuration>
??????????????? <extensions>true</extensions>
??????????? </plugin>
?
??????????? <plugin>
???????????????<artifactId>maven-compiler-plugin</artifactId>
???????????????<version>2.3.2</version>
??????????????? <configuration>
???????????????????<source>1.6</source>
??????????????????? <target>1.6</target>
??????????????? </configuration>
??????????? </plugin>
??????? </plugins>
??? </build>
</project>
打包的类型:apk
使用Android平台:2.3.3(指向properties.platform.version)
配置AndroidMaven Plugin:3.1.1
?
一般的JAVA工程打包的类型是jar或者是war,而android工程打包的类型是apk。在执行中,告诉Android Maven Plugin生成的文件类型是apk。Android Maven Plugin会负责调用Android SDK将JAVA字节码转换成Dalvik 虚拟机识别的内容。在配置plugin中,Android Maven Plugin必须声明这是扩展的插件(<extensions>true</extensions>)。平台的版本和平台号码需要对应,<version>2.3.3</version>和<platform>10</platform>,可以在APILevel找到对应的关系。
?
依赖的Android平台可以在Maven Central查询到。在依赖中声明<scope>provided</scope>是很有必要的。意思是告诉Maven在打包是不用将AndroidSDK提供的jar打包到应用中。
?
创建AVD:
?
参考:详解如何创建和使用AVD
请注意在Target栏位选择:Android 2.3.3 – API Level 10
?
?
?
运行:
?
使用AVD Manager.exe创建好AVD,并Start后,
进入HelloMavenAndroid,输入maven命令,最后在模拟器中看到Hello HelloMavenAndroid
?
cd HelloMavenAndroid
mvn android:emulator-start
mvn clean package android:deployandroid:run
?
名词解释:
Maven Archetype Plugin:The Archetype Plugin allows the user to create a Mavenproject from an existing template called an archetype.
?
?
?
参考:
http://www.sonatype.com/books/mvnref-book/reference/android-dev-sect-helloandroidexample.html
http://www.sonatype.com/books/mvnref-book/reference/android-dev-sect-archetype.html
http://code.google.com/p/maven-android-plugin/wiki/GettingStarted
http://developer.android.com/tools/projects/projects-cmdline.html
http://maven.apache.org/archetype/maven-archetype-plugin/
http://blog.csdn.net/yaowj2/article/details/6794129
http://search.maven.org/
http://www.jizhuomi.com/android/environment/269.html
