1

I have an android app that uses a certain url to communicate with our server when used in production. I want to be able to use a different url when I am debugging, so that I can run the app against a local instance of the server on my machine.

How can I do this without manually editing the url?

2
  • 1
    Why did the 2 answers that got downvoted get downvoted? Commented Oct 6, 2014 at 13:03
  • 1
    @zgc7009 my guess is that it was because they don't address the underlying issue - without manually editing - which implies some dynamic logic, which those 'answers' do not address Commented Dec 20, 2014 at 5:51

4 Answers 4

4

Using Android Studio, you can define build variants, and define strings in that case:

Generate Java Constants

Plugin version greater than 0.7.x (current method)

android {
    buildTypes {
        debug {
            buildConfigField "int", "FOO", "42"
            buildConfigField "String", "FOO_STRING", "\"foo\""
        }

        release {
            buildConfigField "int", "FOO", "52"
            buildConfigField "String", "FOO_STRING", "\"bar\""
        }
    }
}

Plugin version less than 0.7 (old)

android {
    buildTypes {
        debug {
            buildConfig "public final static int FOO = 42;"
        }

        release {
            buildConfig "public final static int FOO = 52;"
        }
    }
}

You can access them with BuildConfig.FOO

Generate Android resources (since plugin 0.8.3)

android {
    buildTypes {
        debug{
            resValue "string", "app_name", "My App Name Debug"
        }
        release {
            resValue "string", "app_name", "My App Name"
        }
    }
}

You can access them in the usual way with @string/app_name or R.string.app_name

source: https://stackoverflow.com/a/17201265/1096905 (All credits to him, i just copy&paste)

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using Android Development Tools for Eclipse, look into BuildConfig.DEBUG. There is some discussion of it at When does ADT set BuildConfig.DEBUG to false?

Comments

-1

You could make a utility class that generates urls for you. Extending on what Robin Ellerkmann mentioned before me.

public class UrlGenerator{

    private static final String STAGIN_URL = "http://www.statingurl.com/";
    private static final String PRDOUCTION_URL = "http://www.productionurl.com/";

    private static final boolean DEBUGGING = true;

    private static String getBaseUrl(){
        if(DEBUGGING)
            return STAGING_URL;
        else
            return PRODUCTION_URL;
    }

    public static String getLoginUrl(){
        return getBaseUrl() + "login";
    }
}

And then throughout your app something like

String loginRequestUrl = UrlGenerator.getLoginUrl();

That way it is all managed in your utility class.

Comments

-2

Use a global DEBUGGING boolean variable and if/else statements in your code.

private boolean DEBUGGING = true;

if (DEBUGGING) {
    //Use local machine url here
} else {
    //Use real url here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.