0
votes

I have obfuscated my code using proguard. In the code I want to read all classes from a specified package with following code.

    URL directoryURL = Thread.currentThread().getContextClassLoader()
            .getResource("com/test/ui/controller");

This code does not work because jar created by proguard does not keep package hierarchy. I write a code that reads entries from obfuscated jar.This is output. See that class packages does not kept in zip file. So getResource() does not work.

com/test/ui/controller/a.class

com/test/ui/controller/c.class

com/test/ui/controller/b.class

com/test/ui/controller/d.class

When I run same code with an unobfuscated jar., here is output. Package level is kept in jar.Do you have any idea how can I tell Proguard to create jar by keeping package level.

com/

com/test/

com/test/ui/

com/test/ui/controller/

com/test/ui/controller/a.class

com/test/ui/controller/c.class

com/test/ui/controller/b.class

com/test/ui/controller/d.class

2

2 Answers

0
votes

-keeppackagenames [package_filter]

Specifies not to obfuscate the given package names. The optional filter is a comma-separated list of package names. Package names can contain ?, *, and ** wildcards, and they can be preceded by the ! negator. Only applicable when obfuscating.

(Source: http://proguard.sourceforge.net/manual/usage.html#obfuscationoptions)

0
votes

-keepdirectories solved my problem.

Specifies the directories to be kept in the output jars (or aars, wars, ears, zips, apks, or directories). By default, directory entries are removed. This reduces the jar size, but it may break your program if the code tries to find them with constructs like "mypackage.MyClass.class.getResource("")". You'll then want to keep the directory corresponding to the package, "-keepdirectories mypackage". If the option is specified without a filter, all directories are kept. With a filter, only matching directories are kept. For instance, "-keepdirectories mydirectory" matches the specified directory, "-keepdirectories mydirectory/*" matches its immediate subdirectories, and "-keepdirectories mydirectory/**" matches all of its subdirectories.