如何使用Android R8保留类构造函数自变量参数名称 [英] How to keep class constructor argument parameter names with Android R8

查看:450
本文介绍了如何使用Android R8保留类构造函数自变量参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Android库,因此我想保留一些构造函数/方法的参数名称.我正在将我的库部署为AAR文件.

I'm writing Android library, so I want to preserve parameter names for some constructors/methodes. I'm deploying my library as AAR file.

在Gradle从3.3.2升级到3.4.0之后,在一切正常之前,将构造函数和公共方法中的所有参数重命名为"var1"之类的东西.

After Gradle upgrade from 3.3.2 to 3.4.0 all arguments in constructors and public methods are renamed to something like "var1", before that everything was fine.

据我了解,主要区别在于,现在默认情况下使用R8代替Proguard来缩小和混淆我们的代码.所以可能我在配置中丢失了一些东西.

As I understand main difference is that, now by default R8 is used to minify and obfuscate our code instead of Proguard. So probably I'm missing something in configuration.

让我说我上课:

public class Foo {
    public String bar;

    public Foo(String bar) {
        this.bar = bar;
    }
}

所以在proguard-rules.pro中,我有:

So in proguard-rules.pro I have:

-keepparameternames
-keepattributes MethodParameters
-keepattributes Signature

-keep class my.class.path.Foo { *; }

然后在我的模块gradle文件中加载该配置:

And I load that configuration in my module gradle file:

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

现在我的AAR文件中的类看起来像这样->

Right now my class inside my AAR file looks like that ->

public class Foo {
    public String bar;

    public SecureElement(String var1) {
        this.bar = var1;
    }
}

因此,即使保留了字段名,但在我的构造函数中也没有保留变量名.

So even field name has been preserved, but not variable name in my constructor.

如何保留方法/构造函数的参数名称?

How can I preserve method/constructor argument names?

我想念什么?

推荐答案

此处 ,请检查R8版本1.5.51是否对您一切正常

As said here, check if everything works for you with R8 version 1.5.51

buildscript {
    repositories {
        maven {
            url 'http://storage.googleapis.com/r8-releases/raw'
        }
    }
    dependencies {
        // Must be before the Gradle Plugin for Android
        classpath 'com.android.tools:r8:1.5.51'
        classpath 'com.android.tools.build:gradle:X.Y.Z'
     }
}

或者您也可以按照此处

您可以通过在项目的gradle.properties文件中添加以下行之一来禁用R8:

You can disable R8 by adding one of the following lines to your project’s gradle.properties file:

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

这篇关于如何使用Android R8保留类构造函数自变量参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