自定义Gradle任务没有在Android Studio下运行?

时间:2014-08-01 11:22:35

标签: android gradle android-studio android-gradle

tl; dr:应用程序的build.gradle中的自定义任务在从命令行构建时运行正常,但在Android Studio中构建时不运行。为什么呢?

详细说明:

我有一个Android Studio项目,我已经添加了一个运行外部脚本的自定义任务。当我使用gradle包装器从命令行构建时,一切正常,但是当我在Android Studio下构建脚本时,脚本没有运行。

以下是应用程序的build.gradle文件,其中包含自定义buildData任务:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "ie.wattless.wattapp"
        minSdkVersion 14
        targetSdkVersion 19
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    task buildData(type:Exec) {
        workingDir '.'
        commandLine './src/scripts/dummy.sh'
    }

    gradle.projectsEvaluated {
        preBuild.dependsOn(buildData)
    }
}

dependencies {
    // This seems to fix a build problem described here:
    // http://stackoverflow.com/questions/24438170/manifest-merger-failed-uses-sdkminsdkversion-14
    compile 'com.android.support:support-v4:20.0.0'
    // do not use dynamic updating.
    //compile 'com.android.support:support-v4:+'
}

我已经用这个替换了原始脚本,以保持简单:

#!/bin/bash

echo 'Dummy script running.'

touch /tmp/dummy

我可以使用gradle包装器进行命令行构建:

MBP2:app lorcan$ ls /tmp/dummy
ls: /tmp/dummy: No such file or directory
MBP2:app lorcan$ ../gradlew :app:assembleDebug
Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
:app:buildData
Dummy script running.
:app:preBuild
:app:compileDebugNdk
:app:preDebugBuild
:app:checkDebugManifest
:app:preReleaseBuild
:app:prepareComAndroidSupportSupportV42000Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:compileDebugJava
:app:preDexDebug
:app:dexDebug
:app:processDebugJavaRes UP-TO-DATE
:app:validateDebugSigning
:app:packageDebug
:app:zipalignDebug
:app:assembleDebug

BUILD SUCCESSFUL

Total time: 9.459 secs
MBP2:app lorcan$ ls /tmp/dummy
/tmp/dummy

所以,脚本运行了;您可以在输出开头附近看到行:app:buildDataDummy script running.,并且/tmp/dummy文件存在。

然后我可以用以下内容清理所有内容:

../gradlew clean && git clean -f -x && rm /tmp/dummy

然后我在项目上启动Android Studio,然后点击"运行"工具栏上的按钮。这是Gradle Console窗口中显示的输出:

Executing tasks: [:app:assembleDebug]

Configuration on demand is an incubating feature.
Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
:app:preBuild
:app:compileDebugNdk
:app:preDebugBuild
:app:checkDebugManifest
:app:preReleaseBuild
:app:prepareComAndroidSupportSupportV42000Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:compileDebugJava
:app:preDexDebug
:app:dexDebug
:app:processDebugJavaRes UP-TO-DATE
:app:validateDebugSigning
:app:packageDebug
:app:zipalignDebug
:app:assembleDebug

BUILD SUCCESSFUL

Total time: 10.089 secs

请注意缺少这些行:

:app:buildData
Dummy script running.

在构建命令行时出现。然后,/tmp/dummy文件不存在。显然,脚本没有运行。

任何想法为什么?

如果重要,我会使用最新版本的Android Studio(0.8.2),它在OS X 10.9.4上的MacBook Pro上运行。

这是我的第一个StackOverflow问题。建设性的批评欢迎。 : - )

提前谢谢。

0 个答案:

没有答案