Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 367
Divyansh Goenka Avatar asked Oct 25 '25 16:10

Divyansh Goenka


1 Answers

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"
like image 179
Divyansh Goenka Avatar answered Oct 28 '25 07:10

Divyansh Goenka