如何使用工具:使用多个库覆盖库 [英] How to use tools:overrideLibrary with multiple libraries

查看:23
本文介绍了如何使用工具:使用多个库覆盖库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个兼容的 Android 项目,例如API 级别 4,但仍想使用 UiAutomator 对其进行测试,这需要在较新的设备上使用 API 级别 18.因此,该应用程序也可以在旧设备上运行,但会在新设备上执行自动测试.

I would like to create an Android project that is compatible to e.g. API level 4 but would still like to test it with UiAutomator that requires API level 18 on newer devices. So the app would also run on old devices but the automatic tests would be performed on new devices.

因此我使用 Android Studio 创建了一个新项目并添加了 UiAutomator 测试库:

Therefore I have created a new project with Android Studio and added the UiAutomator test libraries:

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:23.0.1'
  androidTestCompile 'com.android.support:support-annotations:23.0.1'
  androidTestCompile 'com.android.support.test:runner:0.3'
  androidTestCompile 'com.android.support.test:rules:0.3'
  androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}

编译时报错

Error:Execution failed for task ':app:processDebugAndroidTestManifest'.
> java.lang.RuntimeException: Manifest merger failed : uses-sdk:minSdkVersion 4 cannot be smaller than version 8 declared in library [com.android.support.test:runner:0.3] /Users/dom/Entwicklung/MacBookPro/git/GradleTest/app/build/intermediates/exploded-aar/com.android.support.test/runner/0.3/AndroidManifest.xml
    Suggestion: use tools:overrideLibrary="android.support.test" to force usage

添加

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

到 AndroidManifest.xml 导致另一个错误发生:

to the AndroidManifest.xml causes another error to occur:

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 4 cannot be smaller than version 7 declared in library [com.android.support:appcompat-v7:23.0.1] /Users/dom/Entwicklung/MacBookPro/git/GradleTest/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.0.1/AndroidManifest.xml
    Suggestion: use tools:overrideLibrary="android.support.v7.appcompat" to force usage

改成

<uses-sdk tools:overrideLibrary="android.support.test, android.support.v7.appcompat"/>

再次导致第一个错误:

Error:Execution failed for task ':app:processDebugAndroidTestManifest'.
> java.lang.RuntimeException: Manifest merger failed : uses-sdk:minSdkVersion 4 cannot be smaller than version 8 declared in library [com.android.support.test:runner:0.3] /Users/dom/Entwicklung/MacBookPro/git/GradleTest/app/build/intermediates/exploded-aar/com.android.support.test/runner/0.3/AndroidManifest.xml
    Suggestion: use tools:overrideLibrary="android.support.test" to force usage

我使用的是最新的 Android Studio 和构建工具.Android Studio 用于构建应用程序的 gradle 命令是

I am using the latest Android Studio and build tools. The gradle command that Android Studio uses to build the app is

Gradle tasks [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:compileDebugSources, :app:compileDebugAndroidTestSources]

因此可以重现问题,例如与

so the problem can be reproduced e.g. with

gradle :app:generateDebugAndroidTestSources :app:compileDebugAndroidTestSources

在 tools:overrideLibrary 中指定多个库的正确语法是什么?我读过逗号和空格是正确的,但它似乎不起作用.我已经在 StackOverflow 和其他地方阅读了很多关于它的内容,但不幸的是,到目前为止我找不到解决方案(除了注释掉测试).

What is the correct syntax to specify multiple libraries in tools:overrideLibrary ? I have read that a comma and a space would be correct, but it doesn't seem to work. I have read a lot about it here on StackOverflow and elsewhere but unfortunately I couldn't find a solution so far (aside from commenting out the tests).

推荐答案

我发现解决方案是创建一个 second AndroidManifest.xml,仅用于测试.它必须保存到测试目录并且只需要包含 overrideLibrary 语句:

I have found out that the solution is to create a second AndroidManifest.xml, just for the tests. It has to be saved into the tests directory and needs to contain only the overrideLibrary statement:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="${applicationId}">
    <uses-sdk tools:overrideLibrary="android.app, android.support.test, android.support.test.rule, android.support.test.espresso, android.support.test.espresso.idling, android.support.test.uiautomator.v18"/>
</manifest>

如果你为你的任务使用不同的目录,你可以在你的 gradle 文件中这样指定:

If you are using a different directory for your tasks, you can specify it this way in your gradle file:

androidTest.setRoot('src_test_uiautomator')

AndroidManifest.xml 文件必须位于该目录的根目录中,测试源位于java"子目录中.

The AndroidManifest.xml file has to be in the root of that directory, the test sources in the "java" subdirectory.

这篇关于如何使用工具:使用多个库覆盖库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