升级到Android Gradle插件3.4.0后,类别不再变得模糊

时间:2019-05-08 00:53:40

标签: android android-gradle android-proguard

升级到Android Gradle插件3.4.0后,基本类不再变得模糊。

以下基本步骤可以重现该问题:

  1. 在Android Studio中创建一个新的空项目
  2. phases: install: runtime-versions: php: 7.3 commands: - php -v - php -m - lsb_release -a - LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php - apt-get update -y - apt-get install -y php7.3-gd php7.3-xdebug - ls /usr/lib/php/20180731 - touch /usr/local/etc/php/conf.d/extra_config.ini - "echo extension_dir=\"/usr/lib/php/20180731/\" >> /usr/local/etc/php/conf.d/extra_config.ini" - "echo extension=gd.so >> /usr/local/etc/php/conf.d/extra_config.ini" - "echo zend_extension=xdebug.so >> /usr/local/etc/php/conf.d/extra_config.ini" - cat /usr/local/etc/php/conf.d/extra_config.ini - php -m 中,将//Turn a string into an array of characters let string = "aabcccdeeeef" let array = string.map {String($0)} //Turn the array into a counted set. let counted = NSCountedSet(array: array) //print the entries in the counted set. Note that sets don't preserve order counted.forEach{ print($0, ":", counted.count(for:$0)) } //Or if you want to use a counted set to build a dictionary of counts: var result = [String: Int]() array.forEach { result[$0] = counted.count(for: $0) } print(result) 更改为app/build.gradle
  3. 创建以下类minifyEnabled

    true
  4. 请参考ProguardTestClass.java中的课程:

    public class ProguardTestClass {
    
        public interface ProguardTestInnerInterface {
            void proguardTestCallback(String message);
        }
    
        public static void proguardTestMethod(String input, ProguardTestInnerInterface impl) {
            impl.proguardTestCallback("proguardTestMethod received input=[" + input + "]");
        }
    }
    
  5. 确认它正在MainActivity.java

  6. 中使用Android Gradle插件3.4.0
  7. 确认它正在 public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ProguardTestClass.proguardTestMethod("MainActivity's ProguardTestClass", new ProguardTestClass.ProguardTestInnerInterface() { @Override public void proguardTestCallback(String message) { Log.d(TAG, "Proguard test: " + message); } }); } } 中使用build.gradle
  8. 在“终端”标签中打开“终端”窗口
  9. 运行gradle-5.1.1
  10. 运行gradle/wrapper/gradle-wrapper.properties

预期输出:

./gradlew clean build

实际输出:

find . -name mapping.txt | xargs grep "ProguardTestClass -> "没有提到com.example.proguardgradleplugin340.ProguardTestClass -> com.example.proguardgradleplugin340.a: 的任何重命名。

要查看以前的Android Gradle插件版本可以产生预期的输出,请重复上述步骤,但请指向以前的Android Gradle插件和Gradle版本:

  1. app/build/outputs/mapping/release/mapping.txt中更改为Android Gradle插件3.3.2
  2. ProguardTestClass中更改为build.gradle

我试图通过调查其他diagnostic files that we used to get from Proguard来进行调查,但是它们不再输出了(至少默认情况下还是不输出):

gradle-4.10.1 gradle/wrapper/gradle-wrapper.properties

我还查看了Android Gradle插件3.4.0发行说明,发现它取代了Proguard,而采用了"integrates desugaring, shrinking, obfuscating, optimizing, and dexing all in one step"的新技术。

2 个答案:

答案 0 :(得分:2)

尝试将useProguard false设置为允许插件使用R8而不是ProGuard来缩小和混淆应用的代码和资源。例如。

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

或者,如果您想坚持使用ProGuard,则应从gradle.properties禁用R8,如下所示:

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

请记住设置 useProguard true


编辑#1

在此处查看如何将Proguard迁移到R8:Android/java: Transition / Migration from ProGuard to R8?

答案 1 :(得分:0)

@shizhen提供的解决方案是我接受的解决方案。但是我想记录一个替代方案,以防其他人遇到这个问题。

通过实际实例化类,我能够使R8模糊化该类。请注意,我的ProguardTestClass原始代码是如何不包含构造函数的,而我是以相当静态的方式使用它的。但是,当我添加一个构造函数并从MainActivity.java实例化它时,我猜想R8有理由对其进行混淆。

修订过的ProguardTestClass使R8执行混淆处理:

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 + "]");
    }
}

然后在MainActivity.java中实例化并在ProguardTestClass上调用onCreate()

    proguardTestClass = new ProguardTestClass(3);
    Log.d(TAG, "Proguard test: " + proguardTestClass.incrementValue());