Reputation: 150
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:
Upvotes: 3
Views: 2000
Reputation: 6620
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
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.
Upvotes: 0
Reputation: 27880
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")
Also, you might find the following question useful: Android: Release and testing mode?
Upvotes: 1
Reputation: 20936
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;
}
Upvotes: 4