是否可以使用Proguard Android库模块,而不是应用程序模块?

Dav*_*hoi 8 debugging android proguard gradle android-support-library

我希望有人可以回答我认为基本的Gradle/Proguard问题.

我有一个非常基本的Android项目.该项目包含名为的主应用程序模块,app以及名为的Android支持库的库模块AndroidSupport.

我希望独家运行Proguard AndroidSupport(即不是整个应用程序),因为我在Proguard-ed上运行应用程序上的仪器测试时遇到了麻烦.我的希望是我可以AndroidSupport自己缩小,以便我不需要Proguard我的应用程序代码(因此,避免测试不运行的问题).

这是我的app 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')
}
Run Code Online (Sandbox Code Playgroud)

我的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'
}
Run Code Online (Sandbox Code Playgroud)

我的AndroidSupport模块proguard-rules.pro看起来像:

-dontobfuscate
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.app.** { *; }
Run Code Online (Sandbox Code Playgroud)

app应用程序启用AndroidSupport了Proguard并禁用了Proguard时,我可以使用consumerProguardFiles proguard-rules.pro缩小功能AndroidSupport.
但是当我使用上面粘贴的配置时,我收到以下错误:

Error:Execution failed for task ':AndroidSupport:proguardRelease'.
java.io.IOException: The output jar is empty. Did you specify the proper '-keep' options?`
Run Code Online (Sandbox Code Playgroud)

有谁知道这个设置是否可行?仅在依赖库模块上启用Proguard,而不是在应用程序本身上启用?

提前致谢!

pet*_*tos 0

您不想让库模块的一些接口(API)保持可见吗?要从您的应用程序模块访问?在这种情况下,您应该将 -keep 选项添加到您的库 ProGuard 中,例如:

-keep,allowshrinking class com.example.androidsupportlibproject.YourClass {
    public <methods>;
    protected <methods>;
    public static <methods>;
    public <init>(...);
}
Run Code Online (Sandbox Code Playgroud)