androidgradlebuild-variant

Multiple Gradle productFlavors


I'm working on a Android application with two 'skins' and two kind of 'builds':

productFlavors {
   Staging {
     dimension "server"
     buildConfigField 'String', 'API', "the staging API URL"
   }

   Production {
     dimension "server"
     buildConfigField 'String', 'API', "the production API URL"
   }

   SkinOne {
     dimension "skin"
     // Use default API URL
   }

   SkinTwo {
     dimension "skin"
     // Set a specific staging/production API URL
     // if buildVariant.contains("Staging") -> set staging URL
     // else if buildVariant.contains("Production") -> set production URL
   }
}

The problem is that I want to change te API string when I select the build variant with SkinTwo.

Is this possible in Gradle or do I have to fix this in the BuildConfig class?

I already tried this variantFilter but that doesn't work.


Solution

  • I already fixed it:

    productFlavors {
       Staging {
         dimension "server"
       }
    
       Production {
         dimension "server"
       }
    
       SkinOne {
         dimension "skin"
       }
    
       SkinTwo {
         dimension "skin"
       }
    }
    
    applicationVariants.all { variant ->
        if (variant.getName().contains("StagingSkinOne")) {
            variant.buildConfigField 'String', 'API', "staging URL"
        } else if (variant.getName().contains("ProductionSkinOne")) {
            variant.buildConfigField "String", "API", "production URL"
        } else if (variant.getName().contains("StagingSkinTwo")) {
            variant.buildConfigField "String", "API", "the specific staging URL"
        } else if (variant.getName().contains("ProductionSkinTwo")) {
            variant.buildConfigField "String", "API", "the specific production URL"
        }
    }