maven 学习笔记(七)-(完整Android项目多渠道打包、签名、混淆进阶)

来源:互联网 发布:python urllib decode 编辑:程序博客网 时间:2024/06/05 23:39

       一、签名 

      1.在文件pom.xml添加一下内容

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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/maven-v4_0_0.xsd">   <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <platform.version> 4.1.1.4        </platform.version>        <annotations.version>4.1.1.4</annotations.version>        <android.plugin.version>4.0.0-rc.2</android.plugin.version>        <keystore.filename>keystore/android-demo.keystore</keystore.filename>        <keystore.storepass>demo2015</keystore.storepass>        <keystore.keypass>demo2015</keystore.keypass>        <keystore.alias>android-demo.keystore</keystore.alias>    </properties>  <plugins>                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-jarsigner-plugin</artifactId>                        <version>1.2</version>                        <executions>                            <execution>                                <id>sign</id>                                <goals>                                    <goal>sign</goal>                                </goals>                                <phase>package</phase>                                <inherited>true</inherited>                                <configuration>                                    <includes>                                        <include>${project.build.outputDirectory}/*.apk</include>                                    </includes>                                    <!-- 移除已存在的签名(android debug的签名) -->                                    <removeExistingSignatures>true</removeExistingSignatures>                                    <keystore>${keystore.filename}</keystore>                                    <storepass>${keystore.storepass}</storepass>                                    <keypass>${keystore.keypass}</keypass>                                    <alias>${keystore.alias}</alias>                                </configuration>                            </execution>                        </executions>                    </plugin>




      2.打包自动传递到ftp服务器配置,在pox.xml添加一下内容

            <plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>wagon-maven-plugin</artifactId>                <version>1.0-beta-4</version>                <executions>                    <execution>                        <id>upload_apk</id>                        <phase>deploy</phase>                        <goals>                            <goal>upload-single</goal>                        </goals>                        <configuration>                            <serverId>ftp-repository</serverId>                            <url>ftp://demo.de.com:2690/ftproot/update/app/android/demo</url>                            <fromFile>${basedir}/target/${project.artifactId}-${manifest.metadata.id}-${project.version}.apk</fromFile>                            <toFile>${project.name}_${project.version}_${app.channel}.apk</toFile>                        </configuration>                    </execution>                </executions>            </plugin>



3.多渠道打包,在pox.xml添加一下内容

        <!-- 渠道profiles -->        <profile>            <id>channel-test</id>            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <properties>                <manifest.metadata.id>test</manifest.metadata.id>                <manifest.metadata.channel>test market</manifest.metadata.channel>            </properties>        </profile>        <profile>            <id>channel-baidu</id>            <properties>                <manifest.metadata.id>baidu</manifest.metadata.id>                <manifest.metadata.channel>baidu</manifest.metadata.channel>            </properties>        </profile>




