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?

- 2,390
- 1
- 17
- 45

- 51
- 1
- 3
-
Refer https://stackoverflow.com/questions/30201420/how-to-include-a-proguard-configuration-in-my-android-library-aar?rq=1 and https://stackoverflow.com/questions/26983248/proguard-ignores-config-file-of-library – Manohar May 29 '17 at 07:03
-
@Redman Thanks for fast response,i have gone through the above links but it obfuscate the code to some extent and resources folders remains the same.. – Niranjan S May 29 '17 at 10:16
1 Answers
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.
<?xml version="1.0" encoding="utf-8"?>
<resources
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.

- 945
- 8
- 18