1

About a week ago I asked this question

Why is library module android.support.test not visible in add dependency

After much head scratching and project setup morphing I discovered that this app/build.gradle dependency configuration gets me so much further than ever before in that at least the project compiles but does not build when attempting to run:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "com.my.package.name"
        minSdkVersion 8
        targetSdkVersion 22
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } }

    packagingOptions {
        exclude 'LICENSE.txt'
    }

}

dependencies {
    compile 'com.android.support:appcompat-v7:22.1.0'
    compile 'com.android.support:support-v4:22.1.0'
    compile 'com.android.support.test.espresso:espresso-core:2.1'
    compile 'com.android.support.test:testing-support-lib:0.1'
}

Notice compile instead of androidTestCompile on the testing support dependencies. Using androidTestCompile results in the problems noted in my previous question identified above.

Also notice the two srcDirs in the sourceSets declaration. That declaration was automatically set when I manually created the androidTest/java directory. Should this be declared differently?

Also too, getDefaultProguardFile is underlined in the ide with the tool tip "cannot resolve symbol 'getDefaultProguardFile'". In case that is crucial.

This new revelation, switching androidTestCompile to compile, leads me to believe there is something wrong with the way I set up the project structure for the unit test. Here is a picture of my project structure:Project folder structure

I should mention that this project/module structure was automatically created when migrating from Eclipse. By the icon decorations it looks like it at least recognizes my test class as a test class.

But now I am stuck at this error:

Error:Execution failed for task ':mymodule:dexDebug'. com.android.ide.common.internal.LoggedErrorException: Failed to run command: C:\Users\myuser\AppData\Local\Android\sdk\build-tools\22.0.1\dx.bat --dex --no-optimize --output C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\dex\debug --input-list=C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\tmp\dex\debug\inputList.txt Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Landroid/support/test/BuildConfig; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303) at com.android.dx.command.dexer.Main.run(Main.java:246) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106)

Not sure if this is coming from my project setup or if it is coming from the libraries due to using compile on the support.test libraries.

Any advice on why androidTestCompile doesn't work is appreciated.

Also I just 2 days ago took all the latest updates from SDK Manager to 22.0.1 to no avail.

Another thing I noticed is the only jar I seem to find containing package com.google.common.* is in the espresso library. (com.google.common.* is a dependency of the testing libraries.) But when I inspect the espresso library jar with winzip all of the com.google.common.* package folder structure is there but there are no .class files in the package folders nor subfolders. Just mentioning that in case it lends a clue. Perhaps it is somewhere else also that I had missed.

Thanks!

Edit:

my source sets now look like this

sourceSets {
    main {
        java.srcDirs = ['src/main/java']
     }
}

and dependencies

dependencies {
    compile 'com.android.support:appcompat-v7:22.1.0'
    compile 'com.android.support:support-v4:22.1.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}

I still get can't resolve android.support.test in my test class (test is colored red on these lines):

import android.support.test.InstrumentationRegistry;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

shouldn't I see a testing-support, espresso-core and espresso-contrib in my External libraries? External Libraries

Community
  • 1
  • 1
user2624395
  • 432
  • 1
  • 5
  • 10
  • @Jared Burrows, @Makoto not sure this is a duplicate as already posted in my original question that the .gradle setup suggested in the accepted answer of the question that is indicated I duplicated (which was chronologically asked after I asked my question) didn't work due to not compiling with `androidTestCompile`. Can you provide more detail on what the similarities are? – user2624395 Apr 26 '15 at 16:41
  • Google has examples: https://github.com/googlesamples/android-testing/tree/master/espresso. – Jared Burrows Apr 26 '15 at 17:42
  • answered here: http://stackoverflow.com/questions/29857695/android-tests-build-error-multiple-dex-files-define-landroid-support-test-build for my build, the 5/30/2015 version of the answer did the trick – user2624395 Jun 05 '15 at 02:34

1 Answers1

6

Below is my working Espresso gradle setup based on Android Studio 1.1.0 + Espresso 2.0 + Support Library v11 + gradle environment. It looks likely that you missed androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0' library.

dependencies {
    // ...
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}

android {
    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 21
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    sourceSets {
        packagingOptions {
            //...
            exclude 'META-INF/LICENSE.txt'
            exclude 'LICENSE.txt'
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['res']
        }
    }
}

About the directory, yes. just maintain androidTest and main under src folder. But, your setting does not make IDE to recognize androidTest folder as a test project folder. For that, two projects with same package name causes multi dex error I think. Use androidTestCompile to espresso-related lines in gradle dependencies and clean/rebuild project.

Note that you have to set all referenced projects gradle to same testInstrumentationRunner.

I also spent times to make it work. I wrote Korean tutorial to setup and run Espresso. Sorry about the language but screenshots might be helpful or you can try Google translate to read it.

EDIT. APPENDED BELOW

I thought why your IDE did not show green color in androidTest folder (means Test Project), and found your gradle setup seems to be wrong.

The line { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } } make IDE to recognize androidTest is a Project folder not Test Project folder. remove second one. androidTest will be automatically recognized as a Test Project.

Youngjae
  • 24,352
  • 18
  • 113
  • 198
  • thanks for the answer, I added 'com.android.support.test.espresso:espresso-contrib:2.0' but still androidTestCompile causes the same issue in my original question from a week ago – user2624395 Apr 26 '15 at 01:51
  • @user2624395 // have you ever click "Sync project with gradle files" button? – Youngjae Apr 26 '15 at 02:38
  • @user2624395 // I editted my post. check my editted gradle file `sourceSets` and appended description sentence at the last. – Youngjae Apr 26 '15 at 02:46
  • thank you for getting me further, it now launches into the emulator. However, with `androidTestCompile` I'm back to my original problem where package android.support.test results in "Cannot resolve symbol 'test' " and the `.test.` is highlighted in red when I rebuild the project. So when it runs in the emulator the test is not executed due to compile errors. When I include the adroidTest in the src.dirs. and `compile` all libraries the build succeeds able to result android.support.test package. So I'm still wondering if it is because com.google.common is empty espresso library. – user2624395 Apr 26 '15 at 16:35
  • actually it wasn't running the emulator. somehow I was in the wrong run config when it launched to the emulator. it can't launch the test run config because android.support.test is not resolved and the runner is in that library package. I think I'm missing a dependency that android.support.test needs – user2624395 Apr 26 '15 at 16:56
  • @user2624395 // [The setup document](https://code.google.com/p/android-test-kit/wiki/EspressoSetupInstructions) introduces `androidTestCompile 'com.android.support.test:runner:0.2'` but you do not have it. Did you try to add it? – Youngjae Apr 26 '15 at 17:21
  • yes, I tried `androidTestCompile 'com.android.support.test:runner:0.2'` even though the question mine was indicated as duplicate said to remove that, I tried it anyway with the same results, did you see edits I added to the question a few minutes ago from follow up on your suggestions? Thanks! – user2624395 Apr 26 '15 at 17:26
  • I think differences between you and I are; (1) I set it up in one of referenced library. it use `apply plugin: 'com.android.library'`. (2) I use gradle 1.0.1 and Espresso **2.0**. – Youngjae Apr 26 '15 at 17:39
  • It's definitely my project, the samples from github execute successfully so that rules out my Android Studio environment. Though not yet able to discern what is missing from my project. Hypothesis: something to do with the conversion from Eclipse to Android Studio. Thinking I will create a new empty project and manually move my code into it to test that hypothesis. Thanks for your help. – user2624395 Apr 28 '15 at 01:53