stanfy / spoon-gradle-plugin

Gradle plugin for Spoon.
Apache License 2.0
353 stars 79 forks source link

custom output file name not properly supported #138

Closed OleksandrKucherenko closed 7 years ago

OleksandrKucherenko commented 7 years ago

The issue and workaround in one place:

    /* Compose binary name for our builds. */
    applicationVariants.all { variant ->
        def buildType = variant.buildType.name
        def suffix = (null == variant.buildType.applicationIdSuffix) ? '' : variant.buildType.applicationIdSuffix
        def flavorName = (0 == variant.productFlavors.size()) ? '' : variant.productFlavors[0].name
        def appId = (0 == variant.productFlavors.size()) ? variant.mergedFlavor.applicationId : variant.productFlavors[0].applicationId

        /* Compose custom output file name that contains version number in final filename */
        variant.outputs.each { output ->
            // modify file name
            def file = output.outputFile
            def newFilename = appId + suffix + "-" +
                    project.android.defaultConfig.versionName + "-r" +
                    project.android.defaultConfig.versionCode + "-" +
                    buildType + ".apk"
            output.outputFile = new File(file.parent, newFilename)

            rootProject.logger.info("  output: '" + newFilename + "'");

            // create a copy of APK with a default name for SPOON plugin
            def taskName = "copyApk${flavorName}${buildType.capitalize()}"
            project.task(taskName, type: Copy) {
                description = "Make copy of APK with a default name for making SPOON plugin happy"
                group = "workaround"

                from output.outputFile.absolutePath
                into "${file.parent}"
                rename '.*\\.apk', "${file.name}"
            }

            tasks.matching { it.name.equalsIgnoreCase("assemble${flavorName}${buildType}") }.each {
                it.finalizedBy taskName
            }
        }
    }

Explained: 1) first part of the gradle script force custom names of the APK file 2) second part declare a custom task that executed after "assembleDebug" and recover the "expected by Sppon plugin" apk file name.

What's wrong:

roman-mazur commented 7 years ago

The problem here is that although the plugin does use properties provided by assembleDebug.output, the value is used only once when spoon tasks are created. And it happens before your code that changes file names is executed. IMHO, it's a general problem with Gradle scripts and plugins, and, if memory serves, it should be resolved in upcoming Gradle releases.

You might try moving apply plugin:'spoon' instruction and place it below your own code. In such case spoon plugin should be using values that have already been changed. And probably you will be able to remove the workaround with that copy task.