How to configure the order of libraries in the classpath for Android Studio?

▼魔方 西西 提交于 2019-11-30 22:06:24

问题


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]

回答1:


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.




回答2:


You have to modify the project .iml file to have the JDK as the last entry like the following:

    <orderEntry type="library" 
                exported="" 
                scope="TEST" 
                name="wagon-provider-api-1.0-beta-6" 
                level="project" />
    <orderEntry type="library" 
                exported="" 
                scope="TEST" 
                name="xercesMinimal-1.9.6.2" 
                level="project" />
    <!-- make sure this is the last orderEntry -->
    <orderEntry type="jdk" 
                jdkName="Android API 19 Platform" 
                jdkType="Android SDK" /> 
  </component>
</module>

For a full working example see Robolectric's deckard-gradle sample.



来源:https://stackoverflow.com/questions/22863845/how-to-configure-the-order-of-libraries-in-the-classpath-for-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!