如何在Android Studio 的类路径中配置库的顺序? [英] How to configure the order of libraries in the classpath for Android Studio?

查看:51
本文介绍了如何在Android Studio 的类路径中配置库的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改 Android Studio 类路径中库的顺序.我正在尝试使用 JUnit 4 从 IDE运行单元测试.这按照], [2]

解决方案

我使用以下任务来确保 SDK 依赖项列在最后:

task pushDownJdkDependency {def imlFile = file("ui.iml")最后一个{尝试 {def parsedXml = (new XmlParser()).parse(imlFile)def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }parsedXml.component[1].remove(jdkNode)new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': "Android API 18 Platform", 'jdkType': 'Android SDK'])def writer = new StringWriter()new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml)imlFile.text = writer.toString()} catch (FileNotFoundException e) {//nop, iml 未找到}}}

确保您使用正确的 SDK 标识符.然后将任务挂钩到构建过程中:

gradle.projectsEvaluated {preBuild.dependsOn(pushDownJdkDependency)}

除此之外,将任务添加到您的运行配置中.很遗憾,由于缺乏声誉,我无法发布图片.

I would like to change the order of libraries in the classpath for Android Studio. I am trying to run unit tests with JUnit 4 from within the IDE. This works following the instruction in these slides.

I wonder if it is possible to configure the order of the libraries - other then suggested in the slides - via the Run Configuration of Android Studio?

The first attempt was to change the order of dependencies in the Project Structure dialog in the Dependency tab of the project module as show in the screenshot.

The second guess was to include the classpath configuration in the build.gradle file. There are a couple of vague ideas about this:

sourceSets.main.compileClasspath = file("foo.jar") + sourceSets.main.compileClasspath

...

sourceSets.test.compileClasspath = configurations.robo + sourceSets.test.compileClasspath
sourceSets.test.runtimeClasspath = configurations.robo + sourceSets.test.runtimeClasspath

  • Sources: [1], [2]

解决方案

I use the following task to make sure that the SDK dependency is listed last:

task pushDownJdkDependency {
    def imlFile = file("ui.iml")
    doLast {
        try {
            def parsedXml = (new XmlParser()).parse(imlFile)
            def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }

            parsedXml.component[1].remove(jdkNode)
            new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': "Android API 18 Platform", 'jdkType': 'Android SDK'])
            def writer = new StringWriter()
            new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml)
            imlFile.text = writer.toString()

        } catch (FileNotFoundException e) {
            // nop, iml not found
        }
    }
}

make sure you use the correct SDK identifier. Then hook the task into the build process:

gradle.projectsEvaluated {
    preBuild.dependsOn(pushDownJdkDependency)
}

Other than that, add the task to your run configuration. Unfortunately I cannot post images due to lack of reputation.

这篇关于如何在Android Studio 的类路径中配置库的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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