Java – eclipse Java : Define final variable based on build configuration

eclipse Java : Define final variable based on build configuration… here is a solution to the problem.

eclipse Java : Define final variable based on build configuration

I

want a final variable which is true when I run the debug version of my project and false when I run it. I know I can do this by building a config, but don’t know how to set it up in Eclipse. There don’t seem to be any tutorials or questions on Stack Exchange about defining variables specifically.

I compiled Java in Eclipse Classic 4.2 and created an Android application using the ADT plugin.


EDIT: Based on the @Xavi, I set the following:

    try {
        String line = null;
        java.lang.Process p = Runtime.getRuntime().exec("getprop debugging");
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            Log.d("Property:", line); <-- Parse data here.
        }
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

In the Additional

Emulator Command Line Options field of the Target tab of the Debug Configurations window, I entered:

-prop debugging=true

Unfortunately, it looks like it only works in emulator mode. It doesn’t print anything when running on my phone. (It works fine on the emulator.) )


Edit: Per @Sharks I found some links that look related, but I don’t know how to apply them to my case :

Solution

If you are working in Eclipse using ADT, you can check the variable BuildConfig.DEBUG, which is automatically generated and placed in gen/<package>/BuildConfig.java:

if (BuildConfig.DEBUG) {
   variable = true;
}

Related Problems and Solutions