使用maven對android項目進行構建

來源:互聯網
上載者:User

標籤:android   class   http   tar   ext   com   

軟體需求:

1.JDK 1.6+

2. Android SDk(r21.1 或者最新,版本越新支援越高)

3. Maven 3.1.1+

4. 添加環境到path,建立變數ANDROID_HOME,寫上你的sdk的安裝路徑,例如

ANDROID_HOME:E:\installspace\Android\android-sdk;path變數中添加%ANDROID_HOME%\tools and %ANDROID_HOME%\platform-tools。

檢測是否配置成功,控制台輸入android,如出現Android Sdk Manager,則表示配置成功!

注意:maven 可能出現Android-maven-plugin不相容;

Eclipse構建安裝Maven for Android 外掛程式

有兩種方式:

  • 在Eclipse中安裝[m2e-android][1]外掛程式

      安裝源:http://rgladwell.github.com/m2e-android/updates/

      安裝方法:Help -> Install new Software -> 在出來的對話方塊中點擊 Add ->Name:m2e-android

      Location:http://rgladwell.github.com/m2e-android/updates/

  • 在Eclipse中菜單中找到help-Eclipse MarketPlace中點擊,在彈出的對話方塊中,在search的tab頁下,find的文字框輸入“Android Configuration”,進行搜尋,點擊install安裝。

建立Maven Project項目
  1. 建立一個Maven Project (File -> New -> Project... then select Maven -> Maven Project).

  2. 在 Select Archetype 介面單擊 Add Archetype...
  3. 在 Archetype Group Id 輸入 "de.akquinet.android.archetypes"
  4. 在 Archetype Artifact Id 輸入 "android-quickstart".
  5. 在 Archetype Version 輸入 "1.1.0" 接著next
    • propertyies: platform: 16 (default 16) emulator: not-specified android-plugin-version 3.8.2
  6. Finish.
運行 project

    選中需要啟動並執行項目,右鍵run as-run configurations,雙擊Maven Build

    在Base directory 選擇你所啟動並執行項目路徑,

    Goals:clean install android:deploy android:run

    點擊run.

命令列構建

 

  1. 用android tool建立project
    • android list target
    • 這個顯示了可用的android platforms ,找到你想要的平台對編譯你的應用程式,記下目標id,建議選擇最高的版本。
      • 執行
        • android create project --target <target-id> --name Myapp --path <path-to-workspace>/Myapp --activity MainActivity--package com.example.myfirstapp

        • 將<target-id>換成你在剛剛顯示的列表中顯示的一個id值,將<path-to-workspace>換成應用存放的路徑。

  2. 在maven的~/.m2/setting.xml中,在profiles標籤之間增加如下,配置Android sdk的路徑。
    •     <profile>           <id>android</id>           <properties>               <android.sdk.path>                    E:\installspace\Android\android-sdk               </android.sdk.path>        </properties>        </profile>

    在 <activeProfiles></activeProfiles>標籤中加入

    <activeProfile>android</activeProfile>

  3. 在應用中增加檔案pom.xml,如下所示:
    • <?xml version="1.0" encoding="UTF-8"?><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>com.example.myfirstapp</groupId>  <artifactId>MyFirstApp</artifactId>  <version>1.0.0</version>  <packaging>apk</packaging>  <name>MyFirstApp</name>  <dependencies>    <dependency>      <groupId>com.google.android</groupId>      <artifactId>android</artifactId>      <version>4.1.1.4</version>      <scope>provided</scope>    </dependency>  </dependencies>  <build>    <sourceDirectory>src</sourceDirectory>    <finalName>${project.artifactId}</finalName>    <pluginManagement>      <plugins>        <plugin>          <groupId>com.jayway.maven.plugins.android.generation2</groupId>          <artifactId>android-maven-plugin</artifactId>          <version>3.8.2</version>          <extensions>true</extensions>        </plugin>      </plugins>    </pluginManagement>    <plugins>      <plugin>        <groupId>com.jayway.maven.plugins.android.generation2</groupId>        <artifactId>android-maven-plugin</artifactId>        <configuration>          <sdk>                        <!-- platform as api level (api level 16 = platform 4.1)-->            <platform>16</platform>          </sdk>        </configuration>      </plugin>    </plugins>  </build></project>
      在pom.xml中如下修改,
      • 修改groupId,artifactId和name
      • 設定version標籤,例如 <version>0.1.0-SNAPSHOT</version>.

      • 更新Android dependency
  4. build apk
     mvn clean install
  5. 將apk部署到裝置
    mvn android:deploy
  6. 直接運行

           clean install android:deploy android:run 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.