Simas
Simas

Reputation: 44188

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:

Looking for an alternative.

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

Upvotes: 16

Views: 11121

Answers (2)

Rickard Elimää
Rickard Elimää

Reputation: 7591

I wanted something similar, so I spent about two hours learning about Groovy Methods. I wanted be able to go against a production, sandbox and local environment. Because I'm lazy, I only wanted to change the URL at one place. Here is what I came up with:

 flavorDimensions 'environment'
    productFlavors {
        production {
            def REMOTE_HOST = "evil-company.com"
            buildConfigField 'String', 'API_HOST', "\"${REMOTE_HOST}\""
            buildConfigField 'String', 'API_URL', "\"https://${REMOTE_HOST}/api/v1/\""
            buildConfigField 'String', 'WEB_URL', "\"https://${REMOTE_HOST}/\""
            dimension 'environment'
        }
        rickard {
            def LOCAL_HOST = "192.168.1.107"
            buildConfigField 'String', 'API_HOST', "\"${LOCAL_HOST}\""
            buildConfigField 'String', 'API_URL', "\"https://${LOCAL_HOST}/api/v1/\""
            buildConfigField 'String', 'WEB_URL', "\"https://${LOCAL_HOST}/\""
            applicationIdSuffix ".dev"
        }
    }

Alternative syntax, because you can only use ${variable} with double quotes in Groovy Methods.

rickard {
    def LOCAL_HOST = "192.168.1.107"
    buildConfigField 'String', 'API_HOST', '"' + LOCAL_HOST + '"'
    buildConfigField 'String', 'API_URL', '"https://' + LOCAL_HOST + '/api/v1/"'
    buildConfigField 'String', 'WEB_URL', '"https://' + LOCAL_HOST + '"'
    applicationIdSuffix ".dev"
}

What was hard for me to grasp was that strings needs to be declared as strings surrounded by quotes. Because of that restriction, I couldn't use reference API_HOST directly, which was what I wanted to do in the first place.

Upvotes: 0

Simas
Simas

Reputation: 44188

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");
}

Upvotes: 37

Related Questions