proguard 中的否定符(感叹号)应该允许我保留除 apache 库之外的东西:
-keep class !org.apache.**
根据这些答案。这就是要走的路:
但是,它混淆了我的 APK 中的所有类。
这是我的 build.gradle 的一部分(我有 Android Studio 3.5.3)
compileSdkVersion 29
buildToolsVersion "29.0.2"
//...
buildTypes {
release {
minifyEnabled true
proguardFiles /*getDefaultProguardFile('proguard-android.txt'),*/ 'proguard-rules.pro'
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources false
}
}
dependencies {
//Utility libs
implementation 'org.apache.commons:commons-collections4:4.1'
implementation 'org.apache.commons:commons-lang3:3.4'
implementation group: 'commons-io', name: 'commons-io', version: '2.5'
}
在我添加
-printconfiguration
之后给我的proguard-rules.pro
我看到的文件有很多-keep
遵循我的-keep class !org.apache.**
的规则-printconfiguration
-keep class !org.apache.**
# Referenced at ***anonymized***\app\build\intermediates\merged_manifests\release\AndroidManifest.xml:180
-keep class android.support.v4.app.CoreComponentFactory { <init>(); }
# Referenced at ***anonymized***\app\build\intermediates\merged_manifests\release\AndroidManifest.xml:180
-keep class com.mycompany.MyApplication { <init>(); }
# Referenced at C:\Users\***anonymized***\.gradle\caches\transforms-2\files-2.1\7f5f0b3369d8fa8a72a20e2278ec0acc\appcompat-v7-28.0.0\res\layout\abc_action_menu_item_layout.xml:17
-keep class android.support.v7.view.menu.ActionMenuItemView { <init>(...); }
Ezekiel Baniaga 建议的方法也没有工作。相反,它保留了包括 apache 包在内的所有内容:
proguard-rules.pro
-printconfiguration
-dontshrink
-dontoptimize
-dontobfuscate
-keep,allowshrinking,allowoptimization,allowobfuscation class org.apache.**
最佳答案
如果这不再起作用,您应该向 R8 项目提交错误报告。
为了同时继续使用 Proguard,您可以将其添加到您的 gradle.properties 文件中:
android.enableR8=false
进一步的测试表明,ProGuard 的隐式行为没有像 R8 中那样实现。
所以像这样的规则:
-keep class !org.apache.**
使用 ProGuard 时将隐式保留所有其他类,但在使用 R8 时不会。要使用 R8 实现相同的行为,请将规则更改为:
-keep class !org.apache.**,**
关于android - Proguard (R8) 否定运算符(operator)不工作以保留某些包以外的任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59248681/