user565
user565

Reputation: 1001

How to access gradle defaultConfig in a parent gradle file

I want to access the defaultConfig which define in a subproject (app--> build.gradle) into a parent build.gradle but seems to be not working OR I am doing something wrong. Actually I need to access the buildvarient which I define in a defaultConfig of app build.gralde file.

app build.gradle:

android {
    def buildTypeV // define here to update in loop

     dependencies.metaClass.allCompile { dependency ->
        buildTypes.each { buildType ->
      //      "${buildType.name}Compile" project(path: ":${dependency.name}", configuration: buildType.name)
            buildTypeV=buildType.name
        }
    }

    defaultConfig {
         versionNameSuffix "${buildTypeV}"
    }

From parent build.gradle file, I am doing like this to access versionNameSuffix:

gradle.buildFinished {

     def buildV=${project.android.defaultConfig.versionNameSuffix};
        println "buildV:::: $buildV"

    }

It seems to be a wrong way to access the defaultConfig as the build is failing it. Can someone give me an idea of how I can access It properly?

Error which I see on console:

* What went wrong:
Could not find method $() for arguments [build_8kbbn8x9zcumz6eqfx8ryt61s$_run_closure2$_closure3@77596dd9] on root project 'df-backpack' of type org.gradle.api.Project.

In my subproject define application variant:

android.applicationVariants.all { variant ->
    variant.outputs.all {
        def flavor = variant.name
        outputFileName = "../app-${flavor}-final.apk"
    }
    variant.assembleProvider.get().doLast getbuilder(variant.name.toLowerCase())
}

// in parent build.gradle:

 project('hpnurse') {
        buildStr=it. android.applicationVariants.all;
        //also tried like this 
        //it.variant.buildType.name;

    }

Upvotes: 0

Views: 1946

Answers (1)

Giorgio Antonioli
Giorgio Antonioli

Reputation: 16214

When you are calling project.android you referring to the caller project, in your case, the parent project. Instead, you should refer to your Android subproject.

From the parent project you can access a specific subproject by name using project(name).

gradle.buildFinished {
    project('app') {
        def buildV = it.android.defaultConfig.versionNameSuffix
        println "buildV:::: $buildV"
    }
}

Upvotes: 1

Related Questions