admin管理员组

文章数量:1292175

I would like to know how to set the OutputfileName generated by AndroidStudio with a Graddle project depends on the value of a buildConfigField.

For exemple, i define a boolean field named "NO_BLUETOOTH_CHECK".

The OutputFileName should be

  • myproject_BTOn_r_10.0.2.apk if NO_BLUETOOTH_CHECK is false

  • myproject_BTOff_r_10.0.2.apk if NO_BLUETOOTH_CHECK is true

I do not not how to deal with it. I tried the code below and much more but i got always compiling errors.

 buildTypes {
        release {
            buildConfigField "boolean", "NO_BLUETOOTH_CHECK", "true"
            buildConfigField "String", "SFTP_USERNAME", '""'
            buildConfigField "String", "SFTP_PASSWORD", '""'

            signingConfig signingConfigs.release
        }
        debug {
            buildConfigField "boolean", "NO_BLUETOOTH_CHECK", "true"
            buildConfigField "String", "SFTP_USERNAME", '"******"'
            buildConfigField "String", "SFTP_PASSWORD", '"******"'

            signingConfig signingConfigs.debug
        }

        applicationVariants.all { variant ->
            //   signingConfig signingConfigs.variant
            variant.outputs.all { output ->
                def BTValue = variant.buildTypes.buildConfigFields[ "NO_BLUETOOTH_CHECK"].value ? "BTOff" : "BTOn"

                if (variant.buildType.name == 'release')
                    outputFileName = new File("${rootProject.name}_${BTValue}_r_${variant.versionName}.${variant.versionCode}.apk");
                else
                    outputFileName = new File("${rootProject.name}_${BTValue}_d_${variant.versionName}.${variant.versionCode}.apk");
            }
        }

I would like to know how to set the OutputfileName generated by AndroidStudio with a Graddle project depends on the value of a buildConfigField.

For exemple, i define a boolean field named "NO_BLUETOOTH_CHECK".

The OutputFileName should be

  • myproject_BTOn_r_10.0.2.apk if NO_BLUETOOTH_CHECK is false

  • myproject_BTOff_r_10.0.2.apk if NO_BLUETOOTH_CHECK is true

I do not not how to deal with it. I tried the code below and much more but i got always compiling errors.

 buildTypes {
        release {
            buildConfigField "boolean", "NO_BLUETOOTH_CHECK", "true"
            buildConfigField "String", "SFTP_USERNAME", '""'
            buildConfigField "String", "SFTP_PASSWORD", '""'

            signingConfig signingConfigs.release
        }
        debug {
            buildConfigField "boolean", "NO_BLUETOOTH_CHECK", "true"
            buildConfigField "String", "SFTP_USERNAME", '"******"'
            buildConfigField "String", "SFTP_PASSWORD", '"******"'

            signingConfig signingConfigs.debug
        }

        applicationVariants.all { variant ->
            //   signingConfig signingConfigs.variant
            variant.outputs.all { output ->
                def BTValue = variant.buildTypes.buildConfigFields[ "NO_BLUETOOTH_CHECK"].value ? "BTOff" : "BTOn"

                if (variant.buildType.name == 'release')
                    outputFileName = new File("${rootProject.name}_${BTValue}_r_${variant.versionName}.${variant.versionCode}.apk");
                else
                    outputFileName = new File("${rootProject.name}_${BTValue}_d_${variant.versionName}.${variant.versionCode}.apk");
            }
        }
Share Improve this question edited Feb 13 at 10:25 Yaegaki2 asked Feb 13 at 10:24 Yaegaki2Yaegaki2 96 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I can't see any buildTypes property for variant. Perhaps you meant buildType (without an ending s)?

There are two other issues that I can see:

  • Accessing the buildConfigFields Map may return null, which you need to deal with.
  • Your boolean BuildConfig fields will actually return strings, which you need to convert to booleans if you want to use them as such.

So something like this should work:

def BTValue = variant.buildType.buildConfigFields["NO_BLUETOOTH_CHECK"]?.value?.toBoolean() ? "BTOff" : "BTOn"

本文标签: gradleAndroidHow to generate outputFileName with buildConfigField optionsStack Overflow