简体   繁体   中英

Proguard: How not to optimize classes from a library

I've got a JavaME project here in which I had to include a given library. I'm using Netbeans 6.8, so I just added the library to the project. The classes of the library are then correctly packed into the resulting jar-file.

My problem now is that the classes of this library must not be touched by the Proguard obfuscator. So I've tried different -keep options:

-keep class com.package.**

I've also tried -keepnames and -keepclassmembers, but Proguard will quit saying:

Unexpected error while editing code:

Class = [com/package/class]

Method = [run()V]

Exception = [java.lang.IllegalArgumentException] (Invalid instruction offset [1077] in code with length [1075])

Error: Invalid instruction offset [1077] in code with length [1075]

Is there a way to tell Proguard to ignore a certain library or certain classes?

Thanks in advance!

If you want ProGuard to leave a library untouched, you should specify it with -libraryjars instead of -injars:

-libraryjars somelibrary.jar

Its classes won't be included in the output jar, so you then still have to add the library jar to the class path of your processed application.

Alternatively, you could process the library jar as an input jar, but keep its classes and class members :

-keep class some.library.** { *; }

Now, the unexpected error is something different. It indicates a bug in ProGuard. You should make sure you are using the latest version of ProGuard. Otherwise, you should report an issue on ProGuard's bug tracker at Sourceforge.

You can combine your JARs in a separate pass with ProGuard, specifying these options:

-dontnote **
-dontwarn **
-dontobfuscate
-dontshrink
-dontoptimize

Which has the simple effect of combining JARs without processing them at all.

The dontnote and dontwarn options are to suppress notes and warning, which for a simple combine operation are spurious (and there will be a number of them).

The first pass should look something like:

-injars        MyApplication-Unobfuscated.jar
-libraryjars   SomeLibraryJar.jar(!META-INF/**)
-libraryjars   AnotherLibraryJar.jar(!META-INF/**)
-outjars       MyApplication-Obfuscated.jar

// other options.

The second pass should look like:

-injars        MyApplication-Obfuscated.jar
-injars        SomeLibraryJar.jar(!META-INF/**)
-injars        AnotherLibraryJar.jar(!META-INF/**)
-outjars       MyApplication.jar

-dontnote **
-dontwarn **
-dontobfuscate
-dontshrink
-dontoptimize

The classes of the library are then correctly packed into the resulting jar-file.

That's where your issue is. Once it is in the jar provided to Proguard, it will be processed. I believe you must be creating a big jar with all dependencies. If so, exclude your JavaME code from this big jar and keep it in a separate jar. Then Proguard won't process it.

Else, find a way to move your JavaMe code in a separate jar and make it a dependency of the jar you are providing to Proguard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM