<转>使用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-typef-name"*.java"|whilereadfile;do["`head-c3--"$file"`"==$'\xef\xbb\xbf']&&echo"foundBOMin:$file";done

使用vim自动去除bom

1

去掉utf-8BOM

2

:setnobomb

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:invalidentrycompressedsize(expected15331butgot15809bytes)

必须定义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>

相关推荐