Gradle Android插件:适用于所有产品风格的后编译任务

时间:2015-01-30 11:45:59

标签: android gradle android-gradle

我需要执行我自己的代码(通过javaexec),在我的android项目已经由gradle编译之后但是在它被打包到apk之前(事实上,在资源被移动到它们的最终目的地之前)。所以我用过:

gradlew tasks --all

获取可用任务列表。我使用的是产品口味,所以几乎所有的任务都有一个介于两者之间的味道的名称,如:

  

assembleFlavorA

  

installFlavorB

等...

我现在可以做的是,在编译开始之前通过挂钩到preBuild任务来执行我自己的任务:

preBuild <<{
    //Do some stuff
}

上面的每个构建变体都会被调用,这正是我想要的。 但是,当我尝试使用汇编任务或构建任务时:

assemble <<{
    //Get's never executed
}

build <<{
    //Get's never executed
}

无论我正在构建哪种产品风格,上述代码都不会执行。查看releaseFlavorA的依赖项列表:

myapp:assembleFlavorARelease - Assembles the Release build for flavor FlavorA [library:bundleRelease]
    myapp:checkFlavorAReleaseManifest
    myapp:compileFlavorAReleaseAidl
    myapp:compileFlavorAReleaseJava
    myapp:compileFlavorAReleaseNdk
    myapp:compileFlavorAReleaseRenderscript
    myapp:dexFlavorARelease
    myapp:generateFlavorAReleaseAssets
    myapp:generateFlavorAReleaseBuildConfig
    myapp:generateFlavorAReleaseResValues
    myapp:generateFlavorAReleaseResources
    myapp:generateFlavorAReleaseSources
    myapp:lintVitalFlavorARelease - Runs lint on just the fatal issues in the FlavorARelease build
    myapp:mergeFlavorAReleaseAssets
    myapp:mergeFlavorAReleaseResources
    myapp:packageFlavorARelease
    myapp:preFlavorADebugBuild
    myapp:preFlavorAReleaseBuild
    myapp:preAltdorfDebugBuild
    myapp:preAltdorfReleaseBuild
    myapp:preBerlinDebugBuild
    myapp:preBerlinReleaseBuild
    myapp:preBuild
    myapp:prepareFlavorAReleaseDependencies
    myapp:prepareComAndroidSupportAppcompatV71910Library - Prepare com.android.support:appcompat-v7:19.1.0
    myapp:prepareTrunkGradleLibraryUnspecifiedLibrary - Prepare trunk-gradle:library:unspecified
    myapp:processFlavorAReleaseJavaRes
    myapp:processFlavorAReleaseManifest
    myapp:processFlavorAReleaseResources
    myapp:validateReleaseSigning
    myapp:zipalignFlavorARelease

我只看到preBuild但是既没有汇编也没有构建,这很奇怪,因为它在运行时显示

gradlew tasks 

但是上面列表中的大多数方法都是特定于风味的,我不想拥有20次相同的任务,因为我有20种不同的风格......那么,一旦编译完成,我怎样才能执行必要的任务,但APK还没有包装好所有口味?类似的东西:

//I know there is no task called "postCompile" - so anything post compiling and pre-packaging would be fine
postCompile << {
    //Do something that needs to be done for all flavors
}

修改 所以我在构建customerA时重新检查了命令行输出,例如:

  

gradlew assembleCustomerARelease

    C:\Users\user\workspace\android\trunk-gradle>gradlew assembleCustomerARelease
