注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

和申的个人主页

专注于java开发,1985wanggang

 
 
 

日志

 
 

使用maven构建android 的apk包并自动签名  

2015-01-11 23:17:40|  分类: 安卓 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

最近使用maven自动构建android的签名apk包(配合hudson),遇到几个问题跟大家分享下

1、使用maven-android-plugin可以很容易实现自动构建,但基于命令行的方式有别与eclipse的打包方式

2、编译时出现非法字符的错误

*.java:[1,0] 非法字符 \65279

说明某些文件采用UTF-8的时候写入了BOM的头信息,eclipse采用jdt会自动处理这些头信息但maven直接调用javac没有那么智能了,首先找找那些文件

#查找BOM
find -type f -name "*.java"|while read file;do [ "`head -c3 -- "$file"`" == $'\xef\xbb\xbf' ] && echo "found BOM in: $file";done

使用vim自动去除bom

去掉utf-8 BOM
:set nobomb

3、打包是出现无法签名的情况

                <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>${keyFilePath}</keystore>
							<storepass>${storePassword}</storepass>
							<keypass>${keyPassword}</keypass>
							<alias>${keyAlias}</alias>
						</configuration>
					</execution>
				</executions>
			</plugin>

 

 alias必须与生成签名文件时的条目一致

4、签名之前必须保证apk生成的时候没有使用debug签名,不然会提示

jarsigner 无法对 jar 进行签名 java.util.zip.ZipException: invalid entry compressed size (expected 15331 but got 15809 bytes)

必须定义maven的android插件信息

 

                        <configuration>
					<sdk>
						<path>${env.ANDROID_HOME}</path>
						<platform>7</platform>
					</sdk>
					<sign>
						<debug>false</debug>
					</sign>
					<deleteConflictingFiles>true</deleteConflictingFiles>
				</configuration>

 

 

 5、至此使用一条命令 mvn clean package就可以自动编译打包了

下面是完整的配置

 

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.xxxx.gcl</groupId>
	<artifactId>xxxx</artifactId>
	<version>2.1.2</version>
	<packaging>apk</packaging>
	<name>xxxxxx</name>
	<dependencies>
		<dependency>
			<groupId>com.google.android</groupId>
			<artifactId>android</artifactId>
			<version>2.1.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.thoughtworks.xstream</groupId>
			<artifactId>xstream</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>com.dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>com.autonavi</groupId>
			<artifactId>mapapi</artifactId>
			<version>1.0</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>${artifactId}</finalName>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<groupId>
					com.jayway.maven.plugins.android.generation2
                  </groupId>
				<artifactId>android-maven-plugin</artifactId>
				<version>3.1.1</version>
				<configuration>
					<sdk>
						<path>${env.ANDROID_HOME}</path>
						<platform>7</platform>
					</sdk>
					<sign>
						<debug>false</debug>
					</sign>
					<deleteConflictingFiles>true</deleteConflictingFiles>
				</configuration>
				<extensions>true</extensions>
				<inherited>true</inherited>
			</plugin>
			<plugin>
				<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>${keyFilePath}</keystore>
							<storepass>${storePassword}</storepass>
							<keypass>${keyPassword}</keypass>
							<alias>${keyAlias}</alias>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<profiles>
		<profile>
			<id>local</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<properties>
				<keyFilePath>xxxxxxx</keyFilePath>
				<storePassword>xxxx</storePassword>
				<keyPassword>xxxx</keyPassword>
				<keyAlias>xxxxx</keyAlias>
			</properties>
		</profile>
		<profile>
			<id>dev</id>
			<properties>
				<keyFilePath>xxxxx</keyFilePath>
				<storePassword>xxxxx</storePassword>
				<keyPassword>xxxx</keyPassword>
				<keyAlias>xxxxxx</keyAlias>
			</properties>
		</profile>
	</profiles>
</project>
  评论这张
 
阅读(391)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016