I've seen scripts that manipulate the build tasks of gradle and am now wondering if it's possible to set a string outside the `defaultConfig but treat it as default for all flavors.
Basically I've multiple flavors and all of them have a specific ApplicationId. I want to use that id to set a string resource to be used later in my java code.
defaultConfig {
minSdkVersion 11
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
productFlavors {
one {
applicationId "com.my.app.one"
}
two {
applicationId "com.my.app.two"
}
}
I want to set a string like this:
resValue "string", "authority", applicationId + ".dataprovider"
What I've tried:
defaultConfig
section the applicationId is still null, thus I get a wrong stringLooking for an alternative.
P.S. I'm using Android Studio 0.8.12.
There is no "current flavor". The Gradle build file is building an object model of the build process. It is not an interpreted script only being used once a build is underway and a "current flavor" is known.
Build Variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Build Type applies different build and packaging settings. An example of build types are “Debug” and “Release”.
Product flavors are a powerful feature of the Gradle plugin from Android Studio to create customised versions of products. They form part of what we call Build Variants.
When to use Product Flavors. When we want to address the issue of having separate project code for each version of the app while still having one project code. Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.
Well after a lot of searching I've managed to find the answer to this. Perhaps someone will find this useful.
productFlavors.all {
resValue "string", "authority", applicationId + ".dataprovider"
}
This simple snippet sets a string to all flavors after their invidual variables have been set. It's kind of like a defaultConfig
but not quite since it's executed after the flavor blocks.
Extra:
Turns out I can even set the applicationId
! My final result so far is:
def final String AUTHORITY = '.dataprovider'
productFlavors.all {
applicationId "com.my.app." + name
resValue "string", "authority", applicationId + AUTHORITY
buildConfigField "String", "AUTHORITY", "\""+applicationId + AUTHORITY+"\""
}
Now I can get each flavor provider's authority through BuildConfig.AUTHORITY
and @string/authority
, which I use in the manifest and in my class file respectively:
<provider
android:name="com.my.app.DataProvider"
android:authorities="@string/authority"
android:exported="false" />
public class DataProvider extends ContentProvider {
public static final String PROVIDER = BuildConfig.AUTHORITY;
public static final Uri SEARCH_URI = Uri.parse("content://" + PROVIDER + "/search");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With