使用maven構建android 的apk包並自動簽名

最近使用maven自動構建android的簽名apk包(配合hudson),遇到幾個問題跟你們分享下: java

一、使用maven-android-plugin能夠很容易實現自動構建,但基於命令行的方式有別與eclipse的打包方式 android

二、編譯時出現非法字符的錯誤 apache

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

說明某些文件採用UTF-8的時候寫入了BOM的頭信息,eclipse採用jdt會自動處理這些頭信息但maven直接調用javac沒有那麼智能了,首先找找那些文件 vim

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 api

1 去掉utf-8 BOM
2 :set nobomb

三、打包是出現沒法簽名的狀況 dom

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必須與生成簽名文件時的條目一致 eclipse

四、簽名以前必須保證apk生成的時候沒有使用debug簽名,否則會提示 maven

1 jarsigner: 沒法對 jar 進行簽名: java.util.zip.ZipException: invalid entry compressed size (expected 15331 but got 15809 bytes)

必須定義maven的android插件信息 ide

 

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>

 五、至此使用一條命令 mvn clean package就能夠自動編譯打包了 ui

下面是完整的配置

 

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>
相關文章
相關標籤/搜索