3

I want to have a final variable that is true when I run the Debug version of my project, and false when I run the Run version. I understand I can do this with build configurations but have no idea how to set this up in Eclipse. There don't appear to be any tutorials or questions on Stack Exchange regarding defining variables specifically.

I'm compiling Java in Eclipse Classic 4.2, creating an Android app using the ADT plugin.


EDIT: Per @Xavi, I set up 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();
    }

And in the "Additional Emulator Command Line Options" field of the Target tab of the Debug Configurations window, I've entered:

-prop debugging=true

Unfortunately it looks like this only works in emulator mode. It doesn't print anything when running on my phone. (It works fine running on an emulator.)


EDIT: Per @Sharks I found some links that seem relevant, but I don't know how to apply them to my situation:

Community
  • 1
  • 1
TheMaster42
  • 150
  • 6
  • the quickest solution is to use cmdline arguments. the quick solution is to use a .config file and read that. and there's always a reflection-based solution ;) – Shark Sep 06 '12 at 09:51
  • @Shark - I googled all three of the things you mentioned, but I didn't see anything I understood or that looked appropriate to specifically Android on Eclipse. The Android configurations in Eclipse seem to have far fewer features/options than for other "languages." I editing the question listing some links that seemed relevant, but which I can't make apply to my configuration. – TheMaster42 Sep 06 '12 at 20:02

3 Answers3

4

If you're working in Eclipse with ADT then you can check variable BuildConfig.DEBUG. It's generated automatically and placed in the gen/<package>/BuildConfig.java:

if (BuildConfig.DEBUG) {
   variable = true;
}
Yury
  • 20,618
  • 7
  • 58
  • 86
  • uh, i don't think anything except R.java goes there but I might be wrong. – Shark Sep 06 '12 at 09:50
  • 2
    This feature appeared not a long time ago. So, not a lot people know about it. – Yury Sep 06 '12 at 09:52
  • Sounds great then :) when did it appear? – Shark Sep 06 '12 at 09:53
  • 3
    I heard that BuildConfig.DEBUG is... buggy. http://code.google.com/p/android/issues/detail?id=27940 - http://www.digipom.com/be-careful-with-buildconfig-debug/ I tried it earlier with the "Run" button and it still returned `true`. – TheMaster42 Sep 06 '12 at 09:58
  • It's appeared from ADT 17.0.0 (March 2012). @TheMaster42 Run button does not produce release code. But I'm agree with you the feature sometimes behaves incorrectly (the usual solution is to disable Automatic Build and Run clean). – Yury Sep 06 '12 at 10:57
  • @Yury In Eclipse I unchecked `Project->Build Automatically` and then chose `Project->Clean...`, followed by `Project->Build Project`. This still results in BuildConfig.DEBUG evaluating to true when I run my "Run" build. (The "Run" button in Eclipse.) – TheMaster42 Sep 06 '12 at 20:30
  • But it's ok. When you export your signed package this value should disappear. Try to create a simple program that simply produce in Activity value of the DEBUG value. At first, at first run this program from Run command and then export a signed apk file, install it on your device and check the result. – Yury Sep 07 '12 at 10:59
  • What I'd want is 'true' when I do 'Debug as' but 'false' otherwise 'Run as' or apk packaged); is this the *intended* way the constant should get generated? If not, its name is highly misleading. – akauppi Mar 05 '14 at 10:12
1

In addition to @Yury's answer, you could use -prop debugging=true in Additional Emulator Command Line Options and check it at runtime by means of Runtime.getRuntime().exec("getprop debugging")

Screenshot of debug configuration

Also, you might find the following question useful: Android: Release and testing mode?

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • You don't have this option for Android apps. Or am I missing something? Please show a screenshot. – Aleks G Sep 06 '12 at 09:20
  • In Eclipse, clicking on the arrow near the Debug button then choosing "Debug Confugurations..." opens a window that contains only 3 tabs: Android, Target, and Common. None of these appear to have options related to Environment variables. – TheMaster42 Sep 06 '12 at 09:33
  • You were both right, the option isn't there. I've edited the answer to provide an alternative. – Xavi López Sep 06 '12 at 09:55
  • Looks like this only works for the emulator, and not on an actual phone. I've updated the original question with the code I tried. – TheMaster42 Sep 06 '12 at 11:35
0

Ok so since command-line arguments don't really work for you; the Eclipse ones don't work for you so why not try something like this

  • put a config file in your APK's assets.
  • read the file in a static block before your variable assignment
  • assign your debug value accordingly to what you read in the file...

    private ByteArrayOutputStream getAssetFileAsByteArrayOutputStream(String filename)
    {
    AssetManager assetManager = this.getAssets();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();//  OutputStream(helper);
    try
    {
        final InputStream assetFile = assetManager.open(filename, AssetManager.ACCESS_RANDOM);
        byte readBuffer[] = new byte[1024 * 64];    //64kB
        int byteCount;
        while ((byteCount = assetFile.read(readBuffer)) > 0)
            byteStream.write(readBuffer, 0, byteCount);
        byteStream.flush();
        //          copiedFileStream.close();
    } catch (IOException e)
    {
    }
    return byteStream;
    }
    

Then before you initialize your final (const) value either put a static block

  static 
  {
       //read file
       //get the value you want
  }
  public final static Variable myVar = valFromFile ? "DEBUG" : "NORMAL";

or just move the initialization of the finalized variable to the constructor, you can initialize final vars in there.

Shark
  • 6,513
  • 3
  • 28
  • 50