我想隐藏 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
然后这个应用程序运行没有失败,但我可以看到生成的 Java 文件 arr
文件。所以我想隐藏Java代码。
最佳答案
对于不应该提供的库 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 规则配置。请参阅我上面链接的问题,其中一个答案提供了一个基本配置,以保留公共(public)方法(以便人们仍然可以使用该库,一旦它只是被混淆了)。
另请参阅 Android 测试模板:module-android-library .
我会假设,这个 AAPT 错误源于提供了一个空的 ProGuard 配置文件。
关于java - 收缩库代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43317074/