How to get Android app to automatically adjust w / debug vs release values?

I am working on an Android application, in particular with the Android Android SDK. In development mode, I work with a test application Facebook, which comes with the same ID. However, in release mode, the application will work with the second Facebook application with a different identifier.

I wonder how most Android (or Java may be a pretty good area of ​​expertise), the developers here want their application to be automatically created using debug and release values. An ideal solution does not require manual switching (for example: switching public static final DEBUG = false; to true ).

+4
source share
6 answers

It has been a while since you asked, but I thought I would share how I do it.

Like Sebastian’s hint, Ant script can handle this change for you and generate the constant final constants you are looking for. You can customize IntelliJ or Eclipse to make it almost seamless.

I tried to describe in detail the various steps that I have taken here , let me know if this helps. I know that I should never make any manual changes before letting go, and this is a pleasant relief!

+9
source

In eclipse ADT 17.0 and later, there is a new feature for this. Check out the BuildConfig.DEBUG, which is automatically generated with your code.

For more information see http://developer.android.com/sdk/eclipse-adt.html

+6
source

I cannot recommend the IMEI method ... the main problem is that not all Android devices will have IMEI. It is best to verify the signature used to sign the .apk.

 // See if we're a debug or a release build try { PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES); if (packageInfo.signatures.length>0) { String signature = new String(packageInfo.signatures[0].toByteArray()); isReleaseBuild = !signature.contains("Android Debug"); } } catch (NameNotFoundException e1) { e1.printStackTrace(); } 
+5
source

I use a slightly more mundane method (if you are still interested in solutions).

When the application starts, my application checks for the text file stored in / sdcard /. Every application I am looking for looks for a specific file, for example, "applicationdebug.txt". If the file exists, the application goes into debug mode and begins to be verbose with log operators and using my Facebook debug key, etc.

Then I simply delete (or rename) the file on the device to see how the application works in release mode.

+1
source

Usually you use only 1 or 2 devices for debugging. So, can you set the DEBUG switch based on the Device? So you can just use IMEI.

  • add a new Application class to your project and initialize this field (presumably to put it in the Const class).

    In your application onCreate method, call Const.setupDebug(getApplicationContext());

  • Deploy setupDebug like this

    public class Const {

     private static boolean debug = false; public static boolean isDebug() { return debug; } private static void setDebug(boolean debug) { Const.debug = debug; } private static String [] DEBUG_DEVICES = new String[] { "000000000000000", "gfjdhsgfhjsdg" // add ur devices }; public static void setupDebug(Context context) { Arrays.sort(DEBUG_DEVICES); 
     TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); String imei = mTelephonyMgr.getDeviceId(); if (imei == null) imei = "000000000000000"; if(Arrays.binarySearch(DEBUG_DEVICES, imei) > -1) { setDebug(true); } 
    } >

    }

  • Switching from a constant field to a constant.

    Const.isDebug ()

0
source

With Eclipse, I create 3 projects in the workspace:

  • ApplicationProject

This is a library project. Contains all the source code. I add /refs.xml

 <bool name="debug_mode">true</bool> 
  • ApplicationProjectDEBUG

Use ApplicationProject Overide AndroidManifest and another XML file with a specific development configuration In the /refs.xml values ​​I add

 <bool name="debug_mode">true</bool> 
  • ApplicationProjectPROD

Use ApplicationProject Overide AndroidManifest and another XML file with a specific production configuration In the values ​​/refs.xml add

 <bool name="debug_mode">false</bool> 

I am signing an APK from this project to put in the store

0
source

All Articles