Class no longer obfuscated after upgrading to Android Gradle plugin 3.4.0

前端 未结 2 928
无人共我
无人共我 2021-01-14 19:07

After upgrading to Android Gradle plugin 3.4.0, a basic class no longer gets obfuscated.

The following basic steps can reproduce the problem:

  1. In Androi
2条回答
  •  悲&欢浪女
    2021-01-14 19:34

    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());
    

提交回复
热议问题