简体   繁体   中英

What's the difference between -keep class com.sample.Foo {*;} and -keep class com.sample.Foo?

I just built two release APKs.

One of them had

-keep class com.sample.Foo

And the other had

-keep class com.sample.Foo {*;}

When I decompiled the app on http://www.javadecompilers.com/apk , Foo was the same for both builds.

The code of Foo is below:

package com.sample;

import android.util.Log;

public class Foo {

    public int field;

    public Foo() {
        field = 90;
    }

    public int doSth() {
        doPrivate();
        return 2 * field;
    }

    private void doPrivate() {
        Log.d("Proguard", "doPrivate");
    }

    void doDef() {
        Log.d("Proguard", "doDef");
    }
}

That's how I made sure Foo and its methods weren't obfuscated:

package com.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.lang.reflect.Method;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            Class<?> aClass = Class.forName("com.sample.Foo");
            Object instance = aClass.newInstance();
            Method method = aClass.getMethod("doSth");
            Integer res = (Integer) method.invoke(instance);
            Log.d("Proguard", "doSth res=" + res);
        } catch (Exception e) {
            Log.e("Proguard", "", e);
        }
    }
}

Update #1

My Proguard config.

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

The first one only keeps the class and its constructor.

The second one keeps the class and its constructor and any field or method inside the class.

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