crash using ORMLite on Android with proguard - android

We're using ORMLite in our Android app. It's working fine, except when we try to do a build with proguard switched on.
I've read various similar posts, and so far I've got in my proguard-project.txt
-keep class com.j256.** {
*;
}
as suggested in the following discussion http://sourceforge.net/p/proguard/discussion/182456/thread/6765bb69
and I've got
-keepclassmembers class * {
public <init>(android.content.Context);
public <init>(android.app.Activity,int);
}
as suggested in another stackoverflow question Proguard with OrmLite on Android
But it still not working. I can get it to run if I add
-dontobfuscate
but that somewhat missing the point of using proguard in the first place.
When I run I get an
IllegalStateException: Could not find OpenHelperClass because none of the generic
parameters of class class <our.package.name>.LaunchActivity extends
OrmLiteSqliteOpenHelper. You should use getHelper(Context, Class) instead.
Where
public class LaunchActivity extends OrmLiteBaseActivity<DatabaseHelper>
and
public class DatabaseHelper extends OrmLiteSqliteOpenHelper
I've added
-keep public class * extends com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
-keep public class <our.package.name>.LaunchActivity
But still no luck. This question seems to have been asked before (Problems with OrmLite and proguard obfuscation) but I'm hoping somebody will know what the solution is!

The error message mentions generic parameters, so ORMLite is probably using reflection to retrieve generic type information. This information is stored in optional Signature attributes (Java erases generic types), which ProGuard removes by default. You can keep them with
-keepattributes Signature

Related

In Android, is there a proguard rule that keeps all inner objects of a class and protects them against obfuscation?

I have some dagger classes in an android SDK that need to be protected from minification and obfuscation when creating and publishing the release version. I protect those by using custom proguard rules around the #Module and #DaggerGenerated annotations.
-keep #dagger.Module public class * { public <methods>; }
-keep #dagger.internal.DaggerGenerated public class * {public <methods>;}
Some of those classes have companion objects that should also be protected. At the moment, I protect them by annotating the companions specifically with #Keep. Is there a way to specify a proguard rule to automatically keep the companion?
I tried a few variations such as the one below but none of them got the job done.
-keep #dagger.Module class *$Companion { *; }
Perhaps not an ideal solution, but you could instead try to keep an entire package instead of individual files, using something like:
-keep class com.foo.bar.** { *; }
this way, you'll be covering any companion objects found here as well

What does -keep class do in ProGuard

I know that keep class is suppose to keep the class.
But let's say if I keep class on a specific class, will it not obfuscate the said class?
-keep class com.myproject.activities.**
What is the difference between keep class vs keep public class
I know this is too late to answer but this may helpfull for someone.
From Guard Square documentation about -keep
-keep Specifies classes and class members (fields and methods) to be preserved as entry points to your code. For example, in order to keep an application, you can specify the main class along with its main method. In order to process a library, you should specify all publicly accessible elements.
You can check below links for more details about ProGuard specifires.
https://www.guardsquare.com/en/proguard/manual/usage
http://omgitsmgp.com/2013/09/09/a-conservative-guide-to-proguard-for-android/

Proguard: How to avoid shrinking (and obfuscating) an entire package to avoid removing (and obfuscating) "unused methods"?

