1. 程式人生 > >Android工程在Maven環境下打包

Android工程在Maven環境下打包

一、Maven開發環境搭建

1、下載安裝Maven:

http://maven.apache.org/download.html

2、配置Maven環境變數:



配置好環境變數以後,在cmd中執行mvn -v來檢查maven環境變數是否配置完成,見下圖:

3、配置Maven在Android環境下的外掛maven-android-plugin:

(1)從以下地址下載外掛到本地:https://code.google.com/p/maven-android-plugin/wiki/GettingStarted

(2)通過cmd進入下載的檔案目錄下,執行mvn clean install安裝android平臺下得全部apis(PS:這裡需要注意下,需要下載全部Android apis,否則會報錯,包括Extras下得一些外掛)

二、配置maven打包指令碼

1、如果使用了第三方jar包,通過以下命令講第三方jar安裝到maven庫下:

mvn install:install-file -DgroupId=com.destiny.demo -DartifactId=json -Dversion=1.1 -Dfile=C:/Users/Destiny/Desktop/Destiny_Demo/libs/json_simple-1.1.jar -Dpackaging=jar -DgeneratePom=true

2、如果使用了第三方的.so檔案,也是通過以下命令將.so安裝到maven庫下:

mvn install:install-file -DgroupId=com.destiny.demo -DartifactId=sotest -Dversion=v1 -Dfile=C:/Users/Destiny/Desktop/Destiny_Demo/libs/armeabi/test.so -Dpackaging=so -DgeneratePom=true -Dclassifier=armeabi

3、將以下pom.xml檔案拷貝到自己的android工程目錄下:

<?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>

	<!-- 舉個例子,應用包名為:com.destiny.demo -->
	<groupId>com.destiny</groupId>
	<artifactId>demo</artifactId>
	<!-- version:應用版本號 -->
	<version>2.0.0</version>
	<!-- packaging:打包方式 -->
	<packaging>apk</packaging>

	<!-- 這裡填寫工程名 -->
	<name>Destiny_Demo</name>

	<!-- 新增第三方資源 -->
	<dependencies>
		<dependency>
			<groupId>com.google.android</groupId>
			<artifactId>android</artifactId>
			<version>2.3.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.destiny.demo</groupId>
			<artifactId>sotest</artifactId>
			<version>v1</version>
			<classifier>armeabi</classifier>
			<type>so</type>
		</dependency>
		<dependency>
			<groupId>com.destiny.demo</groupId>
			<artifactId>json</artifactId>
			<version>1.1</version>
			<type>jar</type>
		</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>10</platform>
					</sdk>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jarsigner-plugin</artifactId>
				<version>1.2</version>
				<executions>
					<execution>
						<id>signing</id>
						<goals>
							<goal>sign</goal>
						</goals>
						<phase>package</phase>
						<inherited>true</inherited>
						<configuration>
							<archiveDirectory></archiveDirectory>
							<includes>
								<include>target/${artifactId}.apk</include>
							</includes>
							<!-- 簽名配置 -->
							<keystore>keystore/keystore>
							<storepass>storepass</storepass>
							<keypass>keypass</keypass>
							<alias>alias</alias>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>


4、通過cmd命令進入到對應android工程目錄下,執行mvn clean install來打包,如果打包成功,會在工程目錄下生成tartget的資料夾,裡面有對應的編譯生成檔案以及我們需要的apk檔案