Gradle应用程序变量mergeAssets不起作用

时间:2014-07-13 02:52:32

标签: android gradle android-studio android-gradle build.gradle

我正在尝试使用gradle applicationVariants mergeAssets任务从我的apk中删除一些未使用的资源文件。

即使我能够打印“此行打印”这一行,但当应用程序变体“运行”时,似乎似乎没有调用variant.mergeAssets.doFirst {},我从未在gradle控制台中看到输出“此行未打印”。我试图删除的资产也没有删除,但似乎删除它的代码永远不会执行。

我做错了吗?我已尝试将整个android.applicationVariants任务移到buildTypes之外,我已尝试将其嵌套在release{}中。我尝试过的任何内容似乎都无法正常工作。

buildTypes {
    release {
        runProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), file('proguard-project.txt')
        signingConfig signingConfigs.release
    }

    publicBeta.initWith(buildTypes.release)
    publicBeta {
        runProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), file('proguard-project.txt')
    }

    android.applicationVariants.all { variant ->
        println 'this line is printed'
        variant.mergeAssets.doFirst {
            println 'this line is never printed'
            File fonts = file("${rootDir}/build/intermediates/exploded-aar/com.github.johnkil.android-robototextview/robototextview/2.0.1/assets/fonts")
            if (fonts.exists()) {
                for (File file : fonts.listFiles()) {
                    if (file.getName().contains("RobotoSlab")) {
                        println("delete " + file.getName() + " font")
                        file.delete()
                    };
                }
            }
        }
    }
}

以下是gradle控制台输出,为简洁起见:

Executing tasks: [:shuttle_paid:assembleRelease]

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
this line is printed
this line is printed
this line is printed
:shuttle_library:compileLint
:shuttle_library:copyReleaseLint UP-TO-DATE
:shuttle_library:preBuild
:shuttle_library:preReleaseBuild
:shuttle_library:checkReleaseManifest
:shuttle_library:preDebugBuild
:shuttle_library:preDebugTestBuild
:libraries:castcompanionlibrary-android:compileLint
:libraries:castcompanionlibrary-android:copyReleaseLint UP-TO-DATE
:libraries:castcompanionlibrary-android:preBuild
:libraries:castcompanionlibrary-android:preReleaseBuild
:libraries:castcompanionlibrary-android:checkReleaseManifest
:libraries:castcompanionlibrary-android:preDebugBuild
:libraries:castcompanionlibrary-android:preDebugTestBuild
:libraries:castcompanionlibrary-android:prepareComAndroidSupportAppcompatV72000Library UP-TO-DATE
:libraries:castcompanionlibrary-android:prepareComAndroidSupportMediarouterV72000Library UP-TO-DATE
:libraries:castcompanionlibrary-android:prepareComAndroidSupportSupportV42000Library UP-TO-DATE
:libraries:castcompanionlibrary-android:prepareComGoogleAndroidGmsPlayServices5077Library UP-TO-DATE
:libraries:castcompanionlibrary-android:prepareReleaseDependencies
:libraries:castcompanionlibrary-android:compileReleaseAidl UP-TO-DATE
:libraries:castcompanionlibrary-android:compileReleaseRenderscript UP-TO-DATE
:libraries:castcompanionlibrary-android:generateReleaseBuildConfig UP-TO-DATE
:libraries:castcompanionlibrary-android:generateReleaseAssets UP-TO-DATE
:libraries:castcompanionlibrary-android:mergeReleaseAssets UP-TO-DATE
:libraries:castcompanionlibrary-android:generateReleaseResValues UP-TO-DATE
:libraries:castcompanionlibrary-android:generateReleaseResources UP-TO-DATE
:libraries:castcompanionlibrary-android:mergeReleaseResources UP-TO-DATE
:libraries:castcompanionlibrary-android:processReleaseManifest UP-TO-DATE
:libraries:castcompanionlibrary-android:processReleaseResources UP-TO-DATE
:libraries:castcompanionlibrary-android:generateReleaseSources UP-TO-DATE
:libraries:castcompanionlibrary-android:compileReleaseJava UP-TO-DATE
:libraries:castcompanionlibrary-android:extractReleaseAnnotations UP-TO-DATE
:libraries:castcompanionlibrary-android:mergeReleaseProguardFiles UP-TO-DATE
:libraries:castcompanionlibrary-android:processReleaseJavaRes UP-TO-DATE
:libraries:castcompanionlibrary-android:packageReleaseJar UP-TO-DATE
:libraries:castcompanionlibrary-android:compileReleaseNdk UP-TO-DATE
:libraries:castcompanionlibrary-android:packageReleaseJniLibs UP-TO-DATE
:libraries:castcompanionlibrary-android:packageReleaseLocalJar UP-TO-DATE
:libraries:castcompanionlibrary-android:packageReleaseRenderscript UP-TO-DATE
:libraries:castcompanionlibrary-android:packageReleaseResources UP-TO-DATE
:libraries:castcompanionlibrary-android:bundleRelease UP-TO-DATE
:shuttle_library:prepareComAndroidSupportAppcompatV72000Library UP-TO-DATE
:shuttle_library:prepareComAndroidSupportMediarouterV72000Library UP-TO-DATE
:shuttle_library:prepareComAndroidSupportSupportV42000Library UP-TO-DATE
:shuttle_library:prepareComGithubJohnkilAndroidRobototextviewRobototextview210Library UP-TO-DATE
:shuttle_library:prepareComGoogleAndroidGmsPlayServices5077Library UP-TO-DATE
:shuttle_library:prepareComLarswerkmanHoloColorPicker14Library UP-TO-DATE
:shuttle_library:prepareShuttleLibrariesCastcompanionlibraryAndroidUnspecifiedLibrary UP-TO-DATE
:shuttle_library:prepareReleaseDependencies
:shuttle_library:compileReleaseAidl UP-TO-DATE
:shuttle_library:compileReleaseRenderscript UP-TO-DATE
:shuttle_library:generateReleaseBuildConfig UP-TO-DATE
:shuttle_library:generateReleaseAssets UP-TO-DATE
:shuttle_library:mergeReleaseAssets UP-TO-DATE
:shuttle_library:generateReleaseResValues UP-TO-DATE
:shuttle_library:generateReleaseResources UP-TO-DATE
:shuttle_library:mergeReleaseResources UP-TO-DATE
:shuttle_library:processReleaseManifest UP-TO-DATE
:shuttle_library:processReleaseResources UP-TO-DATE
:shuttle_library:generateReleaseSources UP-TO-DATE
:shuttle_library:compileReleaseJava UP-TO-DATE
:shuttle_library:extractReleaseAnnotations UP-TO-DATE
:shuttle_library:mergeReleaseProguardFiles UP-TO-DATE
:shuttle_library:processReleaseJavaRes UP-TO-DATE
:shuttle_library:packageReleaseJar UP-TO-DATE
:shuttle_library:compileReleaseNdk UP-TO-DATE
:shuttle_library:packageReleaseJniLibs UP-TO-DATE
:shuttle_library:packageReleaseLocalJar UP-TO-DATE
:shuttle_library:packageReleaseRenderscript UP-TO-DATE
:shuttle_library:packageReleaseResources UP-TO-DATE
:shuttle_library:bundleRelease UP-TO-DATE
:shuttle_paid:preBuild
:shuttle_paid:preReleaseBuild
:shuttle_paid:checkReleaseManifest
:shuttle_paid:preDebugBuild
:shuttle_paid:prePublicBetaBuild
:shuttle_paid:prepareComAndroidSupportAppcompatV72000Library UP-TO-DATE
:shuttle_paid:prepareComAndroidSupportMediarouterV72000Library UP-TO-DATE
:shuttle_paid:prepareComAndroidSupportSupportV42000Library UP-TO-DATE
:shuttle_paid:prepareComGithubJohnkilAndroidRobototextviewRobototextview210Library UP-TO-DATE
:shuttle_paid:prepareComGoogleAndroidGmsPlayServices5077Library UP-TO-DATE
:shuttle_paid:prepareComLarswerkmanHoloColorPicker14Library UP-TO-DATE
:shuttle_paid:prepareShuttleLibrariesCastcompanionlibraryAndroidUnspecifiedLibrary UP-TO-DATE
:shuttle_paid:prepareShuttleShuttle_libraryUnspecifiedLibrary UP-TO-DATE
:shuttle_paid:prepareReleaseDependencies
:shuttle_paid:compileReleaseAidl UP-TO-DATE
:shuttle_paid:compileReleaseRenderscript UP-TO-DATE
:shuttle_paid:generateReleaseBuildConfig UP-TO-DATE
:shuttle_paid:generateReleaseAssets UP-TO-DATE
:shuttle_paid:mergeReleaseAssets UP-TO-DATE
:shuttle_paid:generateReleaseResValues UP-TO-DATE
:shuttle_paid:generateReleaseResources UP-TO-DATE
:shuttle_paid:mergeReleaseResources UP-TO-DATE
:shuttle_paid:processReleaseManifest UP-TO-DATE
:shuttle_paid:processReleaseResources UP-TO-DATE
:shuttle_paid:generateReleaseSources UP-TO-DATE
:shuttle_paid:compileReleaseJava UP-TO-DATE
:shuttle_paid:lintVitalRelease
:shuttle_paid:compileReleaseNdk UP-TO-DATE
:shuttle_paid:proguardRelease UP-TO-DATE
:shuttle_paid:dexRelease UP-TO-DATE
:shuttle_paid:processReleaseJavaRes UP-TO-DATE
:shuttle_paid:validateReleaseSigning
:shuttle_paid:packageRelease UP-TO-DATE
:shuttle_paid:zipalignRelease UP-TO-DATE
:shuttle_paid:assembleRelease

BUILD SUCCESSFUL

Total time: 8.682 secs

1 个答案:

答案 0 :(得分:1)

将始终打印打印的行,因为它在配置阶段执行。 (见Gradle User Guide

您的gradle控制台输出显示所有合并*资源任务被跳过为UP-TO-DATE。这意味着自上次执行任务以来输入和输出没有改变,因此gradle不再执行它们。不幸的是,Gradle还不够聪明,知道你已经改变了任务实现,并且它不应该跳过任务。

尝试运行清理以强制运行任务