Divyansh Goenka
Divyansh Goenka

Reputation: 1094

How to get the buildtype in a build.gradle EARLY ON

I have a React Native application and I'm trying to specify the folder for storing the generated source map in project.ext.react like so (I need it to implement a library which will help me debug JS errors)

project.ext.react = [
        ...
        extraPackagerArgs    : [ "--sourcemap-output", "$buildDir/intermediates/assets/$buildType/index.android.bundle.map" ]
]

As evident, the path should be something like <buildDir>/intermediates/assets/<buildType>/index.android.bundle.map

(e.g. <buildDir>/intermediates/assets/release/index.android.bundle.map in case of release and <buildDir>/intermediates/assets/debug/index.android.bundle.map in case of debug)

After referring to several answers on StackOverflow and outside, I'm getting the buildType in the build.gradle by declaring it first and then assigning it:

//3rd line of build.gradle
def buildType
....
//much later
   applicationVariants.all { variant ->
    buildType = variant.buildType.name 
....

However, this leads to a problem where buildType is initialized much after it is used, and so the output path becomes something like <buildDir>/intermediates/assets/null/index.android.bundle.map, thereby failing the whole process for me. Is there a way to get the build type earlier on?

Upvotes: 4

Views: 2591

Answers (2)

Divyansh Goenka
Divyansh Goenka

Reputation: 1094

I finally found a way (though its not a very clean one) to look through the array of gradle tasks:

def buildType = gradle.startParameter.taskNames.any{it.toLowerCase().contains("debug")}?"debug":"release"

Upvotes: 9

parohy
parohy

Reputation: 2180

We have under the buildTypes configuration inside app.gradle something like this:

android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
        //def newApkName = "${APP_NAME}-${variant.name}-${variant.versionName}.apk"
        def newApkName = "${APP_NAME}-${variant.name}-r${COMMIT_SHA}-${BRANCH_NAME}-v${variant.versionName}-${BUILD_NUMBER}.apk"

        variableFile.withWriterAppend { out ->
            out.writeLine("${APP_NAME}-${variant.name}=${APP_NAME}-${variant.name}-r${COMMIT_SHA}-${BRANCH_NAME}-v${variant.versionName}-${BUILD_NUMBER}")
            out.writeLine("${APP_NAME}-${variant.name}versionCode=${VERSION_CODE}")
            out.writeLine("${APP_NAME}-${variant.name}versionName=${variant.versionName}")
        }

        output.outputFile = new File(output.outputFile.parent, newApkName)
    }
}

This sets the apk name and the output folder for each buildType defined in gradle. We have buildTypes:

buildTypes {
    debug {
        buildConfigField('String', 'BUILD_ENV', '"Development"')
        minifyEnabled false
    }
    qa {
        signingConfig signingConfigs.release
        minifyEnabled true
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        buildConfigField('String', 'BUILD_ENV', '"QA"')
    }
    release {
        signingConfig signingConfigs.release
        minifyEnabled true
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        buildConfigField('String', 'BUILD_ENV', '"Production"')
    }
}

Upvotes: 1

Related Questions