3.pom.xml文件如下如下:

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion>    <groupId>com.special.demo</groupId>    <artifactId>demo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>apk</packaging>    <name>demo</name>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <platform.version> 4.1.1.4        </platform.version>        <annotations.version>4.1.1.4</annotations.version>        <android.plugin.version>4.0.0-rc.2</android.plugin.version>        <keystore.filename>keystore/android-demo.keystore</keystore.filename>        <keystore.storepass>demo2015</keystore.storepass>        <keystore.keypass>demo2015</keystore.keypass>        <keystore.alias>android-demo.keystore</keystore.alias>    </properties>    <dependencies>        <dependency>            <groupId>com.google.android</groupId>            <artifactId>android</artifactId>            <version>${platform.version}</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>com.google.android</groupId>            <artifactId>support</artifactId>            <version>4.0.0</version>        </dependency>        <dependency>            <groupId>com.google</groupId>            <artifactId>gson</artifactId>            <version>2.2.4</version>        </dependency>    </dependencies>    <build>        <finalName>${project.artifactId}-${project.version}</finalName>        <sourceDirectory>src/main/java</sourceDirectory>        <resources>            <resource>                <directory>${project.basedir}</directory>                <filtering>true</filtering>                <targetPath>${project.build.directory}/filtered-manifest</targetPath>                <includes>                    <include>AndroidManifest.xml</include>                </includes>            </resource>        </resources>        <pluginManagement>            <plugins>                <plugin>                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>                    <artifactId>android-maven-plugin</artifactId>                    <version>${android.plugin.version}</version>                    <extensions>true</extensions>                    <executions>                        <execution>                            <id>alignApk</id>                            <phase>install</phase>                            <goals>                                <goal>zipalign</goal>                            </goals>                        </execution>                    </executions>                    <configuration>                        <assetsDirectory>${project.basedir}/assets</assetsDirectory>                        <resourceDirectory>${project.basedir}/res</resourceDirectory>                        <!-- <androidManifestFile>${project.build.directory}/filtered-manifest/AndroidManifest.xml</androidManifestFile> -->                        <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>                        <nativeLibrariesDirectory>${project.basedir}/libs</nativeLibrariesDirectory>                        <!-- <proguardConfig>proguard.cfg</proguardConfig> -->                        <proguardConfig>proguard-project.txt</proguardConfig>                        <proguardSkip>${project.build.proguardSkip}</proguardSkip>                        <manifestDebuggable>${manifest.debuggable}</manifestDebuggable>                        <release>${project.build.release}</release>                        <run>                            <debug>${project.build.debug}</debug>                        </run>                        <runDebug>${project.build.runDebug}</runDebug>                        <sign>                            <debug>${project.build.sign.debug}</debug>                        </sign>                        <undeployBeforeDeploy>false</undeployBeforeDeploy>                        <mergeManifests>true</mergeManifests>                    </configuration>                </plugin>            </plugins>        </pluginManagement>        <plugins>            <plugin>                <groupId>com.jayway.maven.plugins.android.generation2</groupId>                <artifactId>android-maven-plugin</artifactId>                <version>${android.plugin.version}</version>                <executions>                    <execution>                        <id>alignApk</id>                        <phase>install</phase>                        <goals>                            <goal>zipalign</goal>                        </goals>                    </execution>                </executions>                <configuration>                    <sdk>                        <platform>17</platform>                    </sdk>                    <zipalign>                        <verbose>true</verbose>                        <!-- 是否单元测试 -->                        <skip>true</skip>                        <inputApk>${project.build.directory}/${project.artifactId}-${project.version}.apk</inputApk>                        <outputApk>${project.build.directory}/${project.name}_${app.channel}_${project.version}.apk</outputApk>                    </zipalign>                </configuration>            </plugin>            <plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>keytool-maven-plugin</artifactId>                <version>1.2</version>                <executions>                    <execution>                        <id>zipalign</id>                        <goals>                            <goal>exec</goal>                        </goals>                        <phase>install</phase>                    </execution>                </executions>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.1</version>                <configuration>                    <source>1.6</source>                    <target>1.6</target>                    <encoding>${project.build.sourceEncoding}</encoding>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-resources-plugin</artifactId>                <version>2.6</version>                <executions>                    <execution>                        <phase>initialize</phase>                        <goals>                            <goal>resources</goal>                        </goals>                    </execution>                </executions>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-deploy-plugin</artifactId>                <version>2.8.1</version>                <configuration>                    <skip>true</skip>                </configuration>            </plugin>            <plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>wagon-maven-plugin</artifactId>                <version>1.0-beta-4</version>                <executions>                    <execution>                        <id>upload_apk</id>                        <phase>deploy</phase>                        <goals>                            <goal>upload-single</goal>                        </goals>                        <configuration>                            <serverId>ftp-repository</serverId>                            <url>ftp://demo.de.com:2690/ftproot/update/app/android/demo</url>                            <fromFile>${basedir}/target/${project.artifactId}-${manifest.metadata.id}-${project.version}.apk</fromFile>                            <toFile>${project.name}_${project.version}_${app.channel}.apk</toFile>                        </configuration>                    </execution>                </executions>            </plugin>            <plugin>                <artifactId>maven-source-plugin</artifactId>                <version>2.1</version>                <configuration>                    <!-- <finalName>${project.build.name}</finalName> -->                    <attach>true</attach>                    <encoding>${project.build.sourceEncoding}</encoding>                </configuration>                <executions>                    <execution>                        <phase>compile</phase>                        <goals>                            <goal>jar</goal>                        </goals>                    </execution>                </executions>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-jar-plugin</artifactId>                <version>2.3.1</version>                <configuration>                    <archive>                        <!-- 不把pom.xml打入jar中 -->                        <addMavenDescriptor>false</addMavenDescriptor>                        <!-- <manifestFile> ${basedir}/META-INF/MANIFEST.MF </manifestFile> -->                        <manifest>                            <!-- <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> -->                        </manifest>                    </archive>                </configuration>            </plugin>        </plugins>        <extensions>            <!-- Enabling the use of FTP -->            <extension>                <groupId>org.apache.maven.wagon</groupId>                <artifactId>wagon-ftp</artifactId>                <version>1.0-beta-6</version>            </extension>        </extensions>    </build>    <profiles>        <profile>            <id>debug</id>            <activation>                <activeByDefault>false</activeByDefault>            </activation>            <properties>                <project.build.debug>true</project.build.debug>                <project.build.runDebug>false</project.build.runDebug>                <project.build.proguardSkip>true</project.build.proguardSkip>                <project.build.release>false</project.build.release>                <project.build.sign.debug>false</project.build.sign.debug>                <manifest.debuggable>true</manifest.debuggable>            </properties>            <build>                <!-- <filters> <filter>src/main/resources/env-debug.properties</filter>                     </filters> -->                <plugins>                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-jarsigner-plugin</artifactId>                        <version>1.2</version>                        <executions>                            <execution>                                <id>sign</id>                                <goals>                                    <goal>sign</goal>                                </goals>                                <phase>package</phase>                                <inherited>true</inherited>                                <configuration>                                    <includes>                                        <include>${project.build.outputDirectory}/*.apk</include>                                    </includes>                                    <!-- 移除已存在的签名(android debug的签名) -->                                    <removeExistingSignatures>true</removeExistingSignatures>                                    <keystore>${keystore.filename}</keystore>                                    <storepass>${keystore.storepass}</storepass>                                    <keypass>${keystore.keypass}</keypass>                                    <alias>${keystore.alias}</alias>                                </configuration>                            </execution>                        </executions>                    </plugin>                </plugins>            </build>        </profile>        <profile>            <!-- 上线发布 -->            <id>release</id>            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <properties>                <project.build.debug>false</project.build.debug>                <project.build.runDebug>false</project.build.runDebug>                <project.build.proguardSkip>false</project.build.proguardSkip>                <project.build.release>true</project.build.release>                <project.build.sign.debug>false</project.build.sign.debug>                <manifest.debuggable>false</manifest.debuggable>            </properties>            <build>                <!-- <filters> <filter>src/main/resources/env-release.properties</filter>                     </filters> -->                <plugins>                    <plugin>                        <groupId>org.apache.maven.plugins</groupId>                        <artifactId>maven-jarsigner-plugin</artifactId>                        <version>1.2</version>                        <executions>                            <execution>                                <id>sign</id>                                <goals>                                    <goal>sign</goal>                                </goals>                                <phase>package</phase>                                <inherited>true</inherited>                                <configuration>                                    <includes>                                        <include>${project.build.outputDirectory}/*.apk</include>                                    </includes>                                    <!-- 移除已存在的签名(android debug的签名) -->                                    <removeExistingSignatures>true</removeExistingSignatures>                                    <keystore>${keystore.filename}</keystore>                                    <storepass>${keystore.storepass}</storepass>                                    <keypass>${keystore.keypass}</keypass>                                    <alias>${keystore.alias}</alias>                                </configuration>                            </execution>                        </executions>                    </plugin>                </plugins>            </build>        </profile>                                <!-- 渠道profiles -->        <profile>            <id>channel-test</id>            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <properties>                <manifest.metadata.id>test</manifest.metadata.id>                <manifest.metadata.channel>test market</manifest.metadata.channel>            </properties>        </profile>        <profile>            <id>channel-baidu</id>            <properties>                <manifest.metadata.id>baidu</manifest.metadata.id>                <manifest.metadata.channel>baidu</manifest.metadata.channel>            </properties>        </profile>    </profiles></project>



       二、混淆

      1.在文件pom.xml添加一下内容

    


                        <!-- <proguardConfig>proguard.cfg</proguardConfig> -->                        <proguardConfig>proguard-project.txt</proguardConfig>                        <proguardSkip>${project.build.proguardSkip}</proguardSkip>


     2.混淆webview注意事项:在混淆文件proguard-project.txt中中添加一下内容

  

-keepattributes Signature-keepattributes *Annotation*-keepattributes *JavascriptInterface*  # webview + js# keep 使用 webview 的类-keepclassmembers class com.special.demo.HelloAndroidActivity {   public *;}# keep 使用 webview 的类的所有的内部类-keepclassmembers   class  com.special.demo.HelloAndroidActivity$*{    *;}


    3.使用gson注意事项:在混淆文件proguard-project.txt中中添加一下内容

  

#使用 gson 需要的配置-keep class com.google.gson.JsonObject { *; }



    4.在混淆文件proguard-project.txt完整内容如下:

  

# to define the proguard.config property as described in that file.## Add project specific ProGuard rules here.# By default, the flags in this file are appended to flags specified# in ${sdk.dir}/tools/proguard/proguard-android.txt# You can edit the include path and order by changing the ProGuard# include property in project.properties.## For more details, see#   http://developer.android.com/guide/developing/tools/proguard.html# Add any project specific keep options here:# If your project uses WebView with JS, uncomment the following# and specify the fully qualified class name to the JavaScript interface# class:#-keepclassmembers class fqcn.of.javascript.interface.for.webview {#   public *;#}-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*# ----------------------------------  #  通过指定数量的优化能执行  #  -optimizationpasses n  # ---------------------------------- -optimizationpasses 5#确定统一的混淆类的成员名称来增加混淆 -useuniqueclassmembernames#优化时允许访问并修改有修饰符的类和类的成员   -allowaccessmodification #不优化泛型和反射-keepattributes Signature-keepattributes *Annotation*-keepattributes *JavascriptInterface*  -renamesourcefileattribute SourceFile-adaptresourcefilenames **.properties-adaptresourcefilecontents **.properties,src/META-INF/MANIFEST.MF,META-INF/MANIFEST.MF,META-INF/LICENSE.txt,META-INF/NOTICE.txt# webview + js# keep 使用 webview 的类-keepclassmembers class com.special.demo.HelloAndroidActivity {   public *;}# keep 使用 webview 的类的所有的内部类-keepclassmembers   class  com.special.demo.HelloAndroidActivity$*{    *;}# ----------------------------------  #   混淆时不会产生形形色色的类名   #   -dontusemixedcaseclassnames  # ---------------------------------- -dontusemixedcaseclassnames#      指定不去忽略非公共的库类  #  -dontskipnonpubliclibraryclasses  # ---------------------------------- -dontskipnonpubliclibraryclasses# ----------------------------------  #       不预校验  #    -dontpreverify  # ----------------------------------  -dontpreverify #这1句是屏蔽警告,脚本中把这行注释去掉-ignorewarnings# ----------------------------------  #      输出生成信息  #       -verbose  # ----------------------------------  -verbose    #混淆时应用侵入式重载   -overloadaggressively      #优化时允许访问并修改有修饰符的类和类的成员   -allowaccessmodification  #确定统一的混淆类的成员名称来增加混淆   -useuniqueclassmembernames  #优化-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*-keep public class * extends android.app.Activity-keep public class * extends android.app.Application-keep public class * extends android.app.Service-keep public class * extends android.content.BroadcastReceiver-keep public class * extends android.content.ContentProvider-keep public class * extends android.app.backup.BackupAgentHelper-keep public class * extends android.preference.Preference-keep public class com.android.vending.licensing.ILicensingService#第三分库不混淆-keep class android.support.v4.** {*;}-keep class org.apache.http.entity.mime.** {*;}#使用 gson 需要的配置-keep class com.google.gson.JsonObject { *; }-dontwarn android.support.v4.**-keepclasseswithmembers class * {    public <init>(android.content.Context,android.util.AttributeSet);}-keepclasseswithmembers class * {    public <init>(android.content.Context,android.util.AttributeSet,int);}-keepclassmembers class * extends android.app.Activity {    public void *(android.view.View);}-keep class * extends android.os.Parcelable {    public static final android.os.Parcelable$Creator *;}# Keep - Library. Keep all public  classes, fields, and methods.-keep public class * {    public <fields>;    public  <methods>;}# Keep - Library. Keep all public and protected classes, fields, and methods.#-keep public class * {#   public protected <fields>;#    public protected <methods>;#}# Also keep - Enumerations. Keep the special static methods that are required in# enumeration classes.-keepclassmembers enum  * {    public static **[] values();    public static ** valueOf(java.lang.String);}# Keep names - Native method names. Keep all native class/method names.-keepclasseswithmembers,allowshrinking class * {    native <methods>;}#-libraryjars /Users/jack/android-sdks/platforms/android-17/android.jar-libraryjars libs/android.jar-libraryjars libs/gson-2.2.4.jar-libraryjars libs/commons-lang-2.4.jar-libraryjars libs/android-support-v4.jar


       二、demo

 、demo地址下载链接地址如下:

       https://github.com/spring5555/mvn-android-simple-demo6



0 0