I'm currently using an Android library that uses a lot of reflection.
As soon as I enable proguard, and run it... it crashes.
Why? It uses a lot of reflection and the methods are only invoked via reflection, so they are detected by proguard as unused and removed during the shrinking process, so a a NoSuchMethodError is thrown.
Why this happens? That is easy to figure out they are removed during the shrinking process as proguard considers they are unused and therefore removes that pieces of code (all the methods)
So, how can I configure proguard to avoid shrinking or obfuscating an entire package? (the library)
Note: I don't want to use the -dontshrink option, as it's all or nothing and I just want to avoid a specific package.
More info:
The runtime error is the following:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.my.app.debug, PID: 3771
java.lang.NoSuchMethodError: observeValueForKeyPath [class java.lang.String, class java.lang.Object, class com.my.lib.util.Dictionary, class java.lang.Object]
at com.my.lib.util.Observable$ObservationManager$Observer.<init>(SourceFile:47)
at com.my.lib.util.Observable$ObservationManager$Observer.<init>(SourceFile:26)
at com.my.lib.util.Observable$ObservationManager.addObserver(SourceFile:159)
...
Take note that the problem is one an inner inner class...
My current configuration has something like:
-keep,includedescriptorclasses class com.my.** { *; }
-keepclassmembers class com.my.lib** { *; }
-keep,includedescriptorclasses class com.my.lib.util.Observable$* { *; }
-keep,includedescriptorclasses class com.my.lib.util.Observable$*$* { *; }
But this apparently only avoids obfuscating the methods not removed during the shrinking process... I need to avoid removing methods during shrinking.
According to the documentation -keep should work even when shrinking, while -keepclassmembers only works "if [the] classes are preserved as well".
-keep class com.library.** { *; }
For the sake of people in the future with similar problems, I'll explain what really was happening:
The -keep class my.app.package was really avoiding methods to be removed by shrinking and obfuscated (#F43nd1r's answer made me realize that by really pointing that the docs said that it avoid also shrinking), so the problem was elsewhere
After decompiling the lib's code and passing some time reading it, I found that it received as a parameter an instance of another class (not from that lib) and a string with a method name that it used for reflection. That was the problem, I needed to also avoid obfuscating that class.
I use the option -dontshrink to prevent shrinking.
Source: link

How to make Proguard keep the class method names and attribute names as it is?

As above.
My ProGuard config is
-keepclassmembers public class myApp.interfaces.**
-keepclasseswithmembernames public class myApp.interfaces.**
-keepattributes Signature
-keepparameternames
Really new to this and no real idea what I'm doing.
What I'm trying to achieve is to make Proguard not change the method and variable names of all Classes inside myApp.interfaces.*. If they want to rearrange some algo stuff to optimise it or whatever, it's fine. As long as the names are not changed.
This must be done because of the Serializable issue between server and client.
Simply use:
-keep class myApp.interfaces.** { *; }
It will not touch the class or its members. If you get errors / warnings, Rebuild the project.

How to tell Proguard to obfuscate class names

I would like proguard to obfuscate classnames. I have this line in Proguard.cfg
-keepclasseswithmembers class * {
public static <fields>;
}
-keepnames class * implements java.io.Serializable
-keep public class com.google.**
And I notice that what is not obfuscated is the class names. So running jdgui i see
com/test/abcd/ActualClass.java
public class ActualClassName extends Activity etc
moreover I see methods returning real classnames. like
ActualClassname aa();
and imports statements like
import com.abcd.ActualClassName
How do I get Proguard to obfuscate the classname itself. Its not just for Activities that I see this my Adapters are not being obfuscated. Well there is obfuscation going on but not class names.
Is the rules above what prevents the class names from being obfuscated?
Update: i have since removed the rules above and a Utility class that does not extend anything from Android is not being obfuscated. I'm now wondering if there is some implicit rule about keeping class names of classes that are referenced from classes that are being kept like Activity derivied classes? The classes whose names are not being obfuscated have a few things in common:
1) Static methods
2) Import of other types which are kept like those deriving from activity or serializable.
3) They have methods with parameters of other classes (Some of which might need to be kept).
However there is no where that I am specifically requesting that these utility classes should be kept.
There are several class that appear in your code that must retain the same fully qualified class name in order for android to be able to find them. One example as above are all Activity classes, as they are defined in the manifest by their full name as a String. If proguard were to rename the class then Android would no longer be able to find them.
The typical Proguard config will refer to the Proguard config in the Android SDK which will include several important lines like this one:
-keep public class * extends android.app.Activity
I think your problem is coming from your first line: '-keepclasseswithmembers' includes the class name, so in your case any class with a static field will keep its name. Changing it to simply '-keepclassmembers' will obfuscate the class names while leaving the static fields intact(which I'm assuming you want to do).
That said, I'm not sure why you want to do this, though. If you're trying to preserve the static variable names, shouldn't you want to preserve the class names as well, seeing as how that's how you'll be accessing the static fields? Why are you trying to preserve all of your static variable names?

Categories

Resources