为什么Proguard不会混淆Android App中的某些代码

时间:2012-01-23 10:34:46

标签: android proguard

我正在使用ant和proguard构建我的应用程序的发布版本。我的应用程序的发布版本已成功构建,但是,被混淆的代码的唯一部分是局部变量。方法和类变量都保留了它们的确切名称。令我困惑的是,每次构建时都会生成mapping.txt文件,我看到方法和类变量被映射到其他东西。但是,当我使用Dex2Jar来解构我的.apk文件时,我没有看到任何映射。

我最近将ADB升级到r16。在此更新之前,我使用的是pre-r14版本的ADB。使用pre-r14版本的ADB,我成功地将我的应用程序混淆了大约一年。我能够通过解构它们来验证这些pre-r14 .apks是否完全被混淆了。

有人能告诉我为什么方法名和类变量没有被混淆了吗?我在下面包含了我的构建文件。虽然升级到r16之后某些构建文件的结构有点不同,但我基本上遵循了我在使用pre-r14 ADB时所遵循的相同步骤,所以我不明白为什么我会变得与众不同行为。

proguard.cfg:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable

-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 interface com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet); 
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int); 
}

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

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

的build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project [ <!ENTITY add-proguard-release SYSTEM "add-proguard-release.xml">]>
<project name="MyApp" default="help">

&add-proguard-release;

<property file="local.properties" />

<property file="ant.properties" />

<loadproperties srcFile="project.properties" />

<fail
    message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
    unless="sdk.dir"/>

<!-- Custom Android task to deal with the project target, and import the
         proper rules.
     This requires ant 1.6.0 or above. -->
<path id="android.antlibs">
    <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
    <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
    <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
</path>

<taskdef name="setup"
    classname="com.android.ant.SetupTask"
    classpathref="android.antlibs" />

<import file="${sdk.dir}/tools/ant/build.xml" />

1 个答案:

答案 0 :(得分:0)

最初,我对build.xml对“add-proguard-release”的引用感到困惑。现在我看到这些引用一旦需要将Proguard支持添加到构建过程中。但是,proguard支持现在内置于主SDK_DIR / tools / ant.xml本身。 (我认为它实际上是在r16之前出现的)

简而言之 - 您不需要这些引用,只需要存在proguard.cfg文件。我只是按照开发指南中的描述从命令行创建示例项目,并使用生成的build.xml作为项目build.xml的基础。它可能会“开箱即用”。这当然对我有用。我的发布版本是使用Ant(你需要Ant版本1.8)从命令行构建的,它们的方法是模糊的。

P.S。您还需要在project.properties文件中包含以下行: proguard.config = proguard.cfg

相关问题