Android Development Profile (Profile)

Asked

Viewed 300 times

1

I was taking a look here and I still can’t find out if there is a way, I believe you also go through this.

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, we point to our local IP or server testing, and when we put into production we go there in the code and change the address(s) 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 the Spring I use the spring.profiles.active which I define as dev where I have the files already configured with all development addresses that just change an environment variable or on itself web.xml to change all configuration automatically or even at runtime.

There is something similar that does this automatically?

3 answers

1


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

In the code it is necessary to load this property file and from this instance, where the file was loaded, it is possible to load the values from a key.

  • fmsedrez, thinking that the only way to make it more friendly will be this very.

1

I like to address this issue of profiles in the build, using Gradle buildTypes.

In my case usually my apps 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 your app’s build.Radle, 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 usually have different package Names for each build type

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

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

0

  • 1

    Welcome! Even if this link is a good suggestion, this reply will not be valid if one day the link ceases to work. So, because it’s important for the community to have content here, you’d better respond with more complete answers. You can elaborate more your answer?

  • I thought to use so more would be more a screen for the user to access and make "caca", in the case of build is already implicit that the apk is production or test without having option of the user to change.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.