【问题标题】:Shrink library code收缩库代码
【发布时间】:2017-04-10 07:22:58
【问题描述】:

我想隐藏aar 文件中的真实代码。我的库的 Gradle 文件如下所示。

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
}

我在运行此应用程序时遇到以下异常。

Warning:Exception while processing task java.io.FileNotFoundException: E:\MyProjects\TestApp\testLibrary\build\intermediates\proguard-rules\release\aapt_rules.txt (The system cannot find the path specified)
Error:Execution failed for task ':testLibrary:transformClassesAndResourcesWithProguardForRelease'.
> Job failed, see logs for details

如果我将minifyEnabled true 更改为minifyEnabled false,则此应用程序运行正常,但我可以从生成的arr 文件中看到Java 文件。

所以我想隐藏 Java 代码。

【问题讨论】:

标签: java android gradle


【解决方案1】:

对于一个库,你不应该提供proguard-rules.pro,而是一些proguard-consumer-rules.pro...,其中包含配置规则,当库被编译成APK包时(与通常的行为相反,当库正在构建中)。

// These rules will be applied when a consumer of this library sets 'minifyEnabled true'.
consumerProguardFiles 'proguard-consumer-rules.pro'

该库并没有像那样直接被混淆,而是稍后才被混淆 - 无需让该库的每个使用者都必须定义自己的 ProGuard 规则配置。请参阅我上面链接的问题,其中一个答案提供了基本配置,以保留公共方法(以便人们仍然可以使用该库,一旦它只是被混淆了)。 另请参阅 Android 测试模板:module-android-library

我会假设,这个 AAPT 错误源于提供了一个空的 ProGuard 配置文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 2012-12-07
    • 2010-12-07
    • 1970-01-01
    • 2017-12-28
    • 2016-09-18
    相关资源
    最近更新 更多