Search code examples
androidproguardandroid-productflavors

Obfuscate only one flavour


How would i go about obfuscating just one flavour.

Unfortunately flavour 2 relies on a module (jar) that uses some duplication of classes, and i cannot obfuscate it due to the way it is set up. (3rd party) So wish to skip obfuscating the flavour.

I do not seem able to define minifyENabled false in the flavours section, or add the flavour to the build section.

Note, there are actually 6 flavours in total. The desire is to pick and choose flavours that should be obfuscated

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

    productFlavors {
        flavour1{
            applicationId "uk.co.company.flavour1"
        }
        flavour2{
            applicationId "uk.co.company.flavour2"
        }
   }

Solution

  • As long as there is not present minifyEnabled in ProductFlavor DSL object, then you have to create another buildType, e.g. releaseMinified along with standard release.

    buildTypes {
        release {
            minifyEnabled false
        }
        releaseMinified {
            minifyEnabled true
        }
    }
    
    productFlavors {
        minifiableFlavor{}
        nonMinifiableFlavor{}
    }
    

    And enable this build type only for the flavor that needs that:

    android.variantFilter { variant ->
        if (variant.buildType.name.equals('releaseMinified') && !variant.getFlavors().get(0).name.equals('nonMinifiableFlavor')) {
            variant.setIgnore(true);
        } else if (variant.buildType.name.equals('release') && variant.getFlavors().get(0).name.equals('nonMinifiableFlavor')){
            variant.setIgnore(true);
        }
    }
    

    Then you'll end up with:

    Build types