为testAndroid设置不同的minSdkVersion而不是主应用程序

时间:2015-06-02 00:02:10

标签: android testing android-uiautomator

是否可以为测试设置不同的minSdkVersion而不是应用程序本身?我问,因为我想使用新的测试支持库和UI Automator进行测试。但是,这仅适用于API 18+。与此同时,我仍然希望支持旧版Android,尽管没有经过全面测试。为了做到这一点,我需要添加到build.gradle文件中?

为了澄清,我正在使用Android Studio和" new"基于Gradle的项目结构。

11 个答案:

答案 0 :(得分:66)

我是从谷歌the new testing template获得的。

AndroidManifest.xmltest文件夹中创建新的androidTest文件。

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

答案 1 :(得分:6)

我已将解决方案的示例上传到mauricegavin/android-testing,因为我自己无法找到合适的解决方案。

感兴趣的模块是 ui / uiautomator / BasicSample / app 。您会注意到androidTests目录中有一个AndroidManifest.xml。您在BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(new File (this.imageFileUri.getPath()).getAbsolutePath(), options); int srcWidth = options.outWidth; int srcHeight = options.outHeight; System.out.println("The image width is>>>>>> " + srcWidth + "The image height is >>>>>>" + srcHeight); int scale = 1; int desiredWidth = 10; while(srcWidth / 2 < desiredWidth){ srcWidth /= 2; srcHeight /= 2; scale *= 2; } options.inJustDecodeBounds = false; options.inDither = false; options.inSampleSize = scale; this.bmp = BitmapFactory.decodeFile(file1.getAbsolutePath(), options); 中指定的minSdkVersion仍将用于app/build.gradledebug版本。

您会看到sample project's build.gradle中的release指定了{uiutomator不支持的minSdkVersion,并且通常会导致构建失败。

api 17

感谢mattblang我在本例中使用的答案。

答案 2 :(得分:6)

尝试这个。

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
}

答案 3 :(得分:5)

当您运行androidTest时,将在末尾包含一个"AndroidTest"任务。这就是为什么我根据此模式过滤任务。

我的任务名称是-:app:assembleMyFlavourDebugAndroidTest

defaultConfig {

    gradle.startParameter.taskNames.each {
        if (it.contains("AndroidTest")) {
            minSdkVersion 18
        } else {
            minSdkVersion 16
        }
    }
}

答案 4 :(得分:1)

是的,你可以。您应该将特定于测试的清单条目放在src/androidTest/AndroidManifest.xml中。在构建测试时,manifest merger将组合两个清单,但在构建应用程序时,只会使用主AndroidManifest.xml。

有关详细信息,请参阅this answer

答案 5 :(得分:1)

发布此问题后,我还想将minSdkVersion设置为debugrelease版本的不同值。但是,我还没有机会测试这是否有效。

我还从this blog post找到了一个可能的工作。创建单独的testproduction风格:

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

答案 6 :(得分:1)

@ Code-Apprentice几乎就在那里。但是你不能将产品风味命名为“test”,“androidTest”或“release”。它们就像关键字一样,你不能使用这些名称。

所以答案是

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

答案 7 :(得分:1)

我遇到了以下冲突,为了进行测试,我需要更高的minSdkVersion。

我找到的解决方案是使用buildType change the test build type从以下文档中获得的,它为我完成了工作

以下是解决方法:

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

将buildType更改为stage,应该没问题

答案 8 :(得分:1)

使用 androidx ,您可以使用 tools:overrideLibrary =“ android_libs.ub_uiautomator”

强制使用版本<18中的UI自动器
<?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> 

但是如果您在版本<18上运行测试,可能会导致运行时失败。

答案 9 :(得分:0)

我的解决方案基于风味配置:

  
      
  1. 分为两种味道:
  2.   
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. 将与测试相关的依赖项移至&#34; autoTestCompile&#34;
  2.   
// 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. 运行测试
  2.   

Build Variant Screenshot

答案 10 :(得分:0)

这是最黑的版本。我花了将近一天的时间来创建这个脚本。请注意这个纪念品,但只能将此作为最后的手段

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