9

I migrated an older Android app to Android-Studio/Gradle. The test need uiautomator-v18 which requires minSdkVersion=18. However, I would like to have the minSdkVersion set to 15 or 16.

There are many questions on SO over the same thing, but I am just not able to resolve this problem.

Exerpt AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.searcher"
    android:versionCode="1"
    android:versionName="0.0.1" >

    <uses-sdk
        tools:overrideLibrary="android.support.test.uiautomator.v18"/>

    <!-- ... -->

</manifest>

The build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.searcher"
        minSdkVersion 15
        targetSdkVersion 23
        testApplicationId "com.example.searcher.test"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.google.android.gms:play-services-analytics:8.4.0'
    compile files('libs/dagger-1.2.1.jar')
    compile files('libs/dagger-compiler-1.2.1.jar')
    compile files('libs/javawriter-2.1.2.jar')
    compile files('libs/javax.inject.jar')

    androidTestCompile(
        'com.android.support:support-annotations:23.2.0',
        'com.android.support.test:runner:0.4.1',
        'com.android.support.test:rules:0.4.1',
        'com.android.support.test.uiautomator:uiautomator-v18:2.1.1',
        'org.hamcrest:hamcrest-library:1.3',
        'org.mockito:mockito-core:1.10.5',
        'junit:junit:4.12',
        'com.google.dexmaker:dexmaker:1.1',
        'com.google.dexmaker:dexmaker-mockito:1.1'
    )
}

The above gives the error:

Error:Execution failed for task ':app:processDebugAndroidTestManifest'.
> java.lang.RuntimeException: Manifest merger failed : uses-sdk:minSdkVersion 15 cannot be smaller than version 18 declared in library [com.android.support.test.uiautomator:uiautomator-v18:2.1.1] /mypath/app/build/intermediates/exploded-aar/com.android.support.test.uiautomator/uiautomator-v18/2.1.1/AndroidManifest.xml
    Suggestion: use tools:overrideLibrary="android.support.test.uiautomator.v18" to force usage

But I am already using the overrideLibrary.

If this is not possible, is it possible to have different minSdkVersion for "main" and "androidTest"?

EDIT: After adding the flavors, I was able to run the tests using build variant tstDebug. However, building it with prdDebug ends in an error saying that there are unknown stuff in the androidTest (example: package org.hamcrest does not exist). The modified excerpt of build.gradle:

defaultConfig {
    applicationId "com.example.searcher"
    targetSdkVersion 23
    versionCode 6
    versionName "0.5.0"
}

productFlavors {
    prd {
        minSdkVersion 15
    }
    tst {
        minSdkVersion 18
        testApplicationId "com.example.searcher.test"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
}

// And instead of "androidTestCompile" use "tstCompile"

Is it not possible to tell "Android Studio" that it should build the app without androidTest?

tokosh
  • 1,772
  • 3
  • 20
  • 37
  • Do you tried to change the version in AndroidManifest of your overrideLibrary ? – Sree Mar 04 '16 at 06:45
  • @Sree: Yes, same issue. – tokosh Mar 04 '16 at 07:23
  • I finally could make it run with "flavors": I failed to name the flavored tests correctly. So, for flavor "dev" the test folder should be `androidTestDev`. Which I found in this [answer](http://stackoverflow.com/a/28092437/3045181) – tokosh Mar 14 '16 at 04:01
  • Possible duplicate of [Set different minSdkVersion for testAndroid than for main app](https://stackoverflow.com/questions/30585289/set-different-minsdkversion-for-testandroid-than-for-main-app) – Bharathwaaj Jul 30 '19 at 05:07

2 Answers2

13

Did you put <uses-sdk tools:overrideLibrary ...> in src/main/AndroidManifest.xml?

If so, remove it from src/main/AndroidManifest.xml and try to put the following xml in src/androidTest/AndroidManifest.xml.

(Don't forget android:minSdkVersion="18")

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

    <uses-sdk
        android:minSdkVersion="18"
        tools:overrideLibrary="android.support.test.uiautomator.v18"
        />
</manifest>
sumio
  • 394
  • 3
  • 11
  • Thanks for the answer. The `AndroidManifest.xml` in src/androidTest/ is auto-generated. If I use `minSdkVersion` in the gradle.build, it will create an `AndroidManifest.xml` in build/ with the values from gradle.build. If I leave `minSdkVersion` undefined, it will use version `1` as default, which results in an error, too. – tokosh Mar 10 '16 at 01:13
  • @tokosh More precisely, `minSdkVersion` in your `build.gradle` and `src/androidTest/AndroidManifest.xml` are merged into `build/intermediates/manifest/androidTest/debug/AndroidManifest.xml`. So, storing`src/androidTest/AndroidManifest.xml` is significant. I confirmed it in the following environment: - Android Studio 1.5.1 - Android Gradle Plugin 1.5.0 - `minSdkVersion` in `app/build.gradle`: 16 – sumio Mar 22 '16 at 06:11
  • In addition to the previous comment, the merged AndroidManifest.xml still has `android:minSdkVersion="16"`. It is no problem. Declaring `tools:overrideLibrary` will prevent the manifest merger to fail in compilation. – sumio Mar 22 '16 at 06:35
7

One option is to have a product flavor for your tests:

android {
    productFlavors {
        production {
            minSdkVersion 15
        }
        uiTest {
            minSdkVersion 18
        }
    }
...
}

And then run the tests on the uiTest flavor builds.

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
kevinpelgrims
  • 2,148
  • 1
  • 18
  • 29
  • Sorry for trying it out late. Finally I got the tests running with a lower `minSdkVersion`. What I missed was setting the build-variant in "Android Studio". Without it the tests could not be run. I preferred if this were possible with `overrideLibrary` (as it seems to me this is its purpose). But well, enough time spent. – tokosh Mar 09 '16 at 10:17
  • If instead of a product flavor, you need to increase the minSdkVersion in a buildType, just wrap the minSdkVersion with defaultConfig, like this: `espresso { defaultConfig { minSdkVersion 18 } }` – Sebas LG Jan 30 '17 at 11:02