jane
jane

Reputation: 124

How to access current build variant in gradle.properties?

I need to access the current build variant in gradle properties whenever I change the build variant through the Android Studio interface. Is there any way to access that value?

The problem:

So I have two modules that need to be included according to specific build variants. but using specific flavor implementations (debugImplementation, releaseImplementation) doesn't work; those two modules give conflict even tho they shouldn't. As a work-around I did this: I created a flavor variable in gradle.properties and included the modules in the settings.gradle according to that property:

gradle.properties:

flavor = pos

settings.gradle:

if (getProperty("flavor") == "host"){
    include ':host'
}
if (getProperty("flavor") == "pos"){
    include ':pos'
}

It works like this but I want to automate this process as I don't want to change the variable each time I change the build variant. I need to access that value at gradle sync, not when I build the project

Upvotes: 0

Views: 1766

Answers (2)

jane
jane

Reputation: 124

What I wanted to was to access the current active build variant at Gradle sync so I could have different Maven repositories for different modules. I couldn't access that data. What I ended up doing was deleting repositories from project level build.gradle and creating settings.gradle file for each of modules with specific repositories:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        maven {//your specific configuration}

so this way any repository declared in project will be ignored, it will use repositories from separate settings file.

Upvotes: 0

Have you do this by setting a project-wide variable in your build.gradle file using the Gradle project.ext property, and then using that variable in settings.Gradle to incorporate the modules conditionally.

  1. In your build.gradle file for the(usually at the app module level), set a project-wide property:

    android {
    
     buildTypes {
         debug {
             project.ext.set("currentFlavor", 
     "debug")
         }
         release {
             project.ext.set("currentFlavor", 
     "release")
         }
     }
    }
    
  2. Then go to your settings.gradle file and check the value of the currentFlavor property and include the appropriate modules:

    if (project.hasProperty("currentFlavor")) { def currentFlavor = project.currentFlavor if (currentFlavor == "debug") { include ':host' } else if (currentFlavor == "release") { include ':pos' } }

Now when you change the build variant in the Android Studio interface, the currentFlavor property is updated, and the necessary modules are added to settings.Gradle during the process of Gradle Sync. This eliminates the need for you to manually update the gradle.properties file whenever the build variation is modified.

  1. The build.gradle file of the app module contains project.ext properties that are unique to that module. You must also declare the currentFlavor variable at the project level if you wish to utilize it at the project level (in the main project or any child projects).

    `// project-level build.gradle

    allprojects { ext { // Other project-level properties can go here currentFlavor = "" } }

Just ensure that you assign a value to currentFlavor in the app module's build.gradle file or any other module where you need it.

  `// app-level build.gradle

   android {
   buildTypes {
       debug {
        project.ext.set("currentFlavor", 
   "debug")
    }
    release {
        project.ext.set("currentFlavor", 
   "release")
    }
    }
   }
  1. For: also it doesnt recognize the project inside settings file: > Could not get unknown property 'project' for settings 'test' of type org.gradle.initialization.DefaultSettings.

Because the settings.gradle script is executed before the projects are defined, the project variable in the settings.gradle file is not immediately accessible. As a result, the project variable is still outside of its scope.

The rootProject variable can be used to retrieve project properties in the settings.gradle file. In a multi-project build, the root project is referred to as the rootProject.

  `// settings.gradle

  if (rootProject.hasProperty("currentFlavor")) 
 {
 def currentFlavor = rootProject.currentFlavor
 if (currentFlavor == "debug") {
    include ':host'
 } else if (currentFlavor == "release") {
    include ':pos'
 }`

Ensure that you've declared the currentFlavor property in the root project's build.gradle file as mentioned earlier:

  // project-level build.gradle

  allprojects {
    ext {
        currentFlavor = ""
    }
  }

Upvotes: -1

Related Questions