104

I'm developing an android application which uses web service to get data from server, for that I'm having three different set of URLs to point development system, test server and live server. It's difficult to change URL whenever I want to give application for testing/live. so I planned to make it as configurable, so that application can get appropriate URL based on me build type configuration constant. So,

  • which is the best way to keep this constants, java static class or java public interface or xml resource file.? When? Why?
  • which gives better performance?, When? Why?

Ex: xml resource

<integer name="config_build_type">0</integer>
<string-array name="url_authentication">
    <item >http://development.com/xxxx</item>
    <item >http://test.com/xxx</item>
    <item >http://example.com/xxx</item>
</string-array>

Java static constant

public class Config {
    public static final int BUILD_TYPE = 0; // 0 - development, 1 - test, 2 - live
    public static final String[] URL_AUTHENTICATION = {"http://development.com/", "http://test.com/", "http://example.com"};
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Jayabal
  • 3,619
  • 3
  • 24
  • 32

8 Answers8

98

There is a big difference between the two in that you can reference project resources in your XML layouts. They are available in the application context and are therefore accessible across the global application. The biggest advantages of using project resources is the ease of access and that they allow you to organize your project significantly.

static final constants are compiled into the java bytecode; project resources are compiled into a binary format within the apk. Accessing either is extremely efficient... if there is a difference between the two, it is trivial at most.

There isn't a set rule on how you should be using resources/constants in your project. That said, I personally use resources for values that I might need to use in my XML or java code. On the other hand, I typically use static final constants for values that will only be used by my java code and are specific to my implementation.

Also note that it is possible to load XML resources at runtime depending on the device's current configuration (i.e. screen size, locale, etc.). So you should take this into consideration when deciding whether or not you should declare the constant in XML or directly in your .java files.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • resources are a lot easier to configure in different flavors/build types – HopefullyHelpful Nov 17 '16 at 14:39
  • @Alex I have one doubt can you please clear it : It is recommended to use static variables less so if we create constant file with static variable will it not effect the performance of our application? – Pallavi Nov 21 '16 at 06:06
  • 2
    An additional point to consider is that, if a final constant is used across multiple classes, then all of those objects will need to be recompiled if the constant is changed. But that's not the case if the constant is stored as a resource. This could be an important consideration for a large project that takes a long time to compile. – orodbhen Oct 05 '17 at 00:57
29

For the people who want to see how we can use a Class to define our constants and call any where we need.

Constant.java

    package org.nrum.nrum;

/**
 * Created by rajdhami on 5/23/2017.
 */
public class Constant {
    public static final String SERVER = "http://192.168.0.100/bs.dev/nrum";
//    public static final String SERVER = "http://192.168.100.2/bs.dev/nrum";
    public static final String API_END = SERVER + "/dataProvider";
    public static final String NEWS_API = API_END + "/newsApi";
    public static final String BANNER_API = API_END + "/bannerApi/lists";
    public static final String NOTICE_API = API_END + "/noticeApi/lists";
    public static final String UPLOAD_PATH = SERVER + "/uploads";
    public static final String UPLOAD_PATH_BANNER = UPLOAD_PATH + "/company_1/banner";
    public static final String UPLOAD_PATH_NEWS = UPLOAD_PATH + "/company_1/news";
    public static final int BANNER_TRANSITION_DURATION = 5000;
    public static final int NOTICE_BUTTON_BLINK_DURATION = 5000;
    public static final int BANNER_FETCH_LIMIT = 3;
}

Now we can use above constants in following way.

Constant.NOTICE_BUTTON_BLINK_DURATION
Mahen
  • 760
  • 9
  • 12
13

In general case:

  • XML values have the advantage of accessbilty in layout file and manifest file over Constants in java file
  • XML values have the advantage for multi language support over Constants in java file
cHao
  • 84,970
  • 20
  • 145
  • 172
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • 1
    thank u Dheeresh, ya I understood, it is very useful to keep configuration in xml which are accessed by both layouts, manifest and java – Jayabal Jun 22 '12 at 06:21
2

It’s always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory in each Android project.

http://developer.android.com/training/basics/supporting-devices/languages.html

cHao
  • 84,970
  • 20
  • 145
  • 172
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • The question is about constants to be used internally only by Java code. It is a different scenario. For the specific case of text tags belonging to the user interface, as you mention, there is no doubt that the right place are the XML resources of the Android project. – cesargastonec Aug 03 '20 at 21:07
2

I think both way seems to be good but thing is that it depends on your requirements.

If you have your values(web service link) in your XML and suppose there is any change in your values(web service link) , you can easily change only in XML file.

But if you use inside classes as static variables you have to change in all class files.

So my suggestion is that separate constants from source file and put into resource and access it..

Venky
  • 11,049
  • 5
  • 49
  • 66
  • 1
    "you can easily change only in XML file" It is as easy as changing it in a Java file, and in both cases you'll need to rebuild the project, so I don't think that's a difference, let aside an advantage. – Fran Marzoa Jul 23 '19 at 09:21
0

Project resources need access to Context, which is not available in static methods unless you pass it in, but is always available in Activities -- there seems to be a preferential connection between resources and layouts.

For app variables and constants that may be processed in static methods I create an abstract class and do a static import (of this constants class) in all the other project class files.

Matt N.
  • 53
  • 1
  • 10
PVS
  • 1,168
  • 11
  • 24
0

If the constant related or depend on locale or language or use in manifest you may consider to use xml values

If the constant dont't related to translation or locale or use in manifest I would avoid to put it in xml resource

I would add enum inside the viewmodel or whatever that need to use constant as a key.

enum class Key{
        key,rule,practice
    }

Or

const val MY_CONST = "my constant"
UmAnusorn
  • 10,420
  • 10
  • 72
  • 100
0

In Kotlin:

class Foo() { 
    // any other code for the class Foo 

    companion object {
        const val MY_CONSTANT = "my constant"
    }
} 

This method is particularly recommended for data object.

us_david
  • 4,431
  • 35
  • 29