Android:如何使用比在库中声明的更低的minSdkVersion(uiautomator-v18)

时间:2016-03-04 06:41:48

标签: android android-gradle build.gradle

我将较旧的Android应用迁移到Android-Studio / Gradle。测试需要uiautomator-v18,需要minSdkVersion=18。但是,我希望minSdkVersion设置为15或16。

关于同一件事,SO上有很多问题,但我无法解决这个问题。

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>

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

上面给出了错误:

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

但我已经在使用overrideLibrary了。

如果无法做到这一点,是否可以为&#34; main&#34;提供不同的minSdkVersion。和&#34; androidTest&#34;?

修改: 添加风味后,我能够使用构建变体tstDebug运行测试。但是,使用prdDebug构建它会导致错误,表示androidTest中存在未知内容(例如:package org.hamcrest does not exist)。修改后的摘录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"

是否无法告诉&#34; Android Studio&#34;它应该在没有androidTest的情况下构建应用程序?

2 个答案:

答案 0 :(得分:11)

您是否将<uses-sdk tools:overrideLibrary ...>放入src/main/AndroidManifest.xml

如果有,请将其从src/main/AndroidManifest.xml中删除,然后尝试将以下xml放入src/androidTest/AndroidManifest.xml

(不要忘记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>

答案 1 :(得分:6)

一种选择是为您的测试提供产品风味:

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

然后在uiTest味道构建上运行测试。