Test package for different flavors in Android Studio

user1994183 picture user1994183 · Mar 13, 2014 · Viewed 14.9k times · Source

I am experimenting flavors on an application in androidstudio. I have to write different test classes for the flavors, as I have different class files for the flavors. But I wonder if there is any option to specify test packages for each flavor in build.gradle. Here is my build.gradle for reference. I use 0.4.6 version of AndroidStudio.

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

defaultConfig {
    minSdkVersion 8
    targetSdkVersion 19
    testPackageName "com.example.tests"
}

productFlavors {

    Paid {

        packageName "com.example.paid"

    }
    Free {

        packageName "com.example.free"
    }
}

sourceSets {
    main {
        java.srcDirs = ['src/main/java']
        res.srcDirs = ['src/main/res']
    }

    Paid {
        java.srcDirs = ['src/Paid/java']
        res.srcDirs = ['src/Paid/res']
    }

    Free {
        java.srcDirs = ['src/Free/java']
        res.srcDirs = ['src/Free/res']
    }
}

signingConfigs {

    releaseConfig {

        storeFile file('filename');
        storePassword('filepwd');
        keyAlias "aliasname";
        keyPassword "aliaspassword";
    }

}

buildTypes {

    release {

        runProguard true
        debuggable false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        signingConfig signingConfigs.releaseConfig
        packageNameSuffix ".release"

    }

    debug {

        runProguard false
        debuggable true
        packageNameSuffix ".debug"

    }       
 }
}

dependencies {

    compile project(':androidViewPagerIndicator_library')
    compile 'com.android.support:appcompat-v7:+'

}

Answer

ivagarz picture ivagarz · Mar 13, 2014

From the documentation

Testing multi-flavors project is very similar to simpler projects.

The androidTest sourceset is used for common tests across all flavors, while each flavor can also have its own tests.

As mentioned above, sourceSets to test each flavor are created:

  • android.sourceSets.androidTestFlavor1
  • android.sourceSets.androidTestFlavor2

So, just as you should have now 'free' and 'paid' folders with code specific for each flavor, you can add 'androidTestFree' and 'androidTestPaid' folders where you you can add test cases specific to each one of your flavors.