Set different minSdkVersion for testAndroid than for main app Set different minSdkVersion for testAndroid than for main app android android

Set different minSdkVersion for testAndroid than for main app


I got this from the new testing template from Google.

Create a new AndroidManifest.xml file in your test or androidTest folder.

<?xml version="1.0" encoding="utf-8"?><manifest    xmlns:tools="http://schemas.android.com/tools"    package="your.package.name">    <uses-sdk tools:overrideLibrary="android.support.test.uiautomator.v18"/></manifest>


try this one.

defaultConfig {    applicationId "com.test"    if (gradle.startParameter.taskNames.contains(":app:assembleDebug")) {        minSdkVersion 21    }else{        minSdkVersion 14    }    targetSdkVersion 22    versionCode Integer.parseInt(VERSION_CODE)    versionName VERSION_NAME}



updated 2020-04-16


You can also set with productFlavors

android {    compileSdkVersion 29    defaultConfig {        applicationId "com.test"        targetSdkVersion 29        versionCode 1        versionName "1.0.0"    }    buildTypes {        debug {        }        release {            minifyEnabled true        }    }    flavorDimensions "default"    productFlavors {        development {            minSdkVersion 21        }        production {            minSdkVersion 16        }    }}


I've uploaded an example of the solution to mauricegavin/android-testing as I couldn't find a working one myself.

The module of interest is ui/uiautomator/BasicSample/app. You'll notice that there is an AndroidManifest.xml in the androidTests directory. The minSdkVersion you specify in your app/build.gradle will still be used for debug and release builds.

You'll see that the minSdkVersion in the sample project's build.gradle specifies api 17 which is not supported by uiautomator and would usually cause the build to fail.

<?xml version="1.0" encoding="utf-8"?><manifest    xmlns:tools="http://schemas.android.com/tools"    package="com.example.android.testing.uiautomator.BasicSample" >    <uses-sdk tools:overrideLibrary="android.support.test.uiautomator.v18"/></manifest>

Thanks to mattblang for his answer which I used for this example.