根据android内置类型运行特定的gradle任务

时间:2015-11-18 20:59:29

标签: android gradle

我正在尝试根据构建类型运行特定任务/操作 - 调试或释放。

android {
    applicationVariants.all { variant ->
        println "buildType.name is $buildType.name"
    }
}

但无论我运行assembleDebug还是assembleRelease,我的代码都会针对两种构建类型执行:

10:56:20 PM: Executing external task 'assembleDebug'...
Configuration on demand is an incubating feature.
buildType.name is debug
buildType.name is release
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:curl:compileLint
:curl:copyReleaseLint UP-TO-DATE
:curl:mergeReleaseProguardFiles UP-TO-DATE
:curl:preBuild UP-TO-DATE
:curl:preReleaseBuild UP-TO-DATE
:curl:checkReleaseManifest
:curl:prepareReleaseDependencies
:curl:compileReleaseAidl UP-TO-DATE
:curl:compileReleaseRenderscript UP-TO-DATE
:curl:generateReleaseBuildConfig UP-TO-DATE
:curl:generateReleaseAssets UP-TO-DATE
:curl:mergeReleaseAssets UP-TO-DATE
:curl:generateReleaseResValues UP-TO-DATE
:curl:generateReleaseResources UP-TO-DATE
:curl:packageReleaseResources UP-TO-DATE
:curl:processReleaseManifest UP-TO-DATE
:curl:processReleaseResources UP-TO-DATE
:curl:generateReleaseSources UP-TO-DATE
:curl:processReleaseJavaRes UP-TO-DATE
:curl:compileReleaseJavaWithJavac UP-TO-DATE
:curl:packageReleaseJar UP-TO-DATE
:curl:compileReleaseNdk UP-TO-DATE
:curl:packageReleaseJniLibs UP-TO-DATE
:curl:packageReleaseLocalJar UP-TO-DATE
:curl:packageReleaseRenderscript UP-TO-DATE
:curl:bundleRelease UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2301Library UP-TO-DATE
:app:prepareComAndroidSupportMultidex101Library UP-TO-DATE
:app:prepareComAndroidSupportPaletteV72301Library UP-TO-DATE
:app:prepareComAndroidSupportPercent2301Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
:app:prepareComAppboyAndroidSdkUi182Library UP-TO-DATE
:app:prepareComBraintreepaymentsApiBraintree164Library UP-TO-DATE
:app:prepareComBraintreepaymentsCardForm201Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidAnswers131Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidBeta113Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidCrashlytics251Library UP-TO-DATE
:app:prepareComCrashlyticsSdkAndroidCrashlyticsCore234Library UP-TO-DATE
:app:prepareComEdmodoCropper101Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAnalytics780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBase780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesLocation780Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesMaps780Library UP-TO-DATE
:app:prepareComJakewhartonTimberTimber410Library UP-TO-DATE
:app:prepareComRaizlabsAndroidDBFlow221Library UP-TO-DATE
:app:prepareIoBranchSdkAndroidLibrary1811Library UP-TO-DATE
:app:prepareIoFabricSdkAndroidFabric135Library UP-TO-DATE
:app:prepareParaboCurlUnspecifiedLibrary UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:fabricGenerateResourcesDebug
:app:jrebelPinDebugResids
:app:processDebugResources
:app:generateDebugSources
:app:processDebugJavaRes UP-TO-DATE
:curl:copyDebugLint UP-TO-DATE
:curl:mergeDebugProguardFiles UP-TO-DATE
:curl:preDebugBuild UP-TO-DATE
:curl:checkDebugManifest
:curl:prepareDebugDependencies
:curl:compileDebugAidl UP-TO-DATE
:curl:compileDebugRenderscript UP-TO-DATE
:curl:generateDebugBuildConfig UP-TO-DATE
:curl:generateDebugAssets UP-TO-DATE
:curl:mergeDebugAssets UP-TO-DATE
:curl:generateDebugResValues UP-TO-DATE
:curl:generateDebugResources UP-TO-DATE
:curl:packageDebugResources UP-TO-DATE
:curl:processDebugManifest UP-TO-DATE
:curl:processDebugResources UP-TO-DATE
:curl:generateDebugSources UP-TO-DATE
:curl:processDebugJavaRes UP-TO-DATE
:curl:compileDebugJavaWithJavac UP-TO-DATE
:curl:packageDebugJar UP-TO-DATE
:curl:compileDebugNdk UP-TO-DATE
:curl:packageDebugJniLibs UP-TO-DATE
:curl:packageDebugLocalJar UP-TO-DATE
:curl:packageDebugRenderscript UP-TO-DATE
:curl:bundleDebug UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:generateDebugJrebelLayout
:app:collectDebugMultiDexComponents UP-TO-DATE
:app:packageAllDebugClassesForMultiDex UP-TO-DATE
:app:shrinkDebugMultiDexComponents UP-TO-DATE
:app:createDebugMainDexClassList UP-TO-DATE
:app:dexDebug UP-TO-DATE
:app:jrebelPostProcessDebugResids
:app:validateDebugSigning
:app:packageDebug
:app:zipalignDebug
:app:assembleDebug
:curl:compileDebugSources UP-TO-DATE
:curl:assembleDebug UP-TO-DATE

BUILD SUCCESSFUL

Total time: 10.657 secs
10:56:31 PM: External task execution finished 'assembleDebug'.

为什么会这样?

检测当前构建类型的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以让构建依赖于不同构建的任务

buildTypes {
    release {
        build.dependsOn 'mySweetReleaseTask'
    }
    debug {
        build.dependsOn 'mySweetDebugTask'
    }
}

或者

buildTypes {
    release {
        project.getTasks().getByName('build').dependsOn('mySweetReleaseTask')
    }
    debug {
        project.getTasks().getByName('build').dependsOn('mySweetDebugTask')
    }
}

如果您希望您的任务在测试期间的特定时间运行,那么您可以将'build'替换为'check'。在构建实际开始(执行阶段)之前的配置阶段评估此配置块。