5

我有一个多维的 android gradle 项目,需要很长时间才能构建和测试。我有一个二维的flaver定义。第一个维度有 2 个项目值,第二个维度有 4 个环境定义,并且有 3 种构建类型。

这导致 2x4x3 = 24 个构建变体。我想以某种方式进行优化,即只构建一个构建变体,并且只有一个构建变体用于在 ci 环境中运行单元测试。

构建.gradle

android {
// more configurations
flavorDimensions "project", "environment"

productFlavors {
basic  {
    dimension "project"
}

advanced {
    dimension "project"
}

flavorDevelopment {
    dimension "environment"
    applicationId "ch.myproject.app.development"
}

flavorTest {
    dimension "environment"
    applicationId "ch.myproject.app.test"
}

flavorIntegration {
    dimension "environment"
    applicationId "ch.myproject.app.integration"
}

flavorProduction {
    dimension "environment"
    applicationId "ch.myproject.app.production"
}

buildTypes {
    debug {
        testCoverageEnabled = true
        minifyEnabled = false
        shrinkResources = false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
    }

    debugInstantRun.initWith(buildTypes.debug)
    debugInstantRun {
        // we are changing this build variant later on runtime, so that it will use constant values
        // for versionCode and versionName in the Manifest, for make sure the
        // manifest is unchanged between the instantRun builds

        // Specifies a sorted list of fallback build types that the
        // plugin should try to use when a dependency does not include a
        // "debugInstantRun" build type. You may specify as many fallbacks as you
        // like, and the plugin selects the first build type that's
        // available in the dependency.
        matchingFallbacks = ['debug']
    }

    release {
        // Currently all environments (dev/test/int/prod) are signed by the Production certificates
        minifyEnabled = false
        shrinkResources = false
    }
}
// more configurations
} // end of android

我以前会清理一切, gradlew clean --refresh-dependencies

然后只是组装调试变体, gradlew assembleDebug

然后我尝试在一个调试变体上只运行单元测试: gradlew testAdvancedFlavorDevelopmentDebugUnitTest -> 这不起作用

如果我运行gradlew test所有构建变体(除了发布构建类型),测试正在工作,但这需要很长时间!

也尝试过gradlew testDebugUnitTest->不起作用

我想我可以将单元测试移动到另一个目录而不是测试例如 testAdvancedFlavorDevelopment 然后当我只输入gradlew testtestAdvancedFlavorDevelopmentDebug 和 testAdvancedFlavorDevelopmentDebugInstantRun 的测试时将启动。

但是必须有一种方法可以让测试在测试目录中并通过 gradlew 命令强制编译和单元测试一个特定的构建变体!有任何想法吗?

蒂亚卢克

4

1 回答 1

1

尝试 ./gradlew : ModuleName :test VariantName DebugUnitTest,您还可以在 Gradle 选项卡中找到您可以执行的所有可能的 UnitTest 任务。

在此处输入图像描述

于 2019-04-25T18:04:04.527 回答