Exclude package when building android release library build with gradle

I have a gradle file that builds a library: it contains some external Java code that is also used on other Java platforms where it contains unit test code (found in ./../java-common/src-java/

). I would like to exclude the unit test package when building a release library, and keep this code when building a debug or androidTest (or test) build.

But if I specify to exclude the unit test package in a release build, it does the same for other build types. Here is my build.gradle:

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'

// This is required to be able to create the javadocs when using java 8
if (JavaVersion.current().isJava8Compatible()) {
    allprojects {
        tasks.withType(Javadoc) {
            options.addStringOption('Xdoclint:none', '-quiet')
        }
    }
}

android {
    defaultPublishConfig 'release'
    publishNonDefault true
    testBuildType "debug"

    compileSdkVersion 22
    buildToolsVersion "24.0.3"

    lintOptions {
        abortOnError false
    }

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            sourceSets {
                main {
                    java {
                        exclude '**/com/mycompany/util/unittest/**'
                    }
                }
            }
        }
        debug {
            minifyEnabled false
            debuggable true
        }
    }

    sourceSets {
        main.java.srcDirs += '../../java-common/src-java/'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test:runner:0.4'
    androidTestCompile 'com.android.support.test:rules:0.4'
    androidTestCompile 'com.google.android.gms:play-services-ads:9.2.1'
    androidTestCompile 'com.google.android.gms:play-services-base:9.2.1'
    androidTestCompile fileTree(include: ['*.jar'], dir: 'libs-unittest')
}

...

      

If I run this config, the release build is built without com/mycompany/util/unittest

, but when I try to run the debug build or androidTest (unit test), the build does not include the required com / mycompany / util / unittest package.

Can anyone help me?

Thank!

+3


source to share





All Articles