'Custom build Type not working on androidTest building

Hi i am trying to build a androidTest APK based on a flavour and a custom build type i have defined below:

 productFlavors {
    FlavourOne {
        applicationIdSuffix ".live"
        buildConfigField 'String', 'SERVER_BASE_URL', '"http://live.com"'

    }
    FlavourTwo {
        applicationIdSuffix ".demo"
        buildConfigField 'String', 'SERVER_BASE_URL', '"http://demo.com"'
    }

}

buildTypes {
        debug {
            minifyEnabled false
            // shrink code (remove unused classes and methods) - note that it falls back to experimental shrinker for Instant Run
            shrinkResources false // don't strip unused res files
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'proguard-rules-debug.pro'
            testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-test.pro'
        }
        release {
            minifyEnabled true // shrink code (remove unused classes and methods)
            shrinkResources false // don't strip unused res files
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
        debugDemo {
            applicationIdSuffix '.demo'
            versionNameSuffix '-DEMO'
            minifyEnabled false
            // shrink code (remove unused classes and methods) - note that it falls back to experimental shrinker for Instant Run
            shrinkResources false // don't strip unused res files
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'proguard-rules-debug.pro'
            testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-test.pro'
        }
        demo {
            applicationIdSuffix '.demo'
            versionNameSuffix '-DEMO'
            minifyEnabled true // shrink code (remove unused classes and methods)
            shrinkResources false // don't strip unused res files
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }

When i run gradlew assembleFlavourOneDebugDemoAndroidTest i get an error straight away saying

Task 'assembleFlavourOneDebugDemoAndroidTest' not found in root project 'MyProject'.

It works fine if i omit my custom buildType and just do assembleFlavourOneAndroidTest and it works. It also works if do assembleFlavourOneDebugANdroidTest only...



Solution 1:[1]

According to the documentation only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with:

android {
    testBuildType "demo" 
}

and your gradle task after sync should look like this:

./gradlew assembleFlavourOneDemoAndroidTest

and be careful there will be NO debug as you pointed out in your description at the end.

assembleFlavourOneDebugDemoAndroidTest

Solution 2:[2]

I faced the similar problem, I need debug build type when develop, debugMinify build type when run androidTest on pipeline or assembleAPK

so follow bellow code , You can run androidTest on debug type when develop and when you run assemble APK command , testBuildType will be changed

android{
   buildTypes{
      release{}
      debug{}
      debugMinify{}
   }
   testBuildType getCurrentVariant()
}

def getCurrentVariant() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    println(tskReqStr)

    if (tskReqStr.contains("assemble") && tskReqStr.contains("DebugMinify")) {
        print("buidType: debugMinify")
        return "debugMinify"
    } else {
        print("buidType: debug")
        return "debug"
    }
}

and

gradlew assembleDevelopDebugMinify 
gradlew assembleDevelopDebugMinifyAndroidTest

[DefaultTaskExecutionRequest{args=[assembleDevelopDebugMinify],projectPath='null'}]

buidType: debugMinify

Solution 3:[3]

I'd assume some declarations lack the initWith instruction -

because any test build rigidly depends on initWith debug.

When using initWith release, no test tasks are generated.

debugDemo {
    initWith debug
    ...
}

The initWith property allows you to copy configurations from other build types, then configure only the settings you want to change.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3