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

前端 未结 2 709
天涯浪人
天涯浪人 2021-01-08 00:19

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

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-08 01:22

    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.

提交回复
热议问题