Create testing Android apk using gradle build system
Asked Answered
O

1

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.

Orchestrate answered 20/8, 2013 at 8:3 Comment(2)
Have you read up on stuff like this: #16586909 and this: tools.android.com/tech-docs/new-build-system/…? 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.Terpineol
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!Orchestrate
D
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.

Drambuie answered 15/9, 2013 at 10:55 Comment(1)
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!Orchestrate

© 2022 - 2024 — McMap. All rights reserved.