Stabilitronio
Stabilitronio

Reputation: 93

Gradle set Android project config

There's an app for android, which is a client for a web service at the some url. Going project build with gradle on CI-server (Teamcity). I need to build the application for the address of a web service and want to do it with pleasure=) How is it possible to transfer this setting when building gradle? I would want that you can specify a parameter in the assembly in the console or in the gradle configuration file, which would be used in the application. I am satisfied if this setting will be hardcoding (its change in the application is not provided) Or need to create a file with gradle and installing it up to device with apk? Where should I put it in this case? Sorry for my english. Not my native. I hope my question is clear. What is the best way? How do I get it from my code inside app?

What I have: teamcity, gradle, android-project

What I want: build android project with some settings (address of server API) without code changing

Upvotes: 1

Views: 178

Answers (1)

Stabilitronio
Stabilitronio

Reputation: 93

In my case it was enough:

In build.gradle, inside defaultConfig block:

defaultConfig
    {
        ...
        buildConfigField 'String', 'ServerAddress', '"http://example.com"'
        ...
    }

It generate inside BuildConfig.java:

public final class BuildConfig 
{
    ...
    public static final String ServerAddress = "http://example.com";
}

And finaly usage in my code (I use RoboSpice):

@Rest(rootUrl = BuildConfig.ServerAddress, converters = { MappingJackson2HttpMessageConverter.class })
public interface IRestClient
{
...

Upvotes: 1

Related Questions