Android是一種基于Linux的自由及開(kāi)放源代碼的操作系統,主要使用于便攜設備,如智能手機和平板電腦。2005年由Google收購注資,并組建開(kāi)放手機聯(lián)盟開(kāi)發(fā)改良隨后,逐漸擴展到平板電腦及其他領(lǐng)域上。Android的中間層基本上用Java 實(shí)現,采用Dalvik JAVA虛擬機。在運行時(shí),將Java字節碼轉換為Dalvik 虛擬機可以識別的.dex文件。JAVA是默認的開(kāi)發(fā)語(yǔ)言,API是基于JAVA。
大多數情況下,開(kāi)發(fā)Android應用使用基于Eclipse的Android Development Toolkit ADT。也可以使用Apache Ant來(lái)代替IDE的編譯工作。Android Maven Plugin則可以利用Apache Maven提供的功能,使開(kāi)發(fā)團隊完成編譯,部署和發(fā)布Android應用,還可以使用Maven強大的特性,如依賴(lài)管理,報表,代碼分析等等。
名詞解釋?zhuān)?/p>
開(kāi)放手機聯(lián)盟:開(kāi)放手機聯(lián)盟(Open Handset Alliance)是美國Google公司與2007年11月5日宣布組建的一個(gè)全球性的聯(lián)盟組織。這一聯(lián)盟將會(huì )支持Google可能發(fā)布的手機操作系統或者應用軟件,共同開(kāi)發(fā)名為Android的開(kāi)放源代碼的移動(dòng)系統。開(kāi)放手機聯(lián)盟包括手機制造商、手機芯片廠(chǎng)商和移動(dòng)運營(yíng)商幾類(lèi)。
Java字節碼(Java bytecode):Java bytecode is the form of instructions that the Java virtual machineexecutes. Each bytecode opcode is one byte in length, although some require parameters, resulting in some multi-byte instructions. Not all of the possible 256 opcodes are used. 51 are reserved for future use. Beyond that, Sun Microsystems, the original developer of the Java platform, has set aside three values to be permanently unimplemented
opcode:In computer science, an opcode (operation code) is the portion of a machine language instructionthat specifies the operation to be performed. Their specification and format are laid out in the instruction set architecture of the processor in question (which may be a general CPU or a more specialized processing unit). Apart from the opcode itself, an instruction normally also has one or more specifiers for operands (i.e. data) on which the operation should act, although some operations may have implicit operands, or none at all. There are instruction sets with nearly uniform fields for opcode and operand specifiers, as well as others (the x86architecture for instance) with a more complicated, varied length structure
Android NDK:The NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++. For certain types of apps, this can be helpful so you can reuse existing code libraries written in these languages, but most apps do not need the Android NDK.
參考:
Android: http://baike.baidu.com/view/1241829.htm
JAVA: http://baike.baidu.com/view/374952.htm
http://en.wikipedia.org/wiki/Java_bytecode
http://en.wikipedia.org/wiki/Opcode
http://developer.android.com/tools/sdk/ndk/index.html
在嘗試開(kāi)發(fā)或編譯Android應用之前,你需要安裝和配置JDK,Android SDK,Maven。
必備條件:
JDK 1.6+。
Android SDK (r17 or later) 支持所有版本的開(kāi)發(fā)。http://developer.android.com/sdk/index.html
Maven 3.0.3+。http://maven.apache.org/download.html
配置步驟:
JDK:
JDK的安裝配置不在本文介紹,可參考http://docs.oracle.com/javase/7/docs/webnotes/install/windows/jdk-installation-windows.html
在CMD中鍵入java -version。測試是否安裝成功:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Maven For Windows:
假定下載Maven后,解壓到目錄c:\Program Files\apache-maven-3.0.3。
使用Windows控制面板配置環(huán)境變量(可參照JDK配置):
配置M2_HOME:M2_HOME=c:\Program Files\apache-maven-3.0.3+
配置PATH:PATH=%PATH%;%M2_HOME%\bin+ …
在CMD中鍵入mvn -v。測試是否安裝成功:
Apache Maven 3.0.4 (r1232337; 2012-01-17 16:44:56+0800)
Maven home: C:\Program Files\apache-maven-3.0.3\bin\..
Java version: 1.6.0_22, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_22\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows xp", version: "5.1", arch: "x86", family: "windows"
Android SDK For Windows:
在A(yíng)ndroid官網(wǎng)(Android Developer web site)下載SDK。
假定下載Android后,解壓到目錄D:\android\android-sdk-windows
使用Windows控制面板配置環(huán)境變量(可參照JDK配置):
配置ANDROID_HOME:ANDROID_HOME=D:\android\android-sdk-windows
配置PATH:PATH=%PATH%;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;
參考:
http://code.google.com/p/maven-android-plugin/wiki/GettingStarted
http://docs.oracle.com/javase/7/docs/webnotes/install/windows/jdk-installation-windows.html
http://www.sonatype.com/books/mvnref-book/reference/installation-sect-test-install.html
http://www.sonatype.com/books/mvnref-book/reference/android-dev-sect-config-build.html
http://developer.android.com/sdk/exploring.html
在看完Maven開(kāi)發(fā)Android指南 2 配置android-maven-plugin后,有人會(huì )疑惑怎么沒(méi)有講android-maven-plugin。這是Maven的特性之一,不需要手工下載plugin,配置pom.xml文件,只要是Maven Central 中有的,maven會(huì )自動(dòng)下載對應的插件。
創(chuàng )建項目分兩種:手工創(chuàng )建和使用Maven Archetypes
手工創(chuàng )建:
使用android提供的工具創(chuàng )建項目:
http://developer.android.com/tools/projects/projects-cmdline.html
android create project \
--target <target_ID> \
--name <your_project_name> \
--path path/to/your/project \
--activity <your_activity_name> \
--package <your_package_namespace>
Maven Archetypes:
開(kāi)始一個(gè)新的項目可以使用Maven Archetype Plugin來(lái)創(chuàng )建項目的基本構架。有很多Android Maven Archetypes。如 android-with-test-archetype:創(chuàng )建包含測試代碼的Android工程;android-release-archetype創(chuàng )建包含發(fā)布功能的Android工程。
本例中將使用android-quickstart這個(gè)Maven Archetype Plugin。在Maven Central中搜索android-quickstart查詢(xún)Latest Version,目前最新的是1.0.8,在-DarchetypeVersion=1.0.8
archetypeArtifactId:Maven Archetype Plugin的ID
archetypeGroupId:Maven Archetype Plugin的類(lèi)別
archetypeVersion:Maven Archetype Plugin的版本號
groupId:工程的類(lèi)別(可以理解為JAVA包名)
artifactId:工程的ID(可以理解為工程的名稱(chēng))
mvn archetype:generate -DarchetypeArtifactId=android-quickstart -DarchetypeGroupId=de.akquinet.android.archetypes -DarchetypeVersion=1.0.8 -DgroupId=com.study.android -DartifactId=HelloMavenAndroid
Generating project in Interactive mode
一路回車(chē)就可以創(chuàng )建HelloMavenAndroid工程
在HelloMavenAndroid文件內,打開(kāi)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.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>
在這個(gè)pom.xml文件中高亮部分,分別是:
打包的類(lèi)型:apk
使用Android平臺:2.3.3(指向properties.platform.version)
配置Android Maven Plugin:3.1.1
一般的JAVA工程打包的類(lèi)型是jar或者是war,而android工程打包的類(lèi)型是apk。在執行中,告訴Android Maven Plugin生成的文件類(lèi)型是apk。Android Maven Plugin會(huì )負責調用Android SDK將JAVA字節碼轉換成Dalvik 虛擬機識別的內容。在配置plugin中,Android Maven Plugin必須聲明這是擴展的插件(<extensions>true</extensions>)。平臺的版本和平臺號碼需要對應,<version>2.3.3</version>和<platform>10</platform>,可以在API Level找到對應的關(guān)系。
依賴(lài)的Android平臺可以在Maven Central查詢(xún)到。在依賴(lài)中聲明<scope>provided</scope>是很有必要的。意思是告訴Maven在打包是不用將Android SDK提供的jar打包到應用中。
創(chuàng )建AVD:
請注意在Target欄位選擇:Android 2.3.3 – API Level 10
運行:
使用AVD Manager.exe創(chuàng )建好AVD,并Start后,
進(jìn)入HelloMavenAndroid,輸入maven命令,最后在模擬器中看到Hello HelloMavenAndroid
cd HelloMavenAndroid
mvn android:emulator-start
mvn clean package android:deploy android:run
名詞解釋?zhuān)?/strong>
Maven Archetype Plugin:The Archetype Plugin allows the user to create a Maven project 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://www.jizhuomi.com/android/environment/269.html
Android官方開(kāi)發(fā)團隊致力于為Ecilpse插件提供可靠的支持,而android-maven-plugin致力于將Maven,Android和Eclipse串聯(lián)在一起。
m2e-android是一個(gè) Maven Integration for Eclipse (m2e) plug-in,為Android Developer Tools (ADT)和 Maven Android Plugin添加Maven支持,為ADT提供Maven依賴(lài)管理的特性。
安裝m2e-android需要Eclipse Indigo or Juno (低版本的Eclispe未提供Eclipse Marketplace)
Help -> Eclipse Marketplace... 輸入 "android m2e".點(diǎn)擊Install(包含了Android Development Toolkit 和Maven for Eclipse m2e plug-in)。
如果是已經(jīng)使用《Maven開(kāi)發(fā)Android指南 3 使用Android Maven Archetypes 創(chuàng )建新項目》的方法創(chuàng )建項目,直接使用Eclipse Import:File->Maven->Existing Maven Projects
也可以使用m2e-android利用Maven Android archetypes創(chuàng )建新的項目:








