Android development profile (Profile)

1

I was looking around here and I still have not found if there is a way, I think you also have it.

When we are developing an application that uses some service, we have somewhere in our code the address of the server that we will use to send / receive the data that usually, when we are developing, point to our Local IP or server and when we go into production let's go in the code and change the address (es) as in the code below:

/* Exemplo */
Class Servidor{
   public String final SERVIDOR = "http://192.168.1.8:8080"; //DESENVOLVIMENTO
/* public String final SERVIDOR = "http://servidorproducao.com.br"; //DESENVOLVIMENTO */

}

In Spring I use the spring.profiles.active that I define as dev where I have the files already configured with all the development addresses that just change an environment variable or in web.xml itself to change the whole configuration automatically or even in runtime.

Is there anything similar that does this automatically?

    
asked by anonymous 30.01.2014 / 17:10

3 answers

1

In Java, a file with key-value pairs can be made and loaded using the

30.01.2014 / 17:18
1

I like to handle this profile question in the build, using gradite buildTypes.

In my case, normally my applications have 3 different builds:

  • Dev pointing to development server
  • Staging pointing to the staging / pre-production server
  • Release, which is the signed build pointing to the production environment

In the build.gradle of your application, it would look something like this:

android {
    buildTypes {
        release {
            buildConfigField "String", "BASE_URL", "\"https://api.aplicativo.com/api/v1\""
        }

        debug {
            packageNameSuffix ".debug"
            versionNameSuffix "-debug"
            zipAlign true
            buildConfigField "String", "BASE_URL", "\"https://dev.aplicativo.com/api/v1\""
        }

        staging.initWith(buildTypes.debug)
        staging {
            packageNameSuffix ".stating"
            versionNameSuffix "-staging"
            zipAlign true
            buildConfigField "String", "BASE_URL", "\"https://staging.aplicativo.com/api/v1\""
        }
    }
}

And in the code, only access via the BuildConfig class

HttpGet get = new HttpGet(BuildConfig.BASE_URL + "/clients/");

Note that I also often have different package names for each type of build

com.example.app - > release
com.exemple.app.debug - > debug
com.exemple.app.stating - > staging

That way I can have several versions of the app installed on my smartphone / emulator, each pointing to a different environment.

    
02.02.2014 / 15:34
0

On Android you have the option to use ActivityPreferences to configure how your application works.

Tutorial Activity Preferences

    
01.02.2014 / 15:42