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

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

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

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

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

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

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

使用vim自动去除bom

1 去掉utf-8 BOM
2 :set nobomb

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

01                 <plugin>
02                 <groupId>org.apache.maven.plugins</groupId>
03                 <artifactId>maven-jarsigner-plugin</artifactId>
04                 <version>1.2</version>
05                 <executions>
06                     <execution>
07                         <id>signing</id>
08                         <goals>
09                             <goal>sign</goal>
10                         </goals>
11                         <phase>package</phase>
12                         <inherited>true</inherited>
13                         <configuration>
14                             <archiveDirectory></archiveDirectory>
15                             <includes>
16                                 <include>target/${artifactId}.apk</include>
17                             </includes>
18                             <keystore>${keyFilePath}</keystore>
19                             <storepass>${storePassword}</storepass>
20                             <keypass>${keyPassword}</keypass>
21                             <alias>${keyAlias}</alias>
22                         </configuration>
23                     </execution>
24                 </executions>
25             </plugin>

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

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

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

必须定义maven的android插件信息

 

01                         <configuration>
02                     <sdk>
03                         <path>${env.ANDROID_HOME}</path>
04                         <platform>7</platform>
05                     </sdk>
06                     <sign>
07                         <debug>false</debug>
08                     </sign>
09                     <deleteConflictingFiles>true</deleteConflictingFiles>
10                 </configuration>

 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>
008     <name>xxxxxx</name>
009     <dependencies>
010         <dependency>
011             <groupId>com.google.android</groupId>
012             <artifactId>android</artifactId>
013             <version>2.1.2</version>
014             <scope>provided</scope>
015         </dependency>
016         <dependency>
017             <groupId>com.thoughtworks.xstream</groupId>
018             <artifactId>xstream</artifactId>
019             <version>1.3.1</version>
020         </dependency>
021         <dependency>
022             <groupId>com.dom4j</groupId>
023             <artifactId>dom4j</artifactId>
024             <version>1.6.1</version>
025         </dependency>
026         <dependency>
027             <groupId>commons-httpclient</groupId>
028             <artifactId>commons-httpclient</artifactId>
029             <version>3.1</version>
030         </dependency>
031         <dependency>
032             <groupId>com.autonavi</groupId>
033             <artifactId>mapapi</artifactId>
034             <version>1.0</version>
035         </dependency>
036     </dependencies>
037     <build>
038         <finalName>${artifactId}</finalName>
039         <sourceDirectory>src</sourceDirectory>
040         <plugins>
041             <plugin>
042                 <groupId>
043                     com.jayway.maven.plugins.android.generation2
044                   </groupId>
045                 <artifactId>android-maven-plugin</artifactId>
046                 <version>3.1.1</version>
047                 <configuration>
048                     <sdk>
049                         <path>${env.ANDROID_HOME}</path>
050                         <platform>7</platform>
051                     </sdk>
052                     <sign>
053                         <debug>false</debug>
054                     </sign>
055                     <deleteConflictingFiles>true</deleteConflictingFiles>
056                 </configuration>
057                 <extensions>true</extensions>
058                 <inherited>true</inherited>
059             </plugin>
060             <plugin>
061                 <artifactId>maven-compiler-plugin</artifactId>
062                 <configuration>
063                     <source>1.6</source>
064                     <target>1.6</target>
065                     <encoding>UTF-8</encoding>
066                 </configuration>
067             </plugin>
068             <plugin>
069                 <groupId>org.apache.maven.plugins</groupId>
070                 <artifactId>maven-jarsigner-plugin</artifactId>
071                 <version>1.2</version>
072                 <executions>
073                     <execution>
074                         <id>signing</id>
075                         <goals>
076                             <goal>sign</goal>
077                         </goals>
078                         <phase>package</phase>
079                         <inherited>true</inherited>
080                         <configuration>
081                             <archiveDirectory></archiveDirectory>
082                             <includes>
083                                 <include>target/${artifactId}.apk</include>
084                             </includes>
085                             <keystore>${keyFilePath}</keystore>
086                             <storepass>${storePassword}</storepass>
087                             <keypass>${keyPassword}</keypass>
088                             <alias>${keyAlias}</alias>
089                         </configuration>
090                     </execution>
091                 </executions>
092             </plugin>
093         </plugins>
094     </build>
095     <profiles>
096         <profile>
097             <id>local</id>
098             <activation>
099                 <activeByDefault>true</activeByDefault>
100             </activation>
101             <properties>
102                 <keyFilePath>xxxxxxx</keyFilePath>
103                 <storePassword>xxxx</storePassword>
104                 <keyPassword>xxxx</keyPassword>
105                 <keyAlias>xxxxx</keyAlias>
106             </properties>
107         </profile>
108         <profile>
109             <id>dev</id>
110             <properties>
111                 <keyFilePath>xxxxx</keyFilePath>
112                 <storePassword>xxxxx</storePassword>
113                 <keyPassword>xxxx</keyPassword>
114                 <keyAlias>xxxxxx</keyAlias>
115             </properties>
116         </profile>
117     </profiles>
118 </project>

你可能感兴趣的:(使用maven构建android 的apk包并自动签名)