2014年7月3日 星期四

整合maven 及 eclipse開發android app

起源:


使用lucene在android發生此錯誤:

ERROR/AndroidRuntime(733): FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at org.apache.lucene.index.SegmentInfos.read(SegmentInfos.java:332)
at org.apache.lucene.index.StandardDirectoryReader
...

有時是這個錯誤:
I/TestRunner(2443): java.lang.NoClassDefFoundError: org/apache/lucene/codecs/Codec
I/TestRunner(2443): at org.apache.lucene.index.LiveIndexWriterConfig.<init>(LiveIndexWriterConfig.java:118)
I/TestRunner(2443): at org.apache.lucene.index.IndexWriterConfig.<init>(IndexWriterConfig.java:144)
I/TestRunner(2443):


有人說要用maven來解決:
Googling turns up some interesting results.

https://issues.apache.org/jira/browse/LUCENE-4204

According to the issue above,the Android APK builder removes some files in the META-INF/services directory,which is required by Lucene。They suggest some workarounds,like adding the necessary files from the lucene jars directly to the APK via some ant tasks done after the APK builder is done。Just be careful,since some lucene jars use the same files in the META-INF/services directory,and you may overwrite them uneccessarily.

I would actually suggest that you use the android maven plugin,as they have applied a fix for this already (see https://code.google.com/p/maven-android-plugin/issues/detail?id=97)。Just use the latest version of the plugin (or any version above 3.2.1).





正文

如果不是因為開發在android的lucene專案,遇到難以克服的困難,非得用到maven來解決,我大概絕不會碰這個東西,因為資料實在太難找,每份文件好像都只講一部分,重要部分,都語焉不詳,軟體名稱,都很類似,翻成中文後,更不知那個是那個。對我這個新手而言,把一切觀念都理清,著實花了好大的力氣。而且這東西,目前還看不出它的價值在那裡, 因為我是新手,花這麼大的力氣去研究它值不值得,但反正時間己經花了,也終於有些懂了,就把這幾天的心得,記錄一下,如果有第二個跟我一樣的人,不必再白費力氣去看網路上那些難懂的文章了。

個人認為寫的最清楚的是這篇文章,如果不是它,我想我還在maven的迷宮裡繞不出來,這裡特別感謝一下。
http://blog.csdn.net/wangwei_cq/article/details/17070307
以下引用一下:
「大多數情況下,開發Android使用基於Eclipse的Android Development Toolkit ADT。也可以使用Apache Ant來代替IDE的編譯工作。Android Maven Plugin則可以利用Apache Maven提供的功能,使開發團隊完成編譯,部署和發佈Android應用,還可以使用Maven強大的特性,如依賴管理,報表,代碼分析等等。」


我想大部分的人都是用eclipse的adt來開發測試,至於maven的強大特性,等時間來證明吧!


還有這個也對我理清觀念很有幫助:
http://www.vogella.com/tutorials/AndroidBuildMaven/article.html


引述內容:
「1.1。Android SDK and ADT

The Android SDK provides tooling for the command line to create and build Android projects.
The Android Developer Tools (ADT) offer the same functionality for Eclipse。In Eclipse you can manually export Android applications via the Eclipse wizard.
To build and deploy Android application the Android SDK provide tools which are currently based on Apache Ant.
The project lead of the ADT has recently announced that ADT is moving to a Gradle based build system and that the Eclipse tooling is also going to be based on the same build system.
Tooling for building Android Application with Maven are also available。The move of ADT to Gradle makes the support of Maven easier as Gradle uses Maven repositories to manage its dependencies。」


他先提到android sdk 與 adt的不同,sdk 是在命令列下建立android projects,而adt則在eclipse圖形介面內提供同樣功能。而android adk目前是用ant來建立及佈署android的app。而adt最近則宣佈用gradle來做這件事(建立及佈署android的app)。maven也同樣可用。只是從maven改到gradle,似乎更方便管理dependencies。


關於gradle,請參考:
http://www.codedata.com.tw/java/understanding-gradle-2-maven/
http://ithelp.ithome.com.tw/question/10129873



其他的內容,他還提到如果要在eclipse的adt之外,建立佈署android app,當然要做的事就多了,要很熟悉指令操作,要建立好環境變數等。因為我也是新手,這種事當然做不來,所以以下的操作,都是針對eclipse 的adt環境。


開始


必備條件:
1)JDK 1.6+。
2)Android SDK (r17 or later) 。http://developer.android.com/sdk/index.html
3)Maven 3.0.3+。http://maven.apache.org/download.html


以上如果是要在eclipse(adt)的環境裡使用,都是不需要的,因為前兩個,如果您已安裝好adt,而且已經正常使用adt開發app,那就已經具備了,可以不管。第三個maven,我是裝eclipse內的maven plugin來用,我也沒安裝這項,總之1,2,3項,我都沒特別去安裝。(會不會是安裝某些東西,自動裝上去的,我不敢保證)
所以基本我的認知上,除非你要在命令列模式使用maven,否則第三項也不用安裝。


安裝eclipse相關插件


主要有三個:
1) Android for Maven Eclipse 1.01 (m2e-android)
為Android Developer Tools (ADT)和 Maven Android Plugin添加Maven支持,為ADT提供Maven依賴管理的特性。

