简体   繁体   English

使用gradle构建系统创建测试Android apk

[英]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. 我正在将我的android项目迁移到gradle构建系统,我无法从我的Integration Test Android项目中导入我的Android项目。

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. 我正在使用具有多个android-libraries的多项目配置,并且它工作得很好,但是我在使用多项目设置设置我的测试项目时遇到了问题。 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: settings.gradle:

include ':MyApp', 'Instrumentation-Tests'

MyAppp/build.gradle: 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 : 最后我的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 . 当我运行gradle compileDebug ,项目MyApp被正确编译(及其所有模块)但是Instrumentation-Tests编译失败,因为它无法找到MyApp定义的android类。

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. 我想将MyApp项目依赖项的输出包含在Instrumentation-Tests编译的类路径中,但我不知道是否可以使用android gradle插件。

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 . 因此对于compile project(':MyApp') MyApp应该是一个带有apply plugin: 'android-library'的Android库项目apply plugin: 'android-library'在它的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. 编辑:考虑使用Gradle进行测试,请使用Ant。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM