How do I keep my Android testing libraries separate from my application libraries in Android Studio?

I have my module dependencies set up like so in my build.gradle file:

dependencies
{
   compile fileTree(dir: 'libs', include: ['*.jar'])
   compile 'com.android.support:support-v4:22.2.0'
   androidTestCompile fileTree(dir: 'libs/test', include: ['*.jar'])
}

Inside my libs folder I have all the libraries necessary to building my application. I placed the libraries necessary to running tests in a subfolder called "test"

This seems to work as my code compiles, and I can make my module. When I attempt to run any of my tests, however, it fails to find the test libraries I attempted to include. The errors in the Gradle console actually point back to pieces of my code that don't show any errors.

Also, I do have my tests set up correctly, because I can get them to run by moving the required jars into the libs folder and changing my dependencies to:

dependencies
{
   compile fileTree(dir: 'libs', include: ['*.jar'])
   compile 'com.android.support:support-v4:22.2.0'
}

But then I don't really see the point of androidTestCompile if it's not working. I'd also rather not include these jar files in my apk if I don't have to.

If it helps, I'm using Android studio version 1.2.2 and Gradle plugin version 1.2.3.

Thanks in advance for any help.

Answers


See below example.

create tests folder parallel to src folder. create all test cases into that folder. if you create folder into another path you need to make call for this.(Added into code.)

 dependencies {
    androidTestCompile files('libs/dexmaker-1.1.jar')
    androidTestCompile files('libs/org.mockito:mockito-core:3.9.5')
    androidTestompile files('espresso-1.0-SNAPSHOT-bundled.jar')
    }

defaultConfig {

    testApplicationId "xxx.xx.abc"
    testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"

}

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']

    }

    // Move the tests to tests/java, tests/res, etc...
    instrumentTest.setRoot('tests')

    instrumentTest {

        java.srcDirs = ['tests/src']

    }

    androidTest.setRoot('tests')
    androidTest.java.srcDirs = ['tests/src']

You should use sourceSets to keep your apk free from the test librarys Gradle User Guide Use the 'main' one for your production code and the 'androidTest' one for the tests, gradle then automatically adds the specified sources.

   sourceSets {
        main {
            ...
        }
        androidTest {
            ...
        }
    }

Need Your Help

Counting the number of queries performed

mysql ruby-on-rails rspec rails-activerecord

I'd like to test that a certain piece of code performs as few SQL queries as possible.

Differences in subsetting data frame methods

r dataframe

tmp1 <- data.frame(x = 10, y = NA, z = NA)