Set different minSdkVersion for testAndroid than for main app

时光毁灭记忆、已成空白 提交于 2019-11-28 06:11:44

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>
Maurice Gavin

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.

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
}

With androidx you can force usage of UI automator in version < 18 with tools:overrideLibrary="android_libs.ub_uiautomator"

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:tools="http://schemas.android.com/tools" package="..." >

    <uses-sdk tools:overrideLibrary="android_libs.ub_uiautomator"/>
</manifest> 

But it may lead to runtime failures if you are running your tests on version < 18

I got the following conflict, for testing i needed a higher minSdkVersion.

The solution i found was taken from the following document using the buildType change the test build type and it did the job for me

The following is the solution:

android {
defaultConfig {
    applicationId "com.doronkettner.ilikemovies"
    ...
    minSdkVersion 18
    ...
    testBuildType "staging"
    ...
    }
    ...
buildTypes {
    release {...}

    debug {...}

    staging {
        initWith(buildTypes.debug) // keep versionName and PIN from 'debug'
        defaultConfig.minSdkVersion 19
    }
}

Change the buildType to stage and it should be ok

Allen Hair

Yes, you can. You should put test-specific manifest entries in src/androidTest/AndroidManifest.xml. When building your tests the manifest merger will combine both manifests, but when building your app only the main AndroidManifest.xml will be used.

See this answer for more details.

After posting this question, I also had the idea to set minSdkVersion to different values for the debug and release builds. However, I haven't had a chance to test if this works or not.

I also found one possible work around from this blog post. Create separate test and production flavors:

productFlavors {
    // The actual application flavor 
    production {
        minSdkVersion 14
    }
    // Test application flavor for uiautomatior tests
    test {
        minSdkVersion 18
    }
}

@Code-Apprentice is almost there. But you can not name a product flavor to "test", "androidTest" or "release". They are like keyword, and you cannot use these names.

So the answer is

    productFlavors {
        product{
            minSdkVersion 15
        }
        uiautoTest {
            minSdkVersion 18
        }
    }

My solution based on flavor configuration:

  1. split to two flavor:
buildTypes {
  release {...}
  debug {...}
}

productFlavors {
   dev { ... }
   autoTest {
      minSdkVersion 18 // set to 18 only in this flavor
      multiDexEnabled true // if you got dex index overflow error
      testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
   }
}
  1. move your test related dependencies into "autoTestCompile"
// for test
autoTestCompile 'com.android.support.test:runner:0.5', {
    exclude group: 'com.android.support', module: 'support-annotations'
}
autoTestCompile 'com.android.support.test:rules:0.5', {
    exclude group: 'com.android.support', module: 'support-annotations'
}
autoTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
}
autoTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'design'
    exclude group: 'com.android.support', module: 'recyclerview-v7'
}
autoTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
}
  1. Run Test

This is the hackiest version. It took me almost a day to create this script. Please take note of this for keepsake, but only use this as a last resort.

android.applicationVariants.all { variant ->

//Making specific variant disablements for faster build
if (variant.buildType.name.contains("debug")) {
    println "Making min version to 21 and disabling multidex"
    variant.mergedFlavor.setMultiDexEnabled false

    def versionMin = new com.android.builder.core.DefaultApiVersion(21)
    variant.mergedFlavor.setMinSdkVersion versionMin
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!