How to obfuscate android library(.aar) using proguard?

大城市里の小女人 提交于 2019-12-23 09:50:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/44236208/how-to-obfuscate-android-library-aar-using-proguard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!