库的 Android Studio consumerProguardFiles 不起作用 [英] Android Studio consumerProguardFiles for the library doesn't work

查看:249
本文介绍了库的 Android Studio consumerProguardFiles 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的库中使用 proguard,但文件(规则)应该在库中设置.这意味着我不想在我的应用模块中明确设置规则(属于库).

I'd like to use proguard on my library, but the file (rules) should be set inside the library. That means I don't want to set the rules w(hich belong to the library) explicitly in my app module.

我发现有类似于 consumerProguardFiles 的属性.我的设置:

I found that there is something like consumerProguardFiles property. My settings:

图书馆gradle:

buildTypes {
    debug {
        debuggable true
        minifyEnabled false
    }
    release {
        minifyEnabled true
        consumerProguardFiles 'proguard-rules.pro'
    }
}

应用程序梯度:

buildTypes {
    debug {
        applicationIdSuffix ".debug"
        debuggable true
        minifyEnabled false
        signingConfig signingConfigs.debug
    }
    release {
        debuggable false
        minifyEnabled true
        signingConfig signingConfigs.release
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

为什么上述配置不起作用?我收到无法找到库中的包/符号的错误.

Why does configuration above doesn't work? I get errors that packages/symbols from the library cannot be found.

重要的是我的库的 proguard-rules.pro 和主应用模块的 proguard-rules.pro 都是空的.

示例性错误:

Error:(3, 60) error: package (my library package here) does not exist
(...)
Error:(16, 9) error: cannot find symbol class NavigationStructureModel
(...)

更多然后一个handrued错误.正如我所看到的,我的图书馆中的所有课程都丢失了.

More then one handrued errors. As I can see my all classes from the library are missing.

推荐答案

你需要弄清楚你是否愿意

You need to figure out if you want to

  1. 在库上运行 proguard
  2. 在应用上运行 proguard(提示:就是这个.)

在库上运行 proguard

那是到达应用程序之前.您的库 proguard 规则负责使所有代码可访问并呈现,但您可能会混淆内部类.

Run proguard on the library

That's BEFORE it reaches the application. Your library proguard rules are responsible for making all code accessible and present but you may obfuscate internal classes.

这是库 build.gradle:

Here's the library build.gradle:

android {
    defaultConfig {
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    buildTypes {
        release {
            minifyEnabled true // Yes, run proguard on the library itself.
        }
    }
}

如果您将此库作为开源发布,则您不需要它.如果你不向公众发布图书馆,你就不需要这个.

If you release this library as open source you don't need this. If you don't release the library to public you don't need this.

我收到无法找到库中的包/符号的错误.

I get errors that packages/symbols from the library cannot be found.

Proguard 构建类之间的依赖关系图.在这一点上,库是一个独立的单元,因此如果您没有提供任何规则,则不会使用任何类,因此 proguard 会将它们全部删除.

Proguard builds a graph of dependencies among classes. At this point the library is a standalone unit so if you didn't supply any rules, none of the classes are ever used so proguard removes them all.

让消费者(应用程序)知道,要保留库的哪些类.

Let the consumer (application) know, which classes of the library are to be kept.

这是库 build.gradle:

Here's the library build.gradle:

android {
    defaultConfig {
        // Here's what proguard on the app should do on the library's behalf.
        consumerProguardFiles 'proguard-consumer-rules.pro' 
    }
    buildTypes {
        release {
            minifyEnabled false // No, don't proguard the library itself.
        }
    }
}

proguard 是如何工作的

您需要告诉 proguard 要保留哪些代码以及要保留哪些名称.Proguard 会删除所有未使用的代码并打乱您不想留下的名称.

How does proguard work

You need to tell proguard what code to keep and what names to keep. Proguard removes all unused code and scrambles names you didn't want to stick around.

您不是告诉 proguard 删除什么,而是告诉 proguard 保留什么.

You don't tell proguard what to remove, you tell proguard what to keep.

这篇关于库的 Android Studio consumerProguardFiles 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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