Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use different proguard files in different flavors or for different build types?

I need to make different flavors for test libraries. Why? Well, I want project to be fully secured so I wanna minifying to be enabled all the time(even for debug build type).

While making tests I'm using different classes from project, so I decided to change proguard files using flavor. It looks like something this :

android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            testProguardFile 'test-proguard-rules.pro'
        }
        debug {
            minifyEnabled true
            testProguardFile 'test-proguard-rules.pro'
        }
    }
    ...
    productFlavors{
        forTest{
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules1.pro', 'debug-rules.pro'
        }
        forRelease{
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'debug-rules.pro'
        }
   }
}

And proguard-rules* files

proguard-rules:

-dontwarn blablabla.**
-dontwarn blabla.**
-keep class bla.** { *; }

proguard-rules1:

-dontshrink
-dontobfuscate
-dontwarn

But this wasn't helpful for me. I want to run tests without obfuscation. Flavors doesn't have such param as minifyEnabled so I try to find any solution. So any suggestions about resolving an issue? May be someone had the same problem and find another solution (not using flavors) ?

like image 830
Mody Avatar asked Feb 26 '16 17:02

Mody


People also ask

How do I explicitly tell ProGuard which classes to keep?

For example, you may want to explicitly tell ProGuard which classes to keep. To do this, create a new .cfg file and apply the ProGuardConfiguration build action in the Properties pane of the Solution Explorer: Keep in mind that this configuration file does not replace the Xamarin.Android proguard_xamarin.cfg file since both are used by ProGuard.

What should I know about ProGuard before using it?

One important item to know in advance before using ProGuard is how it works within the Xamarin.Android build process. This process uses two separate steps: Each of these steps is described next. The Xamarin.Android linker employs static analysis of your application to determine the following: Which assemblies are actually used.

How do I add a custom ProGuard configuration file?

Optionally, you can add a custom ProGuard Configuration file to exert more control over the ProGuard tooling. For example, you may want to explicitly tell ProGuard which classes to keep. To do this, create a new .cfg file and apply the ProGuardConfiguration build action in the Properties pane of the Solution Explorer:

How to create ProGuard rules in Android with one module?

1.Proguard in Normal project with one module. In Android when we create a Project we get a file created inside our project structure with the name proguard-rules.pro Once we start adding up the code and new features we use few third-party libraries also, and once we move forward towards release we add up respective Proguard rules.


1 Answers

After a whole day messing with this I found the following works:

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

productFlavors {
    flavorA {
        applicationIdSuffix ".a"
        proguardFile 'flavorA_rules.pro'
    }
    flavorB {
        applicationIdSuffix ".b"
    }

}

More info: Include additional configurations

like image 88
Mark Avatar answered Sep 20 '22 02:09

Mark