Gradle: applicationVariants.all skips one variant

后端 未结 3 1911
感情败类
感情败类 2021-02-03 13:00

I\'m using Gradle to compile my Android project:

buildTypes {
    release {
        signingConfig signingConfigs.release 
        applicationVariants.all { varia         


        
相关标签:
3条回答
  • 2021-02-03 13:16

    I simplified it by removing one of your lines but essentially you need to change it like so:

    android {
    
        buildTypes {
        ...
        }
    
        applicationVariants.all { variant ->
            def file = variant.outputFile
            def fileName = file.name.replace(".apk", "-renamed".apk")
            variant.outputFile = new File(file.parent, fileName)
        }
    }
    
    0 讨论(0)
  • 2021-02-03 13:32

    There should be 3 output APK files when using your build.gradle configuration: debug unsigned unaligned, release signed aligned and release signed unaligned. There are two variables for applicationVariant to deal with output files: outputFile and packageApplication.outputFile, the former is used for zipalign and the later is used in general case.

    So the proper way to rename all the files will be like this:

    android.applicationVariants.all { variant ->
        if (variant.zipAlign) {
            def oldFile = variant.outputFile;
            def newFile = oldFile.name.replace(".apk", "-renamed.apk")
            variant.outputFile = new File(oldFile.parent, newFile)
        }
    
        def oldFile = variant.packageApplication.outputFile;
        def newFile = oldFile.name.replace(".apk", "-renamed.apk")
        variant.packageApplication.outputFile = new File(oldFile.parent, newFile)
    }
    
    0 讨论(0)
  • 2021-02-03 13:38

    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.

    0 讨论(0)
提交回复
热议问题