參考:
http://rgladwell.github.com/m2e-android/
http://code.google.com/p/maven-android-plugin/wiki/EclipseIntegration
對于一個(gè)使用Maven的Android應用,調試看起來(lái)是個(gè)大問(wèn)題,沒(méi)有"Debug as Android application"怎么辦?總不能一直用logcat(Log.v(TAG,…)吧。
如果閱讀了《Maven開(kāi)發(fā)Android指南 4 與Eclipse整合(m2e-android )》,那么這就不是問(wèn)題了。
m2e-android很智能的將 Android Developer Tools (ADT)和 Maven Android Plugin結合在一起,細心的讀者可以看到,左上角項目圖標上面有一個(gè)M和A,分別代表著(zhù)Maven和Android。

正如上篇提到到的,可以"Run as Android application",那么當然可以"Debug as Android application"
但是還是有些讀者不滿(mǎn)足,就是不喜歡"Debug as Android application",那么DDMS將是你的選擇。(需要安裝m2e-android)

通過(guò)命令部署Android應用
cd HelloM2EAndroid
mvn android:emulator-start
mvn clean package android:deploy
打開(kāi)Eclipse中的DDMS,選擇對應的應用,點(diǎn)擊綠色的小甲殼蟲(chóng),它將會(huì )幫你抓住所有斷點(diǎn)。
名詞解釋?zhuān)?/strong>
logcat是Android中一個(gè)命令行工具,可以用于得到程序的log信息。
The Android logging system provides a mechanism for collecting and viewing system debug output. Logs from various applications and portions of the system are collected in a series of circular buffers, which then can be viewed and filtered by the logcat command. You can use logcat from an ADB shell to view the log messages
DDMS的全稱(chēng)是Dalvik Debug Monitor Service,是 Android 開(kāi)發(fā)環(huán)境中的Dalvik虛擬機調試監控服務(wù)。它為我們提供例如:為測試設備截屏,針對特定的進(jìn)程查看正在運行的線(xiàn)程以及堆信息、Logcat、廣播狀態(tài)信息、模擬電話(huà)呼叫、接收SMS、虛擬地理坐標等等。
Android ships with a debugging tool called the Dalvik Debug Monitor Server (DDMS), which provides port-forwarding services, screen capture on the device, thread and heap information on the device, logcat, process, and radio state information, incoming call and SMS spoofing, location data spoofing, and more
參考:
http://code.google.com/p/maven-android-plugin/wiki/Debug
http://baike.baidu.com/view/4504801.htm
http://baike.baidu.com/view/2688850.htm
聯(lián)系客服