Weird behaviour while obfuscating a JAR with proguard

不想你离开。 提交于 2019-12-02 05:17:10

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8656447/weird-behaviour-while-obfuscating-a-jar-with-proguard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!