Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create testing Android apk using gradle build system

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.

like image 980
David Avatar asked Aug 20 '13 08:08

David


People also ask

How do I execute a Gradle build in Android Studio?

You can execute all the build tasks available to your Android project using the Gradle wrapper command line tool. It's available as a batch file for Windows ( gradlew.bat) and a shell script for Linux and Mac ( gradlew.sh ), and it's accessible from the root of each project you create with Android Studio.

What are the dependencies in Android Gradle?

dependencies – This specify the dependencies that are needed to build the project. Both the top-level and module-level build.gradle files are the main script files for automating the tasks in an android project and are used by the Gradle for generating the APK from the source files.

What is the use of Gradle?

Gradle is a build system (open source) which is used to automate building, testing, deployment etc. “Build.gradle” are scripts where one can automate the tasks. For example, the simple task to copy some files from one directory to another can be performed by Gradle build script before the actual build process happens. Why is Gradle needed?

How do I build an Android app from the command line?

Build your app from the command line. You can execute all the build tasks available to your Android project using the Gradle wrapper command line tool. It's available as a batch file for Windows (gradlew.bat) and a shell script for Linux and Mac (gradlew.sh), and it's accessible from the root of each project you create with Android Studio.


1 Answers

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.

like image 122
yanchenko Avatar answered Sep 20 '22 21:09

yanchenko