How to modify the apk name use java in android gradle plugin 3.+

127 Views Asked by At

We know that Android Gradle Plugin 3.0 modifies the Api https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration, we need to modify the apk name with the following code in build.gradle:

// If you use each() to iterate through the variant objects,
// you need to start using all(). That's because each() iterates
// through only the objects that already exist during configuration time—
// but those object don't exist at configuration time with the new model.
// However, all() adapts to the new model by picking up object as they are
// added during execution.
android.applicationVariants.all { variant ->
    variant.outputs.all {
    outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

Our team wrote a gradle plugin in java because autocomplete and smart hints, but I have a problem when I upgraded the gradle plugin's Android Gradle plugin to 3.1.2, And I don't know how to translate the code above to java, I tried the following method:

extension.getApplicationVariants().all(variant ->
    variant.getOutputs().all(output -> {
        String fileName = createOutputFilename(variant.getBuildType().getVersionNameSuffix());
        out.println("> setting output filename to " + fileName);

        File outputFile = output.getOutputFile();

        String outputFilePath = outputFile.getPath();
        outputFilePath = outputFilePath.substring(0, outputFilePath.lastIndexOf("/"));
        outputFilePath += "/" + fileName;
        out.println("> new outputFilePath - " + outputFilePath);

        outputFile.renameTo(new File(outputFilePath));
    })
);

But it doesn't seem to work. Does anyone know how to solve it?

0

There are 0 best solutions below