Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a string in gradle and also use a flavor-specific applicationId

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:

  • If I put it in the defaultConfig section the applicationId is still null, thus I get a wrong string
  • Putting it to each flavor's section works but since I have multiple flavors creates a lot of code repetition

Looking for an alternative.

P.S. I'm using Android Studio 0.8.12.

like image 309
Simas Avatar asked Oct 23 '14 12:10

Simas


People also ask

How do I get the current flavor in Gradle?

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.

What are buildTypes and product Flavours in Gradle and what can you use them for?

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”.

What is Gradle product flavors?

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 would you use a product flavors in your build setup?

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.


1 Answers

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");
}
like image 102
Simas Avatar answered Oct 24 '22 20:10

Simas