:library:compileLint
:library:copyReleaseLint UP-TO-DATE
:library:mergeReleaseProguardFiles UP-TO-DATE
:library:preBuild
:library:preReleaseBuild
:library:checkReleaseManifest
:library:preDebugBuild
:library:preDebugTestBuild
:library:prepareComAndroidSupportAppcompatV71910Library UP-TO-DATE
:library:prepareReleaseDependencies
:library:compileReleaseAidl UP-TO-DATE
:library:compileReleaseRenderscript UP-TO-DATE
:library:generateReleaseBuildConfig UP-TO-DATE
:library:generateReleaseAssets UP-TO-DATE
:library:mergeReleaseAssets UP-TO-DATE
:library:generateReleaseResValues UP-TO-DATE
:library:generateReleaseResources UP-TO-DATE
:library:mergeReleaseResources UP-TO-DATE
:library:processReleaseManifest UP-TO-DATE
:library:processReleaseResources UP-TO-DATE
:library:generateReleaseSources UP-TO-DATE
:library:compileReleaseJava UP-TO-DATE
:library:processReleaseJavaRes UP-TO-DATE
:library:packageReleaseJar UP-TO-DATE
:library:compileReleaseNdk UP-TO-DATE
:library:packageReleaseJniLibs UP-TO-DATE
:library:packageReleaseLocalJar UP-TO-DATE
:library:packageReleaseRenderscript UP-TO-DATE
:library:packageReleaseResources UP-TO-DATE
:library:bundleRelease UP-TO-DATE
:myapp:preBuild
Path to customer file: C:\Users\user\workspace\android\trunk-gradle\myapp\src\CustomerA\res\xml\customer.xml
Selected server: release
:myapp:preCustomerAReleaseBuild
:myapp:checkCustomerAReleaseManifest
:myapp:preCustomerADebugBuild
:myapp:preCustomerBDebugBuild
:myapp:preCustomerBReleaseBuild
:myapp:preCustomerCDebugBuild
:myapp:preCustomerCReleaseBuild
:myapp:prepareComAndroidSupportAppcompatV71910Library UP-TO-DATE
:myapp:prepareTrunkGradleLibraryUnspecifiedLibrary UP-TO-DATE
:myapp:prepareCustomerAReleaseDependencies
:myapp:compileCustomerAReleaseAidl UP-TO-DATE
:myapp:compileCustomerAReleaseRenderscript UP-TO-DATE
:myapp:generateCustomerAReleaseBuildConfig UP-TO-DATE
:myapp:generateCustomerAReleaseAssets UP-TO-DATE
:myapp:mergeCustomerAReleaseAssets UP-TO-DATE
:myapp:generateCustomerAReleaseResValues UP-TO-DATE
:myapp:generateCustomerAReleaseResources UP-TO-DATE
:myapp:mergeCustomerAReleaseResources UP-TO-DATE
:myapp:processCustomerAReleaseManifest UP-TO-DATE
:myapp:processCustomerAReleaseResources UP-TO-DATE
:myapp:generateCustomerAReleaseSources UP-TO-DATE
:myapp:compileCustomerAReleaseJava UP-TO-DATE
:myapp:lintVitalCustomerARelease
//Some logoutput from the dex-ing task not really relevant in this case
:myapp:dexCustomerARelease UP-TO-DATE
:myapp:processCustomerAReleaseJavaRes UP-TO-DATE
:myapp:validateReleaseSigning
:myapp:packageCustomerARelease UP-TO-DATE
:myapp:zipalignCustomerARelease UP-TO-DATE
:myapp:assembleCustomerARelease

BUILD SUCCESSFUL

Total time: 25.451 secs

基于上面的构建输出,我假设preBuild实际上发生在编译阶段之后?因为它在compileReleaseJava和其他编译任务之后列出。如果是这种情况,那么“preBuild”就足以满足我的要求。

更新04.02.2015

问题仍然存在,但我已将代码更改为不再依赖它。我没有尝试使用必须首先由我的项目编译的java类,而是为该类创建了另一个项目,该项目生成了一个jar文件,而我认为使用了 - 详情可以在这里找到:

https://stackoverflow.com/a/28303047/1041533

但是 - 我认为知道这个问题的答案可能仍然很有趣。

1 个答案:

答案 0 :(得分:10)

您不能扩展这样的汇编和构建任务,因为它们在Gradle执行阶段之前被相应的构建变量相关任务替换。例如:assemble成为assembleRelease。

但是,当构建图形放在一起时,您仍然可以挂钩构建过程。如果要在包任务之前执行自己的代码,可以使用以下代码段:

task doStuff << {
    // Do stuff
}

tasks.whenTaskAdded { theTask ->
    if (theTask.name.contains('package')) {
        theTask.dependsOn 'doStuff'
    }
}

此代码不会区分构建变体;如果需要,你可以在if条件下这样做。