android之使用mvn构建创造项目步骤

转自:http://blog.csdn.net/luhuajcdd/article/details/8132386

手动的创建自己的android application

 

1.用android tool 创建项目。

android create project 
--target <target_ID> 
--name <your_project_name> 
--path path/to/your/project 
--activity <your_activity_name> 
--package<your_package_namespace>
  • target is the "build target" for your application. It corresponds to an Android platform library (including any add-ons, such as Google APIs) that you would like to build your project against. To see a list of available targets and their corresponding IDs, execute: android list targets.
  • name is the name for your project. This is optional. If provided, this name will be used for your .apk filename when you build your application.
  • path is the location of your project directory. If the directory does not exist, it will be created for you.
  • activity is the name for your default Activity class. This class file will be created for you inside<path_to_your_project>/src/<your_package_namespace_path>/ . This will also be used for your .apk filename unless you provide a name.
  • package is the package namespace for your project, following the same rules as for packages in the Java programming language.

例如:创建一个 name:MyAndroidMavenApp; path:E:appmyapp; activity: MainActivity package: com.example.mvnandroid 的android项目。

 android create project --target --name MyAndroidMavenApp --path E:appmyapp --activity MainActivity --package com.example.mvnandroid

注意:前提已经在系统环境中配置android home 和 android tool

  参考:http://blog.csdn.net/luhuajcdd/article/details/8132429

2.在项目根目录中创建一个pom.xml文件,

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.     <modelVersion>4.0.0</modelVersion>  
        <!--包名-->
  4.     <groupId>com.example.mvnandroid</groupId>  
        <!---项目名-->
  5.     <artifactId>amvn_test</artifactId>  
  6.     <version>1.0.0-SNAPSHOT</version>  
        <!--打包为apk包-->
  7.     <packaging>apk</packaging>  
  8.     <name>amvn_t</name>  
  9.   
  10.     <dependencies>  
  11.         <dependency>  
  12.             <groupId>com.google.android</groupId>  
  13.             <artifactId>android</artifactId>  
  14.             <version>2.3.3</version>  
  15.             <scope>provided</scope>  
  16.         </dependency>  
  17.     </dependencies>  
  18.     <build>  
  19.         <finalName>${project.artifactId}</finalName>  
  20.         <sourceDirectory>src</sourceDirectory>  
  21.         <pluginManagement>  
  22.             <plugins>  
  23.                 <plugin>  
  24.                     <groupId>com.jayway.maven.plugins.android.generation2</groupId>  
  25.                     <artifactId>android-maven-plugin</artifactId>  
  26.                     <version>3.2.0</version>  
  27.                     <extensions>true</extensions>  
  28.                 </plugin>  
  29.             </plugins>  
  30.         </pluginManagement>  
  31.         <plugins>  
  32.             <plugin>  
  33.                 <groupId>com.jayway.maven.plugins.android.generation2</groupId>  
  34.                 <artifactId>android-maven-plugin</artifactId>  
  35.                 <configuration>  
  36.                     <run>  
  37.                         <debug>true</debug>  
  38.                     </run>  
  39.                     <sdk>  
  40.                         <path>${env.ANDROID_HOME}</path>  
  41.                         <platform>10</platform>  <!--当前项目的sdk版本-->
  42.                     </sdk>  
  43.                     <emulator>  
  44.                         <avd>emulator-5554_android</avd>  
  45.                     </emulator>  
  46.                     <undeployBeforeDeploy>true</undeployBeforeDeploy>  
  47.                 </configuration>  
  48.             </plugin>  
  49.         </plugins>  
  50.     </build>  
  51. </project>  

3. 在命令中创建的项目生产了一些文件, 在用android maven plugin 的时候, 有些文件是不需要的。

rm -r bin build.xml build.properties libs

4.构建项目

到项目MyAndroidMavenApp 的根目录:

mvn clean install 

可能出现的错误:

1.  Failed to execute goal on project amvn_test: Could not resolve dependencies for project com.example.mvnandroid:amvn_tes
droid:jar:4.1 in central (http://repo.maven.apache.org/maven2) ->

[html] view plaincopy
 
  1. <span style="white-space:pre">  </span><dependency>  
  2.             <groupId>com.google.android</groupId>  
  3.             <artifactId>android</artifactId>  
  4.             <version>4.1</version>  
  5.             <scope>provided</scope>  
  6.         </dependency>  

原因:dependency中version = 4.1 太高了,没有找到。 要修改。2.3.3是可以的

注意:您的系统必须已经有mvn, 配置过环境变量

  下载mvn:http://maven.apache.org/download.html

                         配置mvn 环境:http://www.cnblogs.com/smile2010/archive/2011/11/22/2259535.html

5.部署apk到连接的设备或模拟器上

     mvn android:deploy

可以出现的错误:

No plugin found for prefix 'anroid' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (C:Usershlu.m2 epository), central (http://repo.maven.apache.org/maven2)] -> [Help 1]

解决方法:

在C:Usershlu.m2 epositorysettings.xml中增加如下配置

[html] view plaincopy
 
    1. <pluginGroups>  
    2.     <!-- pluginGroup  
    3.      | Specifies a further group identifier to use for plugin lookup.  
    4.     <pluginGroup>com.your.plugins</pluginGroup>  
    5.     -->  
    6.       <pluginGroup>com.jayway.maven.plugins.android.generation2</pluginGroup>  
    7.   
    8.   </pluginGroups>  
原文地址:https://www.cnblogs.com/lee0oo0/p/3325259.html