简体   繁体   English

ProGuard与maven-android-plugin

[英]ProGuard with maven-android-plugin

I'm trying to pack my project to APK and then obfuscate it with ProGuard. 我正在尝试将我的项目打包到APK,然后使用ProGuard对其进行模糊处理。

Here is my pom: 这是我的pom:

<?xml version="1.0" encoding="UTF-8"?>
    <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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.skayred</groupId>
    <artifactId>skayred</artifactId>
    <version>1.26</version>
    <packaging>apk</packaging>
    <name>skayred</name>

<properties>
    <environment>development</environment>
</properties>

<dependencies>

    <dependency>
        <groupId>android</groupId>
        <artifactId>android</artifactId>
        <version>2.2_r3</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.google.android.maps</groupId>
        <artifactId>maps</artifactId>
        <version>8_r2</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client</artifactId>
        <version>1.6.0-beta</version>
        <exclusions>

            <exclusion>
                <groupId>xpp3</groupId>
                <artifactId>xpp3</artifactId>
            </exclusion>

            <exclusion>
                <groupId>com.google.protobuf</groupId>
                <artifactId>protobuf-java</artifactId>
            </exclusion>

            <exclusion>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-core-asl</artifactId>
            </exclusion>

            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>

            <exclusion>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </exclusion>

            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>

            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
            </exclusion>

            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
            </exclusion>

            <exclusion>
                <groupId>com.google.code.findbugs</groupId>
                <artifactId>jsr305</artifactId>
            </exclusion>

        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client-extensions-android2</artifactId>
        <version>1.6.0-beta</version>
        <exclusions>
            <exclusion>
                <groupId>com.google.android</groupId>
                <artifactId>android</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>10.0.1</version>

        <exclusions>
            <exclusion>
                <groupId>com.google.code.findbugs</groupId>
                <artifactId>jsr305</artifactId>
            </exclusion>
        </exclusions>

    </dependency>

    <dependency>
        <groupId>com.skyhookwireless.wps</groupId>
        <artifactId>skyhookwireless-wps</artifactId>
        <version>4.2.0.14</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>com.bugsense.trace</groupId>
        <artifactId>bugsense-trace</artifactId>
        <version>1.1.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jarsigner-plugin</artifactId>
        <version>1.2</version>

        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>

    </dependency>

