maven 學習筆記(七)-(完整Android項目多渠道打包、簽名、混淆進階)

       一、簽名 

      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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章