本文介绍了摇篮:applicationVariants.all跳过一个变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用摇篮编译我的Andr​​oid项目:

  buildTypes {
    推出 {
        signingConfig signingConfigs.release
        applicationVariants.all {变种 - >
            DEF文件= variant.outputFile
            DEF文件名= file.name
            文件名= fileName.replace(APK,-​​renamed.apk文件)
            variant.outputFile =新的文件(file.parent,文件名)
        }
    }
}
 

并不是所有输出文件重命名,它总是跳过1文件。为什么呢?

 的myapp-调试未对齐,renamed.apk< -renamed,OK!
的myapp-release.apk< -NOT改名,错!
MyApp的释放,未对齐,renamed.apk< -renamed,OK!
 

解决方案

我解决了使用这种code:

  buildTypes {
    推出 {
        signingConfig signingConfigs.release
    }

    applicationVariants.all {变种 - >
        高清APK = variant.packageApplication.outputFile;
        DEF了newName = apk.name.replace(-renamed.apkAPK);
        variant.packageApplication.outputFile =新的文件(apk.parentFile,了newName);
        如果(variant.zipAlign){
            variant.zipAlign.outputFile =新的文件(apk.parentFile,newName.replace( - 对齐,));
        }
    }
}
 

applicationVariants.all {...} 现在是发布外{...} 块。

我觉得 variant.zipAlign.outputFile 使其中的差别。

I'm using Gradle to compile my Android project:

buildTypes {
    release {
        signingConfig signingConfigs.release 
        applicationVariants.all { variant ->
            def file = variant.outputFile
            def fileName = file.name
            fileName = fileName.replace(".apk", "-renamed".apk")
            variant.outputFile = new File(file.parent, fileName)
        }
    }
}

Not all output files are renamed, it always skips 1 file. Why?

myapp-debug-unaligned-renamed.apk    <-renamed, OK!
myapp-release.apk                    <-NOT renamed, WRONG!
myapp-release-unaligned-renamed.apk  <-renamed, OK!
解决方案

I solved using this code:

buildTypes {
    release {
        signingConfig signingConfigs.release 
    }

    applicationVariants.all { variant ->
        def apk = variant.packageApplication.outputFile;
        def newName = apk.name.replace(".apk", "-renamed.apk");
        variant.packageApplication.outputFile = new File(apk.parentFile, newName);
        if (variant.zipAlign) {
            variant.zipAlign.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
        }
    }
}

The block applicationVariants.all {...} is now outside the release {...} block.

I think variant.zipAlign.outputFile makes the difference.

这篇关于摇篮:applicationVariants.all跳过一个变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:26