最近使用maven自动构建android的签名apk包(配合hudson),遇到几个问题跟大家分享下:
1、使用maven-android-plugin可以很容易实现自动构建,但基于命令行的方式有别与eclipse的打包方式
2、编译时出现非法字符的错误
1 | *.java:[1,0] 非法字符: \65279 |
说明某些文件采用UTF-8的时候写入了BOM的头信息,eclipse采用jdt会自动处理这些头信息但maven直接调用javac没有那么智能了,首先找找那些文件
2 | find -type f -name "*.java"|while read file;do [ "`head -c3 -- "$file"`"== $'\xef\xbb\xbf' ] && echo "found BOM in: $file";done |
使用vim自动去除bom
3、打包是出现无法签名的情况
02 | <groupId>org.apache.maven.plugins</groupId> |
03 | <artifactId>maven-jarsigner-plugin</artifactId> |
04 | <version>1.2</version> |
11 | <phase>package</phase> |
12 | <inherited>true</inherited> |
14 | <archiveDirectory></archiveDirectory> |
16 | <include>target/${artifactId}.apk</include> |
18 | <keystore>${keyFilePath}</keystore> |
19 | <storepass>${storePassword}</storepass> |
20 | <keypass>${keyPassword}</keypass> |
21 | <alias>${keyAlias}</alias> |
alias必须与生成签名文件时的条目一致
4、签名之前必须保证apk生成的时候没有使用debug签名,不然会提示
1 | jarsigner: 无法对 jar 进行签名: java.util.zip.ZipException: invalid entry compressed size (expected 15331 but got 15809 bytes) |
必须定义maven的android插件信息
03 | <path>${env.ANDROID_HOME}</path> |
04 | <platform>7</platform> |
09 | <deleteConflictingFiles>true</deleteConflictingFiles> |
5、至此使用一条命令 mvn clean package就可以自动编译打包了
下面是完整的配置
001 | <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
002 | xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
003 | <modelVersion>4.0.0</modelVersion> |
004 | <groupId>com.xxxx.gcl</groupId> |
005 | <artifactId>xxxx</artifactId> |
006 | <version>2.1.2</version> |
007 | <packaging>apk</packaging> |
011 | <groupId>com.google.android</groupId> |
012 | <artifactId>android</artifactId> |
013 | <version>2.1.2</version> |
014 | <scope>provided</scope> |
017 | <groupId>com.thoughtworks.xstream</groupId> |
018 | <artifactId>xstream</artifactId> |
019 | <version>1.3.1</version> |
022 | <groupId>com.dom4j</groupId> |
023 | <artifactId>dom4j</artifactId> |
024 | <version>1.6.1</version> |
027 | <groupId>commons-httpclient</groupId> |
028 | <artifactId>commons-httpclient</artifactId> |
029 | <version>3.1</version> |
032 | <groupId>com.autonavi</groupId> |
033 | <artifactId>mapapi</artifactId> |
034 | <version>1.0</version> |
038 | <finalName>${artifactId}</finalName> |
039 | <sourceDirectory>src</sourceDirectory> |
043 | com.jayway.maven.plugins.android.generation2 |
045 | <artifactId>android-maven-plugin</artifactId> |
046 | <version>3.1.1</version> |
049 | <path>${env.ANDROID_HOME}</path> |
050 | <platform>7</platform> |
055 | <deleteConflictingFiles>true</deleteConflictingFiles> |
057 | <extensions>true</extensions> |
058 | <inherited>true</inherited> |
061 | <artifactId>maven-compiler-plugin</artifactId> |
065 | <encoding>UTF-8</encoding> |
069 | <groupId>org.apache.maven.plugins</groupId> |
070 | <artifactId>maven-jarsigner-plugin</artifactId> |
071 | <version>1.2</version> |
078 | <phase>package</phase> |
079 | <inherited>true</inherited> |
081 | <archiveDirectory></archiveDirectory> |
083 | <include>target/${artifactId}.apk</include> |
085 | <keystore>${keyFilePath}</keystore> |
086 | <storepass>${storePassword}</storepass> |
087 | <keypass>${keyPassword}</keypass> |
088 | <alias>${keyAlias}</alias> |
099 | <activeByDefault>true</activeByDefault> |
102 | <keyFilePath>xxxxxxx</keyFilePath> |
103 | <storePassword>xxxx</storePassword> |
104 | <keyPassword>xxxx</keyPassword> |
105 | <keyAlias>xxxxx</keyAlias> |
111 | <keyFilePath>xxxxx</keyFilePath> |
112 | <storePassword>xxxxx</storePassword> |
113 | <keyPassword>xxxx</keyPassword> |
114 | <keyAlias>xxxxxx</keyAlias> |