天天看點

android代碼混淆aar,如何使用Proguard混淆android庫(.aar)?

android代碼混淆aar,如何使用Proguard混淆android庫(.aar)?

I want to obfuscate the .aar library using proguard for distribution purpose, I tried many solution over internet but nothing has worked till now, only some code are obfuscated. Can anybody help me to solve the issue?

解決方案

In your build.gradle, add consumerProguardFiles under defaultConfig :

android {

compileSdkVersion Integer.parseInt("${COMPILE_SDK}")

buildToolsVersion "${BUILD_TOOLS_VERSION}"

defaultConfig {

targetSdkVersion Integer.parseInt("${TARGET_SDK}")

minSdkVersion Integer.parseInt("${MIN_SDK}")

consumerProguardFiles 'consumer-proguard-rules.pro'

}

}

You add a file named consumer-proguard-rules.pro under the root folder of your library project. This file should contain all the proguard rules of the dependencies mentioned in the build.gradle of your library project. You should add a keep rule for your POJO classes, interfaces with annotations and whatever other class that you need to be kept safe from proguard cleaning or obfuscation.

If you shrink resources, you should add a keep.xml file in your resources under res (res/raw for example). In this file you specify the drawables or layouts to keep. You check in your code in you use reflexion to call a resource with getResources().getIdentifier(..), if so you add your drawables like this.

xmlns:tools="http://schemas.android.com/tools"

tools:shrinkMode="safe"

tools:keep="@layout/layout1, @drawable/icon1,@drawable/icon2,@drawable/img_*"/>

You can use * to tell proguard to keep all the drawables that start with img_ under the drawable folder.