2) Maven Integration for Eclipse 1.5(m2e)
eclipse下的maven

3) Android Maven Plugin)
將Maven,Android和Eclipse串聯在一起




安裝Maven Integration for Eclipse 1.5

Help -> Install New Software...
在work with 的地方輸入http://download.eclipse.org/releases/luna/,按add,輸入一個名字。
選擇http://download.eclipse.org/releases/luna/
在type filter text 輸入 maven,會找到Maven Integration for Eclipse 1.5。可安裝成功。 (安裝1.5似乎會有問題, 1.4比較穩定, 在work with 用Kepler - http://download.eclipse.org/releases/kepler 可找到1.4版)




安裝Android for Maven Eclipse 1.01 (m2e-android)

Help -> Install New Software...
在work with 的地方輸入http://rgladwell.github.com/m2e-android/updates/,按add,輸入一個名字。
選擇http://rgladwell.github.com/m2e-android/updates/
在type filter text 輸入 android,會找到 Android for Maven Eclipse



安裝過程中,出現:

you are installing software that contains unsigned content。the authenticity or validity of this software cannot be established。do you want to continue with the installation?
選OK繼續即可,完成後重啟eclipse adt。


還有一個叫Maven Android Plugin(後來改名叫Android Maven Plugin)並未出現,因為它不用安裝,它會在稍後出現的pom.xml檔中被指定,並自動下載安裝,所以這個不必管。



用maven建立新專案


從Eclipse ADT,file->new->other->maven->maven project
圖一,直接按next
2014-07-02_1428171.png

圖二 在filter輸入de.akquinet.android.archetypes,出現三個項目,選第一個android-quickstart。

2014-07-02_1429071.png

圖三,填入相關資訊,注意預設platform是16,android-plugin-version的版本是3.8.2。事後可能會修改。


2014-07-02_1429071.png

錯誤修正:


建立完maven的新專案時,可能會有一些錯誤:

1)使用maven建立新專案時,會缺android.jar
在project->java build path中加入:D:\adt-bundle-windows-x86-20140321\sdk\platforms\android-19\android.jar (如果使用api 19)


2)在project->java build path中顯示src\test\java(missing)
請手動在src下建立\test\java目錄


3)gen目錄下的r.java無法自動產生,可能原因如下
主menu的project->android->project build target中的target name未打勾,導致gen目錄下r.java不能產生.
也有可能此處的api版本與AndroidManifest.xml中不一致所導致(或其他地方不一致)。在上圖三中預設的api是16,可是adt預設是19


4)前文提到的pom.xml在此時已產生,但可能有一些問題如下:

<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>${android.plugin.version}</version>
<extensions>true</extensions>
</plugin>


使用<version>${android.plugin.version}</version> 會出現錯誤:
改成<version>3.9.0-rc.2</version> 才正確



5)專案內的全部錯誤均消失,但專案名稱仍有錯誤記號
導致無法執行,請在專案中,右鍵->maven->update project..
下列三項打勾:
update project configuration...
refresh workspace...
clean projects...
->按Ok,錯誤即消失


執行測試

建好要測試的設備(用虛擬機或實體設備,我是用wifi over air 直接連到平板測試, 但用wifi遇到問題)
專案,右鍵->run as...-> build...
在視窗的goal輸入合適的命令:

以下是使用Maven構建Android項目中常用的一些命令,你可以根據需要選擇合適的命令。
(在maven的命令列中使用 mvn clean package, 在eclipse -> run as->maven build…->goal, 去掉mvn輸入clean package即可)

mvn clean package
clean:會刪target的內容, package:打包(會產生dex檔),但不部署。 (建立convert to dex時, 如果記憶體太小, 會很慢, 請儘量減少不必要的程式, 至少要有700M的free memory. )

mvn clean install
打包,部署。

mvn clean package android:redeploy android:run
這個命令通常用於手機上已經安裝了要部署的應用,但簽名不同,所以我們打包的同時使用redeploy命令將現有應用刪除並重新部署,最後使用run命令運行應用。

mvn android:redeploy android:run
不打包,將已生成的包重新部署並在andriod設備運行。

mvn android:deploy android:run
部署並運行已生成的包,與redeploy不同的是,deploy不會刪除已有部署和應用數據。

(專案內的target目錄內放的是maven所產生的package, eclipse所產生的放在bin目錄內. )


再讓我引述別人的智慧,供以後參考:
http://mkn939.blogspot.tw/2013/03/maven-eclipsemaven-command-mode-step-by.html

「這個執行方法並沒有把Eclipse很完整的整合進來,
要回command mode打包war,還要手動deploy到server上,感覺有點累
但好處是Maven專案跟Eclipse切的很乾淨,
Eclipse只負責修改Java檔.其他的動作都由Maven完成,
避免使用太多套件(Eclipse plugin)造成Debug不易(鬼打牆事件)

如果使用Eclipse+m2eclipse開發,deploy時出現很奇怪的行為的時候
建議回到純Maven的操作方式,能方便找出問題的源頭」



以上指的是直接用命令列的方式,可以避開與eclipse整合在一起,出現一些奇怪的問題。

Read more...

  © Blogger templates Psi by Ourblogtemplates.com 2008

Back to TOP