Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 489
Joe Grande Avatar asked Jul 07 '15 20:07

Joe Grande


People also ask

Where do I put libraries in Android Studio?

Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.

What is the difference between AAR and jar?

An AAR is similar to a JAR file, but it can contain resources as well as compiled byte-code. AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.

Where are Android libraries located?

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. The gradle folder is hidden.

What is external libraries in Android Studio?

You are developing an Android app on Android Studio, sometimes you want to use an external library for your project, such as a jar file. Common langs is an java library with open source code which is provided by the Apache, it has utility methods for working with String, numbers, concurrency ...


1 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']
like image 113
NovusMobile Avatar answered Oct 31 '22 19:10

NovusMobile