Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

Project Structure

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]
like image 430
JJD Avatar asked Apr 04 '14 13:04

JJD


People also ask

How do I change the Gradle path in Android Studio?

To change its path go to this path File > Settings... > Build, Execution, Deployment > Gradle In the Global Gradle settings Change Service directory path to what you want.

Does the order of Gradle dependencies matter?

This isn't necessary. The order inside the dependencies { } block is preserved. You are kind of right. Dependency ordering is preserved, but for example all compile dependencies will be before testCompile dependencies no matter of how you order them.

Where do I put repositories in Android Studio?

Open the project where you want to use your cloned library, then select 'File > New > Import Module' from the Android Studio toolbar. Click the three-dotted button and navigate to your cloned repository. Select this repository, then click 'OK. '

Where are libraries stored in Android Studio?

The android default libraries like appcompact, design support libraries are stored in your folder where you have installed the SDK, precisely <SDK FOLDER>/extras/android/m2repository/com/android . The 3rd party libraries are stored in . gradle/caches/modules-2/files-2.1 folder.


1 Answers

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.

like image 129
omoser Avatar answered Sep 18 '22 03:09

omoser