如何修改apk名称在android gradle插件中使用java 3. +

时间:2018-05-30 11:13:11

标签: android android-studio gradle groovy android-gradle-3.0

我们知道Android Gradle Plugin 3.0修改了Api https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration,我们需要使用build.gradle中的以下代码修改apk名称:

// 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插件,因为自动完成和智能提示,但是当我将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));
    })
);

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

0 个答案:

没有答案