Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 378
Jonathan Avatar asked Jul 18 '19 12:07

Jonathan


People also ask

How do I get the current build type in Gradle?

In your build. gradle (:app): tasks. all { Task task -> if (task.name == "preDebugBuild") { doFirst { //for debug build } } else if (task.name == "preReleaseBuild") { doFirst { //for release build } } } dependencies { ... }

What is Buildtype in Gradle Android?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.


2 Answers

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
like image 175
Ewei Avatar answered Oct 20 '22 10:10

Ewei


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

like image 4
Kebab Krabby Avatar answered Oct 20 '22 10:10

Kebab Krabby