23

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:+'

}
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
user1994183
  • 233
  • 1
  • 2
  • 4
  • i added an example of basic setup [here](http://stackoverflow.com/questions/26297042/flavors-and-instrumentation-tests-gradle-configuration/28860179#28860179) – sirFunkenstine Mar 04 '15 at 16:52

2 Answers2

39

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.

ivagarz
  • 2,434
  • 22
  • 22
  • 2
    I tried adding the androidTestFree and androidTestPaid folders in src folder like how I have the InstrumentTest folder. But the classes are not getting detected when I select the flavors and when I run the class I get Empty Test Suite warning. – user1994183 Mar 14 '14 at 04:27
  • Which version of the build tools are you using? In version 0.9 the instrumentTest folder was renamed to androidTest, but if you are using a previous version you will have to name the 'flavor' test folders accordingly. In your case that would be instrumentTestFree and instrumentTestPaid. – ivagarz Mar 14 '14 at 08:13
  • Thank you ivagarz. I use version 0.8 and changing the folder names solved the issue. – user1994183 Mar 14 '14 at 09:12
  • I use gradle 2.1, I use default sourceSets, My tests are in MyApplication/src/adroidTestFlavor1 and in MyApplication/src/adroidTestFlavor2. And it doesn't work. What do I wrong? – jakub Oct 10 '14 at 10:44
  • @qbait buildtools version? How are you executing the tests? – ivagarz Oct 10 '14 at 11:36
  • 1
    @ivagarz buildtools 'com.android.tools.build:gradle:0.13.+'. I execute tests from Android Studio (I add Android Tests configuration for module MyApplication). Only java folder in folder that I renamed from androidTest to androidTestFlavor1 is green, second folder androidTestFlavor1/java is orange as usual. – jakub Oct 10 '14 at 12:30
  • So helpful! Struggled with this for a little bit, trying to use gradle Test tasks. This is the only way I have found, without using a special plugin, to separate tests. – Billy Lazzaro Feb 13 '15 at 14:59
4

This is really what did it for me: How to specify unit test folder in two-dimension flavor

dependencies {
  androidTestFlavor1Compile "..."

  //In your case androidTestStbAppleCompile         
}
Community
  • 1
  • 1
FloG
  • 463
  • 5
  • 9