IAmGroot
IAmGroot

Reputation: 13865

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"
        }
   }

Upvotes: 6

Views: 1519

Answers (2)

beigirad
beigirad

Reputation: 5734

As deprecation guide of VariantFilter:

Use AndroidComponentsExtension.beforeVariants API to disable specific variants

We can write this code:

android{...}

androidComponents {
    beforeVariants(selector().withBuildType("release")) { variant ->
        // assume `flavour1` should not be minified
        if (variant.flavorName == "flavour1")
            variant.isMinifyEnabled = false
    }
}

Upvotes: 0

azizbekian
azizbekian

Reputation: 62209

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

Upvotes: 7

Related Questions