我希望有人能回答我认为是基本的 Gradle/Proguard 问题。
我有一个非常基础的 Android 项目。此项目包含名为 app
的主要应用模块和名为 AndroidSupport
的 Android 支持库的库模块。
我希望仅在 AndroidSupport
上运行 Proguard(即不在整个应用程序上),因为当它被 Proguard 编辑时,我无法在应用程序上运行仪器测试。
我希望我可以自己缩小 AndroidSupport
,这样我就不需要混淆我的应用程序代码(从而避免测试未运行的问题)。
这是我的应用
build.gradle
。请注意 Proguard 已禁用:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.androidsupportlibproject"
minSdkVersion 9
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
minifyEnabled false //Proguard DISABLED
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':AndroidSupport')
}
我的 AndroidSupport
模块启用了 Proguard:
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 9
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled true //Proguard ENABLED
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:support-annotations:22.2.0'
}
我的 AndroidSupport
模块 proguard-rules.pro
看起来像:
-dontobfuscate
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.app.** { *; }
当 app
应用程序启用了 Proguard 而 AndroidSupport
禁用了 Proguard 时,我可以使用 consumerProguardFiles proguard-rules.pro
来缩小 Android 支持
。
但是当我使用上面粘贴的配置时,出现以下错误:
Error:Execution failed for task ':AndroidSupport:proguardRelease'.
java.io.IOException: The output jar is empty. Did you specify the proper '-keep' options?`
有人知道这个设置是否可行吗?要仅在依赖库模块上启用 Proguard,而不是在应用程序本身上启用 Proguard?
提前致谢!
最佳答案
难道你不想让一些接口(interface)——你的库模块的 APIs 可见吗?要从您的应用程序模块访问?在这种情况下,您应该将 -keep 选项添加到您的库 ProGuard 中,例如:
-keep,allowshrinking class com.example.androidsupportlibproject.YourClass {
public <methods>;
protected <methods>;
public static <methods>;
public <init>(...);
}
关于android - 是否可以 Proguard 一个 Android 库模块,而不是应用程序模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32059379/