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

查看:420
本文介绍了该库的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'
    }
}

应用gradle:

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

一个以上的人为错误.如我所见,库中的所有类都丢失了.

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

推荐答案

您需要弄清楚

  1. 在图书馆上运行proguard
  2. 在应用程序上运行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会删除所有未使用的代码,并扰乱您不想保留的名称.

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天全站免登陆