</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>

    <resources>
        <!-- a pair definition for resource filtering of xml files only (not binary files since they get mangled -->
        <resource>
            <directory>${project.basedir}/res</directory>
            <filtering>true</filtering>
            <targetPath>${project.build.directory}/filtered-res</targetPath>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>${project.basedir}/res</directory>
            <filtering>false</filtering>
            <targetPath>${project.build.directory}/filtered-res</targetPath>
            <excludes>
                <exclude>**/*.xml</exclude>
            </excludes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.0.0</version>
            <extensions>true</extensions>
            <configuration>
                <resourceDirectory>${project.build.directory}/filtered-res</resourceDirectory>
                <manifest>
                    <debuggable>true</debuggable>
                </manifest>
                <dex>
                    <jvmArguments>
                        <jvmArgument>-Xms256m</jvmArgument>
                        <jvmArgument>-Xmx512m</jvmArgument>
                    </jvmArguments>
                </dex>
                <sdk>
                    <platform>8</platform>
                </sdk>
            </configuration>
            <executions>
                <execution>
                    <id>manifestUpdate</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>manifest-update</goal>
                    </goals>
                </execution>
                <execution>
                    <id>alignApk</id>
                    <phase>package</phase>
                    <goals>
                        <goal>zipalign</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

<profiles>
    <profile>
        <id>development</id>
        <!-- using this since activeByDefault does not work well with multiple profiles -->
        <activation>
            <property>
                <name>environment</name>
                <value>!production</value>
            </property>
        </activation>
        <properties>
            <deployment.host>http://192.168.192.15:5000</deployment.host>
        </properties>
    </profile>
    <profile>
        <id>production</id>

        <properties>
            <deployment.host>https://somehost.com</deployment.host>
        </properties>
    </profile>
    <profile>
        <id>release</id>
        <properties>
            <deployment.host>https://somehost.com</deployment.host>
        </properties>
        <!-- via this activation the profile is automatically used when the release is done with the maven release
        plugin -->
        <activation>
            <property>
                <name>performRelease</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jarsigner-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>signing</id>
                            <goals>
                                <goal>sign</goal>
                                <goal>verify</goal>
                            </goals>
                            <phase>package</phase>
                            <inherited>true</inherited>
                            <configuration>
                                <removeExistingSignatures>true</removeExistingSignatures>
                                <archiveDirectory/>
                                <includes>
                                    <include>${project.build.directory}/${project.artifactId}.apk</include>
                                </includes>
                                <keystore>${sign.keystore}</keystore>
                                <alias>${sign.alias}</alias>
                                <storepass>${sign.storepass}</storepass>
                                <keypass>${sign.keypass}</keypass>
                                <verbose>true</verbose>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <!-- the signed apk then needs to be zipaligned and we activate proguard and we run the manifest
                update -->
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <inherited>true</inherited>
                    <configuration>
                        <sign>
                            <debug>false</debug>
                        </sign>
                        <zipalign>
                            <verbose>true</verbose>
                            <inputApk>${project.build.directory}/${project.artifactId}.apk</inputApk>
                            <outputApk>${project.build.directory}/${project.artifactId}-signed-aligned.apk
                            </outputApk>
                        </zipalign>
                        <manifest>
                            <debuggable>false</debuggable>
                            <versionCodeAutoIncrement>true</versionCodeAutoIncrement>
                        </manifest>
                        <proguard>
                            <skip>false</skip>
                        </proguard>
                    </configuration>
                    <executions>
                        <execution>
                            <id>manifestUpdate</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>manifest-update</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>alignApk</id>
                            <phase>package</phase>
                            <goals>
                                <goal>zipalign</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>${project.build.directory}/${project.artifactId}-signed-aligned.apk</file>
                                <type>apk</type>
                                <classifier>signed-aligned</classifier>
                            </artifact>
                            <artifact>
                                <file>${project.build.directory}/proguard/mapping.txt</file>
                                <type>map</type>
                                <classifier>release</classifier>
                            </artifact>
                        </artifacts>
                    </configuration>
                    <executions>
                        <execution>
                            <id>attach-signed-aligned</id>
                            <phase>package</phase>
                            <goals>
                                <goal>attach-artifact</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

proguard.cfg: proguard.cfg:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-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

-keepclasseswithmembernames class * {
    native <methods>;
}

-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);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

But when I'm trying to run mvn3 clean install -Prelease -X , I've get an error and loooong command. 但是,当我尝试运行mvn3 clean install -Prelease -X ,出现了错误和loooong命令。 When I'm trying to run that command, I've got: 当我试图运行该命令时,我得到了:

Warning: com.google.common.collect.AbstractBiMap: can't find referenced class javax.annotation.Nullable
Warning: com.google.common.collect.AbstractBiMap: can't find referenced class javax.annotation.Nullable
Note: the configuration refers to the unknown class 'com.android.vending.licensing.ILicensingService'
Note: there were 1 references to unknown classes.
  You should check your configuration for typos.
Warning: there were 1277 unresolved references to classes or interfaces.
     You may need to specify additional library jars (using '-libraryjars'),
     or perhaps the '-dontskipnonpubliclibraryclasses' option.
Warning: there were 6 unresolved references to program class members.
     Your input classes appear to be inconsistent.
     You may need to recompile them and try again.
     Alternatively, you may have to specify the options 
     '-dontskipnonpubliclibraryclasses' and/or
     '-dontskipnonpubliclibraryclassmembers'.
java.io.IOException: Please correct the above warnings first.
at proguard.Initializer.execute(Initializer.java:308)
at proguard.ProGuard.initialize(ProGuard.java:210)
at proguard.ProGuard.execute(ProGuard.java:85)
at proguard.ProGuard.main(ProGuard.java:499)

And lots of warning about referencing before. 还有很多关于之前引用的警告。 What I'm doing wrong? 我做错了什么?

The warnings say that many classes that you are processing refer to classes that aren't present in the input jars or library jars: 警告说,您正在处理的许多类都引用输入jar或库jar中不存在的类:

This is a common problem in Android, where you're processing third party libraries that depend on other, missing libraries (but don't really use them in practice). 这是Android中的常见问题,您正在处理依赖于其他缺少库的第三方库(但实际上并未真正使用它们)。 Some Android-specific questions and answers: 一些特定于Android的问题和解答:

The (harmless) note says that your proguard.cfg refers to com.android.vending.licensing.ILicensingService, but that class isn't present in your code. (无害)说明说你的proguard.cfg引用了com.android.vending.licensing.ILicensingService,但是你的代码中没有该类。 You can remove the line from the configuration. 您可以从配置中删除该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM