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

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

问题描述

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

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天全站免登陆