7

I'm migrating my android project to gradle build system and I can't import my Android project from my Integration Test Android project.

I'm using multi-project configuration with several android-libraries and it's working great, but I'm having a problem setting up my testing project with multi-project settings. For external reasons I need to continue using this structure.

MyProject/
 | settings.gradle
 + MyApp/
    | build.gradle
    | src
    | res
    | libs
 + Instrumentation-Tests/
    | build.gradle
    | src
    | res
    | libs

my current configuration files look like:

settings.gradle:

include ':MyApp', 'Instrumentation-Tests'

MyAppp/build.gradle:

apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile files('.....jar')
    compile project('....')
    compile 'com.android.support:support-v4:13.0.+'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 16
    }

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

    }
}

And finally my Instrumentation-Tests/build.gradle:

apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile project(':MyApp')
    compile files('.....jar')
    compile 'com.android.support:support-v4:13.0.+'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 16
    }

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

When I run gradle compileDebug the project MyApp is compiled correctly (and all its modules) but the Instrumentation-Tests compilation fails because it can not find the android classes defined in MyApp.

I've read documentation and a lot of posts but I couldn't make it work, I do also tried using:

compile(project(':MyApp')) { transitive = true }

when declaring the dependency.

Has anybody had the same problem? I'd like to include the output of the MyApp project dependency into the classpath of Instrumentation-Tests compilation, but I don't know if that is possible using the android gradle plugin.

arserbin3
  • 6,010
  • 8
  • 36
  • 52
David
  • 496
  • 6
  • 8
  • Have you read up on stuff like this: http://stackoverflow.com/questions/16586409/how-can-i-create-tests-in-android-studio and this: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing? I wonder if you followed those instructions but changed the location of the sourcesets if it would work with your current setup. You probably need to specify the test source directories relative to the project root. – jDutton Sep 20 '13 at 21:05
  • I ended up moving all to the gradle structure, I do also moved all my instrumentation tests (unit and functional) inside the instrumentationTest folder and everything works! – David Oct 23 '13 at 18:18

1 Answers1

1

This won't work (as of now) because you can only specify library projects as dependencies.

So for the case of compile project(':MyApp') MyApp should have been an Android library project with apply plugin: 'android-library' in it's build.gradle. Which certainly doesn't make sense.

To have a separate test project, you need something else (which I'm researching myself).

EDIT: Given up on testing with Gradle, use Ant for that.

yanchenko
  • 56,576
  • 33
  • 147
  • 165
  • I ended up moving all to the gradle structure, I do also moved all my instrumentation tests (unit and functional) inside the instrumentationTest folder and everything works! – David Oct 23 '13 at 18:18