Maven + Eclipse + Android 环境搭建

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 项目,可以根据如下步骤:

Create a new Maven Project (File -> New -> Project... then select Maven -> Maven Project).

When prompted to Select Archetype click Add Archetype...

In the dialog that appears enter "de.akquinet.android.archetypes" for Archetype Group Id.

In Archetype Artifact Id enter "android-quickstart".

In Archetype Version enter "1.0.10" and continue.

When prompted enter your desired project group and artifact ID, version and, optionally, set the "platform" property for the Android version (defaults to '16').

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: 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值