使用 proguard 混淆 JAR 时的奇怪行为

发布于 2024-12-23 02:34:49 字数 3157 浏览 2 评论 0原文

我正在尝试使用 Proguard 来混淆 Android JAR(4.7,但 4.6 也有问题)。我可以将问题分解为一个简单的示例项目。

问题:对于某些函数(不清楚出于什么原因),公开函数的参数名称会丢失,有时会“混乱”(真的)。我想首先关注“丢失”的部分,因为乱序的东西更奇怪......

1)我在 Eclipse 中创建了一个库项目。由于某些原因,Android SDK 为 2.1-update 1 该项目被标记为“Library Project”,只有一个类 MyJarEntry.java 和一个导出函数 foo

package com.decades.myjar;

import android.location.LocationListener;

public class MyJarEntry {

    public void foo(String provider, long minTime, float minDistance, LocationListener listener) {
    }
}

2) 我的项目有一个子目录“proguard”,其中包含最新的 proguard.jar 和 proguard.cfg,如下所示

-printmapping out.map

-keepparameternames
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
            SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-keep public class * {
    public protected *;
}

3)我的 Build.xml 具有构建 JAR 并像这样优化它的目标,因此在 Eclipse 中构建 lib 后,我在终端中执行“ant jar”和“并优化”。这是构建.xml

<project name="MyJar" default="jar" basedir=".">
<description>
  Lib JAR builder
</description>

<!-- set global properties for this build -->
<property name="res" location="res" />
<property name="bin" location="bin" />

<!-- Output directories -->
<property name="out.dir" value="bin" />
<property name="out.absolute.dir" location="${out.dir}" />
<property name="out.classes.dir" value="${out.absolute.dir}/com" />
<property name="out.classes.absolute.dir" location="${out.classes.dir}" />


<!-- Pack the jar -->
<target name="jar">
    <jar destfile="MyJar.jar" basedir="bin/">
        <!-- replace 'com' by what ever you are using -->
        <!-- as first part of the package name -->
        <!-- e.g. de, org, ... -->
        <!-- the ** is important to include the directory recursively -->
        <include name="com/**" />
    </jar>
</target>

<!-- Obfuscation with ProGuard -->

<property name="version" value="0.0.1"/>            <!-- change this occasionally -->

<property name="proguard-dir" value="proguard"/>
<property name="unoptimized" value="MyJar.jar"/>
<property name="optimized" value="MyJar_${version}.jar"/>


<target name="optimize" unless="nooptimize">

    <!-- Run obfuscator -->
    <java jar="${proguard-dir}/proguard.jar" fork="true" failonerror="true">
        <jvmarg value="-Dmaximum.inlined.code.length=16"/>
        <arg value="@${proguard-dir}/proguard.cfg"/>      
        <arg value="-injars ${unoptimized}"/>
        <arg value="-outjars ${optimized}"/>
        <arg value="-libraryjars /Users/decades/android-sdk-mac_x86/platforms/android-7/android.jar"/>
    </java>     
</target>   

构建和混淆没问题,但将生成的 MyJar_0.0.1.jar 导入测试项目后,代码完成不会显示正确的参数名称,而是 foo 显示为

foo(String arg0, long arg1, float arg2, LocationListener arg3 )

尽管指定了“keepparameternames”。

我花了几个小时却无法让它发挥作用。如果我导入未混淆的 JAR,一切都很好。包中的一些其他函数也显示了正确的参数名称...

没有任何线索,是吗?

问候

I'm trying to obfuscate an Android JAR using Proguard (4.7, but 4.6 problematic too). I could break down the problem into a simple sample project.

The problem: For some functions (unclear for what reasons) the argument names of exposed functions are lost, sometimes "scrambled" (really). I would like to focus on the "lost" part first, because the scrambled thing is much more weird...

1) I created a library project in Eclipse. Android SDK is 2.1-update 1 for some reasons
The project is marked as "Library Project" and has just one class MyJarEntry.java and one exported function foo

package com.decades.myjar;

import android.location.LocationListener;

public class MyJarEntry {

    public void foo(String provider, long minTime, float minDistance, LocationListener listener) {
    }
}

2) My project has a subdir "proguard" containing the latest proguard.jar plus the proguard.cfg, which looks like this

-printmapping out.map

-keepparameternames
-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
            SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-keep public class * {
    public protected *;
}

3) My Build.xml has targets for building the JAR and optimizing it like this, so after building the lib in Eclipse I do "ant jar" and "and optimize" in a terminal. This is build.xml

<project name="MyJar" default="jar" basedir=".">
<description>
  Lib JAR builder
</description>

<!-- set global properties for this build -->
<property name="res" location="res" />
<property name="bin" location="bin" />

<!-- Output directories -->
<property name="out.dir" value="bin" />
<property name="out.absolute.dir" location="${out.dir}" />
<property name="out.classes.dir" value="${out.absolute.dir}/com" />
<property name="out.classes.absolute.dir" location="${out.classes.dir}" />


<!-- Pack the jar -->
<target name="jar">
    <jar destfile="MyJar.jar" basedir="bin/">
        <!-- replace 'com' by what ever you are using -->
        <!-- as first part of the package name -->
        <!-- e.g. de, org, ... -->
        <!-- the ** is important to include the directory recursively -->
        <include name="com/**" />
    </jar>
</target>

<!-- Obfuscation with ProGuard -->

<property name="version" value="0.0.1"/>            <!-- change this occasionally -->

<property name="proguard-dir" value="proguard"/>
<property name="unoptimized" value="MyJar.jar"/>
<property name="optimized" value="MyJar_${version}.jar"/>


<target name="optimize" unless="nooptimize">

    <!-- Run obfuscator -->
    <java jar="${proguard-dir}/proguard.jar" fork="true" failonerror="true">
        <jvmarg value="-Dmaximum.inlined.code.length=16"/>
        <arg value="@${proguard-dir}/proguard.cfg"/>      
        <arg value="-injars ${unoptimized}"/>
        <arg value="-outjars ${optimized}"/>
        <arg value="-libraryjars /Users/decades/android-sdk-mac_x86/platforms/android-7/android.jar"/>
    </java>     
</target>   

Build and obfuscation is fine, but after importing the resulting MyJar_0.0.1.jar into a test project the code completion does not show up the right parameter names, instead foo is presented as

foo(String arg0, long arg1, float arg2, LocationListener arg3)

although "keepparameternames" is specified.

I have spent hours and hours and couldn't make it work. All is fine if I import the unobfuscated JAR. Some other functions in the package do also show up with their correct parameter names...

Having no clue, do you?

Regards

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

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

发布评论

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

评论(1

嘦怹 2024-12-30 02:34:49

ProGuard 4.7(及更早版本)似乎删除了未使用的参数的名称。我现在已经修复了这个问题以供将来的版本使用。您可以通过关闭优化(-dontoptimize)来解决这个问题。

请注意,您随时可以在 ProGuard 的错误跟踪器上报告错误。

ProGuard 4.7 (and older) appears to remove the names of unused parameters. I've now fixed this for future releases. You can work around it by switching off optimization (-dontoptimize).

Note that you can always report bugs on ProGuard's bug tracker.

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