Android官方开发团队致力于为Ecilpse插件提供可靠的支持,而android-maven-plugin致力于将Maven,Android和Eclipse串联在一起。m2e-android是一个 Maven Integration for Eclipse (m2e) plug-in,为Android Developer Tools (ADT)和 Maven Android Plugin添加Maven支持,为ADT提供Maven依赖管理的特性。 安装m2e-android需要Eclipse Indigo or Juno (低版本的Eclispe未提供Eclipse Marketplace)
1.Essentials Tools:必备工具
1.1 Android Development Tool(ADT)以及Android SDK。
这个是必须的,不用赘述。
1.2 Android Development Tools and the M2Eclipse
这个是Eclipse的插件,充当的角色是ADT及m2e之间的桥梁,由于Android项目的特性,Maven所做的操作在Android的项目上并不适用,所以这个插件相当于两个插件之间的适配器,使得m2e的插件作出的变更及其通知能够以ADT所能接受的方式通知ADT对Android的项目作出响应的变更。
进入Eclipse Marketplace.搜索android m2e ,然后点击Installer按钮即可。
本项目的Github托管地址为:
https://github.com/rgladwell/m2e-android
注意,目前该项目支持Eclipse indigo,juno,kepler三个版本。
1.3 Android Connector
这个是在Preferences->Maven->Discovery中选择Android Connector下载。
1.4 Maven-android-sdk-deployer
该项目的github托管地址为:https://github.com/mosabua/maven-android-sdk-deployer
该项目的主要作用是使得Maven Android插件能够使用Android SDK作为Dependence。
(1)Android SDK中下载部署所有你需要的platforms与add-ones.
(2)下载Github 上工程文件。
(3)使用命令行进入到工程目录下,使用mvn install安装依赖项。
(4)如果需要安装特定级别的SDK,使用如下:
- mvn install -P 1.5
- mvn install -P 1.6
- mvn install -P 2.1
- mvn install -P 2.2
- mvn install -P 2.3.3
- mvn install -P 3.0
- mvn install -P 3.1
- mvn install -P 3.2
- mvn install -P 4.0
- mvn install -P 4.0.3
- mvn install -P 4.1
- mvn install -P 4.2
- mvn install -P 4.3
- mvn install -P 4.4
如果有如下错误,说明你的SDK中可能确实相应的platforms。
2 配置
安装好m2e之后,需要选择你本地的Maven的安装路径。
在你的settings文件中也可以添加如下内容:
<span style="font-family:Microsoft YaHei;"><settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>android</id>
<properties>
<android.sdk.path>
E:\dev_tool\android-sdks(android-SDK home路径)
</android.sdk.path>
</properties>
</profile>
</profiles>
<activeProfiles> <!--make the profile active all the time -->
<activeProfile>android</activeProfile>
</activeProfiles>
</settings></span>
3 工程
如果你已经有了一个安卓的工程,可以直接右击->Configure->Convert to Maven Project转换为Maven项目,其中pom文件的packaging类型需要更改为apk.
<project xmlns="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.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.cst.ming.android.sample</groupId>
<artifactId>sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>apk</packaging>
如果要新建一个Android 项目,可以根据如下步骤:
1 Create a new Maven Project (File -> New -> Project... then select Maven -> Maven Project).
2 When prompted to Select Archetype click Add Archetype...
3 In the dialog that appears enter "de.akquinet.android.archetypes" for Archetype Group Id.
4 In Archetype Artifact Id enter "android-quickstart".
5 In Archetype Version enter "1.0.10" and continue.
6 When prompted enter your desired project group and artifact ID, version and, optionally, set the "platform" property for the Android version (defaults to '16').
7 Click Finish
注意,这里的de.akquinet.android.archetypes模板的version经常会改变,可以前往Maven的Central仓库搜索查看相应版本。
Either way you should
在编译的时候,最好在pom文件中声明一个build插件:
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.0.0</version>
<inherited>true</inherited>
<extensions>true</extensions>
<configuration>
<sdk>
<platform>7</platform>
<path>${env.ANDROID_SDK_HOME}</path>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
</plugin>
4 常见问题
(1)No Android SDK path could be found.
在settings.xml里面添加:
<profiles>
<profile>
<id>android</id>
<properties>
<android.sdk.path>
/Users/lily/android-sdk-macosx <!-- 此处为自己sdk路径 -->
</android.sdk.path>
</properties>
</profile>
</profiles>
<activeProfiles> <!--make the profile active all the time -->
<activeProfile>android</activeProfile>
</activeProfiles>
或者在系统变量中创建ANDROID_HOME变量,或者直接在pom.xml中指定android. sdk.path。
(2)[ERROR] Failed to execute goal com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.8.0:generate-sources(default-generate-sources) on project controltower-android: Executiondefault-generate-sources of goalcom.jayway.maven.plugins.android.generation2:android-maven-plugin:3.8.0:generate-sourcesfailed:
以及
Project Build Error:unknown packaging:apk
这是因为没有安装m2e android connector,可以按照如下步骤。
Preferences -> Maven -> Discovery and click "Open Catalog".然后选择 m2e android connector 安装好以后就可以解决这个问题。
(3)@Override 的代码全部都报错
默认Maven中的JAVA版本是1.5,只要修改成1.6就好了
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
(4)No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?这个错误即是找不到JDK的错误,可以使用如下方案:
l 在Eclipse的ini文件中添加如下语句:
-vm
C:\Program Files\Java\jdk1.7.0_25\bin\javaw.exe
l Window ->Preferences -> Java -> Installed JREs, 点击"Add"按钮,选择默认的"Standard VM", 点击"Next >", 点击"JRE home"右边的"Directory"选择文件夹D:\Development\Java\jdk1.7.0_25\jre,一路“确定”,回到"Installed JREs"界面,在刚添加的"jdk1.7.0_25"打上勾。再选择目录树中的"Execution Environments",点选界面左侧的"J2SE-1.7",然后在右侧点选"jdk1.7.0_25"。重启Eclipse,一切OK了。或者edit已经存在的jre,然后点击Add Extends JARS按键,添加jdk\lib\tools.jar文件,同样可以解决。
(5)导入工程后,pom.xml文件中,以下插件报错
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<sdk>
<!-- platform or api level (api level 4 = platform 1.6)-->
<platform>18</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
提示:
Plugin execution not covered by lifecycle configuration:
com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.8.0:consume-aar (execution:
default-consume-aar, phase: compile)
解决:
打开 'Problems'视图: 'Window' -> 'Show View' -> 'Problems'
右键点击报错的地方,选择'Quick Fix'
点击选择'Permanently mark goal generate in pom.xml as ignored in Eclipse build' -> 'Finish' -> 'OK'
修改完后,右键项目—> Maven —> Update Project
(6)dependency=[com.actionbarsherlock:library:apklib:4.1.0:compile] not found in
workspace com.actionbarsherlock.library 换了artifactId了,是actionbarsherlock,并且使用4.4.0的版本..Plugin execution not covered by lifecycle configuration:
5 常用命令
Goal |
Description |
Creates an Android Archive (aar) file. |
|
Creates the apk file. By default signs it with debug keystore. |
|
Creates the apklib file. |
|
No description. |
|
Deploys the apk(s) of the current project(s) to all attached devices and emulators. Automatically skips other projects in a multi-module build that do not use packaging apk without terminating. |
|
Deploys a specified Android application apk to attached devices and emulators. By default it will deploy to all, but a subset or single one can be configured with the device and devices parameters.This goal can be used in non-android projects and as standalone execution on the command line. |
|
Deploys all directly declared dependencies of <type>apk</type> in this project's pom. |
|
DevicesMojo lists all attached devices and emulators found with the android debug bridge. It uses the same naming convention for the emulator as used in other places in the Android Maven Plugin and adds the status of the device in the list. TODO The goal is very simple and could be enhanced for better display, a verbose option to display and to take the android.device paramter into account. |
|
Converts compiled Java classes to the Android dex format. |
|
After compiled Java classes use emma tool |
|
EmulatorStartMojo can start the Android Emulator with a specified Android Virtual Device (avd). |
|
EmulatorStartMojo can stop the Android Emulator with a specified Android Virtual Device (avd). |
|
EmulatorStopeAllMojo will stop all attached devices. |
|
Generates R.java based on resources specified by the resources configuration parameter. Generates java files based on aidl files. |
|
Display help information on android-maven-plugin. mvn android:help -Ddetail=true -Dgoal=<goal-name>
to display parameter details. |
|
Runs the instrumentation apk on device. |
|
Internal. Do not use. |
|
Internal. Do not use. |
|
LintMojo can run the lint command against the project. Implements parsing parameters from pom or command line arguments and sets useful defaults as well. Warning, if you use android.lint.enableClasspath and/or android.lint.enableLibraries the behavior of this goal will vary depending on the phase where this goal is executed. See android.lint.classpath/lintClassPath and android.lint.libraries/lintLibraries for more details. |
|
Updates various version attributes present in the AndroidManifest.xml file. |
|
Can execute tests using UI/Application Exerciser Monkey. |
|
Can execute monkey runner programs. |
|
No description. |
|
Processes both application and dependency classes using the ProGuard byte code obfuscator, minimzer, and optimizer. For more information, see https://proguard.sourceforge.net. |
|
Copy file or directory from all the attached (or specified) devices/emulators. |
|
Copy file to all the attached (or specified) devices/emulators. |
|
Undeploys and the deploys (= redeploys) the apk(s) of the current project(s) to all attached devices and emulators. Automatically skips other projects in a multi-module build that do not use packaging apk without terminating. |
|
Reploys a specified Android application apk to attached devices and emulators. By default it will deploy to all, but a asubset or single one can be configured with the device and devices parameters. This simply tries to undeploy the APK first and then deploy it again. This goal can be used in non-android projects and as standalone execution on the command line. |
|
Runs the first Activity shown in the top-level launcher as determined by its Intent filters. Android provides a component-based architecture, which means that there is no "main" function which serves as an entry point to the APK. There's an homogeneous collection of Activity(es), Service(s), Receiver(s), etc. The Android top-level launcher (whose purpose is to allow users to launch other applications) uses the Intent resolution mechanism to determine which Activity(es) to show to the end user. Such activities are identified by at least: 1 Action type: android.intent.action.MAIN 2 Category: android.intent.category.LAUNCHER
<activity android:name=".ExampleActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
This Mojo will try to to launch the first activity of this kind found in AndroidManifest.xml. In case multiple activities satisfy the requirements listed above only the first declared one is run. In case there are no "Launcher activities" declared in the manifest or no activities declared at all, this goal aborts throwing an error. The device parameter is taken into consideration so potentially the Activity found is started on all attached devices. The application will NOT be deployed and running will silently fail if the application is not deployed. |
|
Can execute tests using ui uiautomator. |
|
Undeploys the apk(s) of the current project(s) to all attached devices and emulators. Automatically skips other projects in a multi-module build that do not use packaging apk without terminating. |
|
Undeploys a specified Android application apk from attached devices and emulators. By default it will undeploy from all, but a subset or single one can be configured with the device and devices parameters. You can supply the package of the application and/or an apk file. This goal can be used in non-android projects and as standalone execution on the command line. |
|
Unpack libraries code and dependencies into target. This can be useful for using the proguard maven plugin to provide the input jars. Although it is encouraged to use the proguard mojo of the android maven plugin. |
|
ZipalignMojo can run the zipalign command against the apk. Implements parsing parameters from pom or command line arguments and sets useful defaults as well. |