Class no longer obfuscated after upgrading to Android Gradle plugin 3.4.0

眉间皱痕 提交于 2019-12-06 09:33:36

Try to set useProguard false to let the plugin use R8 instead of ProGuard to shrink and obfuscate your app’s code and resources. E.g.

android {
    ...
    buildTypes {
        debug {
            minifyEnabled true
            useProguard false
        }
        release {
            minifyEnabled true
            useProguard false 
        }
    }
}

Or alternatively, if you want to stick to ProGuard, you should disable R8 from gradle.properties like below:

# Disables R8 for Android Library modules only.
android.enableR8.libraries = false
# Disables R8 for all modules.
android.enableR8 = false

And remember to set useProguard true.


Edit #1

Check here for how to migrate Proguard to R8: Android/java: Transition / Migration from ProGuard to R8?

Michael Osofsky

The solution from @shizhen is what I accepted as the solution to the problem. But I wanted to document an alternative in case anyone else runs into this problem.

I was able to make R8 obfuscate the class by actually instantiating the class. Notice how my original code for ProguardTestClass contained no constructor and I used it in a fairly static way. But when I added a constructor and instantiated it from MainActivity.java, that gave R8 a reason to obfuscate it I guess.

Revised ProguardTestClass that made R8 perform the obfuscation:

public class ProguardTestClass {

    //
    // NEW CODE: BEGIN
    //
    private int avar;

    public ProguardTestClass(int avar) {
        this.avar = avar;
    }

    public int incrementValue() {
        return ++avar;
    }
    //
    // NEW CODE: END
    //

    public interface ProguardTestInnerInterface {
        void proguardTestCallback(String message);
    }

    public static void proguardTestMethod(String input, ProguardTestInnerInterface impl) {
        impl.proguardTestCallback("proguardTestMethod received input=[" + input + "]");
    }
}

Then in MainActivity.java I instantiated and called the ProguardTestClass on onCreate():

    proguardTestClass = new ProguardTestClass(3);
    Log.d(TAG, "Proguard test: " + proguardTestClass.incrementValue());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!