Maven + Eclipse + Android 环境搭建
Android官方开发团队致力于为Ecilpse插件提供可靠的支持,而android-maven-plugin致力于将Maven,Android和Eclipse串联在一起。m2e-android是一个MavenIntegrationforEclipse(m2e)plug-in,为AndroidDeveloperTools(ADT)和MavenAndroidPlugin添加Maven支持,为ADT提供Maven依赖管理的特性。安装m2e-android需要EclipseIndigoorJuno(低版本的Eclispe未提供EclipseMarketplace)
1.EssentialsTools:必备工具
1.1 AndroidDevelopmentTool(ADT)以及AndroidSDK。
这个是必须的,不用赘述。
1.2 AndroidDevelopmentToolsandtheM2Eclipse
这个是Eclipse的插件,充当的角色是ADT及m2e之间的桥梁,由于Android项目的特性,Maven所做的操作在Android的项目上并不适用,所以这个插件相当于两个插件之间的适配器,使得m2e的插件作出的变更及其通知能够以ADT所能接受的方式通知ADT对Android的项目作出响应的变更。
进入EclipseMarketplace.搜索androidm2e,然后点击Installer按钮即可。
本项目的Github托管地址为:
https://github.com/rgladwell/m2e-android
注意,目前该项目支持Eclipseindigo,juno,kepler三个版本。
1.3 AndroidConnector
这个是在Preferences->Maven->Discovery中选择AndroidConnector下载。
1.4Maven-android-sdk-deployer
该项目的github托管地址为:https://github.com/mosabua/maven-android-sdk-deployer
该项目的主要作用是使得MavenAndroid插件能够使用AndroidSDK作为Dependence。
(1)AndroidSDK中下载部署所有你需要的platforms与add-ones.
(2)下载Github上工程文件。
(3)使用命令行进入到工程目录下,使用mvninstall安装依赖项。
(4)如果需要安装特定级别的SDK,使用如下:
- mvninstall-P1.5
- mvninstall-P1.6
- mvninstall-P2.1
- mvninstall-P2.2
- mvninstall-P2.3.3
- mvninstall-P3.0
- mvninstall-P3.1
- mvninstall-P3.2
- mvninstall-P4.0
- mvninstall-P4.0.3
- mvninstall-P4.1
- mvninstall-P4.2
- mvninstall-P4.3
- mvninstall-P4.4
如果有如下错误,说明你的SDK中可能确实相应的platforms。
2 配置
安装好m2e之后,需要选择你本地的Maven的安装路径。
在你的settings文件中也可以添加如下内容:
<spanstyle="font-family:MicrosoftYaHei;"><settingsxmlns="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.0http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>android</id>
<properties>
<android.sdk.path>
E:\dev_tool\android-sdks(android-SDKhome路径)
</android.sdk.path>
</properties>
</profile>
</profiles>
<activeProfiles><!--maketheprofileactiveallthetime-->
<activeProfile>android</activeProfile>
</activeProfiles>
</settings></span>
3 工程
如果你已经有了一个安卓的工程,可以直接右击->Configure->ConverttoMavenProject转换为Maven项目,其中pom文件的packaging类型需要更改为apk.
<projectxmlns="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.0http://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项目,可以根据如下步骤:
1CreateanewMavenProject(File->New->Project...thenselectMaven->MavenProject).
2WhenpromptedtoSelectArchetypeclickAddArchetype...
3Inthedialogthatappearsenter"de.akquinet.android.archetypes"forArchetypeGroupId.
4InArchetypeArtifactIdenter"android-quickstart".
5InArchetypeVersionenter"1.0.10"andcontinue.
6WhenpromptedenteryourdesiredprojectgroupandartifactID,versionand,optionally,setthe"platform"propertyfortheAndroidversion(defaultsto'16').
7ClickFinish
注意,这里的de.akquinet.android.archetypes模板的version经常会改变,可以前往Maven的Central仓库搜索查看相应版本。
Eitherwayyoushould
在编译的时候,最好在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)NoAndroidSDKpathcouldbefound.
在settings.xml里面添加:
<profiles>
<profile>
<id>android</id>
<properties>
<android.sdk.path>
/Users/lily/android-sdk-macosx<!--此处为自己sdk路径-->
</android.sdk.path>
</properties>
</profile>
</profiles>
<activeProfiles><!--maketheprofileactiveallthetime-->
<activeProfile>android</activeProfile>
</activeProfiles>
或者在系统变量中创建ANDROID_HOME变量,或者直接在pom.xml中指定android.sdk.path。
(2)[ERROR]Failedtoexecutegoalcom.jayway.maven.plugins.android.generation2:android-maven-plugin:3.8.0:generate-sources(default-generate-sources)onprojectcontroltower-android:Executiondefault-generate-sourcesofgoalcom.jayway.maven.plugins.android.generation2:android-maven-plugin:3.8.0:generate-sourcesfailed:
以及
ProjectBuildError:unknownpackaging:apk
这是因为没有安装m2eandroidconnector,可以按照如下步骤。
Preferences->Maven->Discoveryandclick"OpenCatalog".然后选择m2eandroidconnector安装好以后就可以解决这个问题。
(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)Nocompilerisprovidedinthisenvironment.PerhapsyouarerunningonaJREratherthanaJDK?这个错误即是找不到JDK的错误,可以使用如下方案:
l在Eclipse的ini文件中添加如下语句:
-vm
C:\ProgramFiles\Java\jdk1.7.0_25\bin\javaw.exe
lWindow->Preferences->Java->InstalledJREs,点击"Add"按钮,选择默认的"StandardVM",点击"Next>",点击"JREhome"右边的"Directory"选择文件夹D:\Development\Java\jdk1.7.0_25\jre,一路“确定”,回到"InstalledJREs"界面,在刚添加的"jdk1.7.0_25"打上勾。再选择目录树中的"ExecutionEnvironments",点选界面左侧的"J2SE-1.7",然后在右侧点选"jdk1.7.0_25"。重启Eclipse,一切OK了。或者edit已经存在的jre,然后点击AddExtendsJARS按键,添加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>
<!--platformorapilevel(apilevel4=platform1.6)-->
<platform>18</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
提示:
Pluginexecutionnotcoveredbylifecycleconfiguration:
com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.8.0:consume-aar(execution:
default-consume-aar,phase:compile)
解决:
打开'Problems'视图:'Window'->'ShowView'->'Problems'
右键点击报错的地方,选择'QuickFix'
点击选择'Permanentlymarkgoalgenerateinpom.xmlasignoredinEclipsebuild'->'Finish'->'OK'
修改完后,右键项目—>Maven—>UpdateProject
(6)dependency=[com.actionbarsherlock:library:apklib:4.1.0:compile]notfoundin
workspacecom.actionbarsherlock.library换了artifactId了,是actionbarsherlock,并且使用4.4.0的版本..Pluginexecutionnotcoveredbylifecycleconfiguration:
5常用命令
Goal | Description |
android:aar | CreatesanAndroidArchive(aar)file. |
android:apk | Createstheapkfile.Bydefaultsignsitwithdebugkeystore. |
android:apklib | Createstheapklibfile. |
android:clean | Nodescription. |
android:deploy | Deploystheapk(s)ofthecurrentproject(s)toallattacheddevicesandemulators.Automaticallyskipsotherprojectsinamulti-modulebuildthatdonotusepackagingapkwithoutterminating. |
android:deploy-apk | DeploysaspecifiedAndroidapplicationapktoattacheddevicesandemulators.Bydefaultitwilldeploytoall,butasubsetorsingleonecanbeconfiguredwiththedeviceanddevicesparameters.Thisgoalcanbeusedinnon-androidprojectsandasstandaloneexecutiononthecommandline. |
android:deploy-dependencies | Deploysalldirectlydeclareddependenciesof<type>apk</type>inthisproject'spom. |
android:devices | DevicesMojolistsallattacheddevicesandemulatorsfoundwiththeandroiddebugbridge.ItusesthesamenamingconventionfortheemulatorasusedinotherplacesintheAndroidMavenPluginandaddsthestatusofthedeviceinthelist.TODOThegoalisverysimpleandcouldbeenhancedforbetterdisplay,averboseoptiontodisplayandtotaketheandroid.deviceparamterintoaccount. |
android:dex | ConvertscompiledJavaclassestotheAndroiddexformat. |
android:emma | AftercompiledJavaclassesuseemmatool |
android:emulator-start | EmulatorStartMojocanstarttheAndroidEmulatorwithaspecifiedAndroidVirtualDevice(avd). |
android:emulator-stop | EmulatorStartMojocanstoptheAndroidEmulatorwithaspecifiedAndroidVirtualDevice(avd). |
android:emulator-stop-all | EmulatorStopeAllMojowillstopallattacheddevices. |
android:generate-sources | GeneratesR.javabasedonresourcesspecifiedbytheresourcesconfigurationparameter.Generatesjavafilesbasedonaidlfiles. |
android:help | Displayhelpinformationonandroid-maven-plugin. mvnandroid:help-Ddetail=true-Dgoal=<goal-name>
todisplayparameterdetails. |
android:instrument | Runstheinstrumentationapkondevice. |
android:internal-integration-test | Internal.Donotuse. |
android:internal-pre-integration-test | Internal.Donotuse. |
android:lint | LintMojocanrunthelintcommandagainsttheproject.Implementsparsingparametersfrompomorcommandlineargumentsandsetsusefuldefaultsaswell.Warning,ifyouuseandroid.lint.enableClasspathand/orandroid.lint.enableLibrariesthebehaviorofthisgoalwillvarydependingonthephasewherethisgoalisexecuted.Seeandroid.lint.classpath/lintClassPathandandroid.lint.libraries/lintLibrariesformoredetails. |
android:manifest-update | UpdatesvariousversionattributespresentintheAndroidManifest.xmlfile. |
android:monkey | CanexecutetestsusingUI/ApplicationExerciserMonkey. |
android:monkeyrunner | Canexecutemonkeyrunnerprograms. |
android:ndk-build | Nodescription. |
android:proguard | ProcessesbothapplicationanddependencyclassesusingtheProGuardbytecodeobfuscator,minimzer,andoptimizer.Formoreinformation,seehttps://proguard.sourceforge.net. |
android:pull | Copyfileordirectoryfromalltheattached(orspecified)devices/emulators. |
android:push | Copyfiletoalltheattached(orspecified)devices/emulators. |
android:redeploy | Undeploysandthedeploys(=redeploys)theapk(s)ofthecurrentproject(s)toallattacheddevicesandemulators.Automaticallyskipsotherprojectsinamulti-modulebuildthatdonotusepackagingapkwithoutterminating. |
android:redeploy-apk | ReploysaspecifiedAndroidapplicationapktoattacheddevicesandemulators.Bydefaultitwilldeploytoall,butaasubsetorsingleonecanbeconfiguredwiththedeviceanddevicesparameters.ThissimplytriestoundeploytheAPKfirstandthendeployitagain.Thisgoalcanbeusedinnon-androidprojectsandasstandaloneexecutiononthecommandline. |
android:run | RunsthefirstActivityshowninthetop-levellauncherasdeterminedbyitsIntentfilters. Androidprovidesacomponent-basedarchitecture,whichmeansthatthereisno"main"functionwhichservesasanentrypointtotheAPK.There'sanhomogeneouscollectionofActivity(es),Service(s),Receiver(s),etc. TheAndroidtop-levellauncher(whosepurposeistoallowuserstolaunchotherapplications)usestheIntentresolutionmechanismtodeterminewhichActivity(es)toshowtotheenduser.Suchactivitiesareidentifiedbyatleast: 1Actiontype:android.intent.action.MAIN 2Category:android.intent.category.LAUNCHER
<activityandroid:name=".ExampleActivity"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
ThisMojowilltrytotolaunchthefirstactivityofthiskindfoundinAndroidManifest.xml.Incasemultipleactivitiessatisfytherequirementslistedaboveonlythefirstdeclaredoneisrun.Incasethereareno"Launcheractivities"declaredinthemanifestornoactivitiesdeclaredatall,thisgoalabortsthrowinganerror. ThedeviceparameteristakenintoconsiderationsopotentiallytheActivityfoundisstartedonallattacheddevices.TheapplicationwillNOTbedeployedandrunningwillsilentlyfailiftheapplicationisnotdeployed. |
android:uiautomator | Canexecutetestsusinguiuiautomator. |
android:undeploy | Undeploystheapk(s)ofthecurrentproject(s)toallattacheddevicesandemulators.Automaticallyskipsotherprojectsinamulti-modulebuildthatdonotusepackagingapkwithoutterminating. |
android:undeploy-apk | UndeploysaspecifiedAndroidapplicationapkfromattacheddevicesandemulators.Bydefaultitwillundeployfromall,butasubsetorsingleonecanbeconfiguredwiththedeviceanddevicesparameters.Youcansupplythepackageoftheapplicationand/oranapkfile.Thisgoalcanbeusedinnon-androidprojectsandasstandaloneexecutiononthecommandline. |
android:unpack | Unpacklibrariescodeanddependenciesintotarget.Thiscanbeusefulforusingtheproguardmavenplugintoprovidetheinputjars.Althoughitisencouragedtousetheproguardmojooftheandroidmavenplugin. |
android:zipalign | ZipalignMojocanrunthezipaligncommandagainsttheapk.Implementsparsingparametersfrompomorcommandlineargumentsandsetsusefuldefaultsaswell. |
更多相关文章
- Android的xml文件中@、@android:type、@*、?、@+的含义和区别
- Android布局文件中常用的属性
- Android工具箱之文件目录
- android布局文件中各属性所代表的意义
- Android中资源文件的Shape使用总结
- 【魔幻塔防】60关配置文件
- 【魔幻塔防】80关配置文件
- 【魔幻塔防】22关配置文件
- 【魔幻塔防】128关配置文件