为什么 Proguard 不混淆 Android 应用程序中的某些代码

发布于 2024-12-28 19:22:49 字数 2955 浏览 0 评论 0原文

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

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

有人可以告诉我为什么方法名称和类变量没有被混淆吗?我已将我的构建文件包含在下面。虽然升级到 r16 后某些构建文件的结构有点不同,但我基本上遵循使用 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" />

I am building a release version of my app using ant and proguard. The release version of my app is built successfully, however, the only portions of the code that get obfuscated are local variables. Methods and class variables are all retaining their exact names. Adding to my confusion is the fact that the mapping.txt file gets generated every time I build, and I see methods and class variables being mapped to something else. However, when I use Dex2Jar to deconstruct my .apk file, I don't see any of the mappings.

I recently upgraded ADB to r16. Before this update, I was using a version of ADB that was pre-r14. Using the pre-r14 version of ADB, I had been successfully obfuscating my app for about a year. I was able to verify that these pre-r14 .apks were completely obfuscated by deconstructing them as well.

Can someone tell me why the method names and class variables are not being obfuscated? I have included my build files down below. Though the structure of some of the build files is a bit different after upgrading to r16, I am essentially following the same steps that I used to follow when using the pre-r14 ADB, so I don't understand why I'm getting different behavior.

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" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2025-01-04 19:22:49

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

简而言之 - 您不需要这些引用,只需要 proguard.cfg 文件即可。我只需按照开发指南中所述从命令行创建示例项目,并使用生成的 build.xml 作为项目 build.xml 的基础。它可能会“开箱即用”地工作。对我来说确实如此。我的发布版本是使用 Ant(您需要 Ant 版本 1.8)从命令行构建的,其方法已被混淆。

PS 您还需要在project.properties 文件中添加以下行: proguard.config=proguard.cfg

Initially I was confused by your build.xml's references to "add-proguard-release". Now I see that these references were once necessary to add Proguard support to the build process. However, proguard support is now built into the master SDK_DIR/tools/ant.xml itself. (I think it came in before r16 in fact)

In short - you don't need these references, just the proguard.cfg file to be present. I'd just create the sample project from the command line as described in the dev guide and use the generated build.xml as the basis for your project's build.xml. It will probably work right 'out of the box'. It certainly does for me. My release builds which are built using Ant (you'll need Ant version 1.8) from the command line have their methods obfuscated OK.

P.S. You also need to have the line: proguard.config=proguard.cfg in the project.properties file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文