Question

I recently activated ProGuard for my Eclipse Android project. After adding external libs and dynamically referenced classes to the proguard.cfg, I don't get any errors when building the apk. I get however a NoSuchMethodError when I try to start the installed app.

I narrowed it down to a specific method called in the onCreate method of the main activity. To simplify things, here's what the class and method look like (I left out a lot of code, but I think this should illustrate it):

public class TestMain extends TabActivity implements OnSharedPreferenceChangeListener{
    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        testMethod();
    }
}

testMethod() is defined as follows:

private void testMethod() {
    int charsLeft = maxPostMessageLength - someEditText.length();
    ...
}

When I remove the "someEditText.length()" part, the app starts. So, the way I see it, the method that can't be found is the EditText.length() method. Strangely, though, the app also starts when I remove "someEditText.length()" from the testMethod and put it directly into the onCreate method:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        test = someEditText.length();
        testMethod();
    }

Does anyone know how I can get rid of this error and why I can call someEditText.length() directly in the onCreate method but not in a method called by the onCreate method? Without using Proguard the app works fine, of course.

Edit: I tried the -dontshrink, -dontobfuscate and the -dontoptimzie options in the proguard.cfg. With -dontoptimize the app starts without errors. Still, it would be interesting what exactly causes this specific error.

Was it helpful?

Solution 2

I accidentally stumbled upon a possible solution. Well, it totally works in my case, so this IS a solution to the original problem: Today, I implemented some code with @Override annotations, which didn't work, at first. Luckily, someone else already had the same problem and an easy Eclipse-related solution: 'Must Override a Superclass Method' Errors after importing a project into Eclipse

Now, I thought, well, if I was always using Java level 1.5 before, why not try ProGuard again, without the -dontoptimize option, now that I set it to 1.6. Can't hurt...

And what can I say, now the app starts and I don't get the strange error when EditText.length() is called in a method.

OTHER TIPS

The Proguard documentation proudly states: "The ProGuard tool shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes".

Well, I gave up with the 'shrinking' part of it after getting runtime errors like you describe. I added the line

-dontshrink

to the proguard.cfg

You can see which routines have been removed from your code by inspecting the file usage.txt. I'm happy to say that in my projects it's always missing, meaning that the code is obfuscated but nothing has been removed. I don't get any runtime errors now.

The optimizer may remove the method invocation and the method if it comes to the conclusion that the method doesn't have any side-effects. It should never create inconsistent code though, and I'm not aware of a problem like this. You should check if it persists with the latest version of ProGuard. Otherwise, you should file a bug report on the ProGuard site, preferably with a small example that illustrates the problem.

I had a similar problem to the OP and it ended up being a proguard config option I set -allowaccessmodification, removing this solved the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top