1

I have the following Gradle configuration in my Android Project:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
    }
}
apply plugin: 'com.android.application'

dependencies {
    repositories {
        jcenter()
        google()
    }
    compile fileTree(include: '*.jar', dir: 'libs')
    compile 'com.android.support:support-v4:25.4.0'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'

}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    buildTypes {
        debug {
            debuggable true
            applicationIdSuffix ".debug"
        }
    }

    defaultConfig {
        jackOptions {
            enabled true
        }
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

I want to start making UI tests so I recorded an espresso test via the Android Studio recording functionality. Afterwards, when saving the test class file, i noticed some of the imports were no resolved.

Error:(17, 8) The import org.junit cannot be resolved
Error:(19, 15) The import android.support.test cannot be resolved

As you can see, I add the junit dependency directly, so why doesn't android studio or gradle see the junit library?

This is my dep tree:

androidTestCompile - Classpath for compiling the androidTest sources.
+--- junit:junit:4.12
|    \--- org.hamcrest:hamcrest-core:1.3
\--- com.android.support.test.espresso:espresso-core:3.0.0
     +--- com.android.support.test:runner:1.0.0
     |    +--- com.android.support:support-annotations:25.4.0
     |    +--- junit:junit:4.12 (*)
     |    \--- net.sf.kxml:kxml2:2.3.0
     +--- com.android.support.test:rules:1.0.0
     |    \--- com.android.support.test:runner:1.0.0 (*)
     +--- com.android.support.test.espresso:espresso-idling-resource:3.0.0
     +--- com.squareup:javawriter:2.1.1
     +--- javax.inject:javax.inject:1
     +--- org.hamcrest:hamcrest-library:1.3
     |    \--- org.hamcrest:hamcrest-core:1.3
     +--- org.hamcrest:hamcrest-integration:1.3
     |    \--- org.hamcrest:hamcrest-library:1.3 (*)
     \--- com.google.code.findbugs:jsr305:2.0.1

And my source folder is organized like this:

└── src
    ├── androidTest
    │   └── java
    │       └── pt
    │           └── joaomneto
    │               └── titancompanion
    │                   └── (expresso tests source code)
    └── pt
        └── joaomneto
            └── titancompanion
                └── (app source code)

Thanks for your help

Joao Neto
  • 310
  • 3
  • 18
  • Refer to this question to find out actual dependencies tree: https://stackoverflow.com/questions/21645071/using-gradle-to-find-dependency-tree – Ivan Pronin Jul 29 '17 at 19:47
  • @IvanPronin already checked that I had junit 4.12 in my dep tree. I had even forced the dependency on build.gradle `androidTestCompile 'junit:junit:4.12'`. – Joao Neto Jul 29 '17 at 21:03
  • What is `instrumentTest.setRoot('tests')` and what happens, when you remove it? – R. Zagórski Jul 30 '17 at 14:16
  • @R.Zagórski I don't know actually. Something I copy pasted. I'm a maven guy so I'm not fully versed on gradle. It's almost certainly something I copy pasted from somwhere else. I've removed the line but I still have the same problem. `error: package org.junit does not exist` – Joao Neto Jul 30 '17 at 20:39

1 Answers1

1

As I found out, the problem wasn't with the dependency tree but with the folder structure I was using. After changing the folder structure to a more standard src/main/java and src/androidTest/java and changing the gradle.build accordingly

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src/main/java']
        resources.srcDirs = ['src/main/java']
        aidl.srcDirs = ['src/main/java']
        renderscript.srcDirs = ['src/main/java']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }

    androidTest.java.srcDir 'src/androidTest/java'
    debug.setRoot('build-types/debug')
    release.setRoot('build-types/release')
}

Android studio was now able to see the tests files in the proper location and apply the good dependency tree scope to them.

Joao Neto
  • 310
  • 3
  • 18