1

我们知道Android Gradle Plugin 3.0修改了Api https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration,我们需要修改apk名称,代码如下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"
    }
}

我们团队用java写了一个gradle插件,因为autocomplete和smart hints,但是我在升级gradle插件的Android Gradle插件到3.1.2的时候出现了问题,而且我不知道如何将上面的代码翻译成java,我试过了以下方法:

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));
    })
);

但这似乎不起作用。有谁知道如何解决它?

4

0 回答 0