开发者

How to make Android app automatically configure w/ debug vs. release values?

I'm working on an Android app, specifically one t开发者_开发百科hat uses the Facebook Android SDK. In development mode, I'm working with a test Facebook app that goes by one ID. However, in release mode, the app will be working with second Facebook app with a different ID.

I'm wondering how most Android (or Java might be a suitable enough realm of knowledge) developers here go about having their app automatically build with debug vs. release values. An ideal solution does not involve a manual switch (e.g.: switching public static final DEBUG = false; to true) before building.


It's been a while since you asked but I thought I'd share how I'm doing it.

Like Sebastian hinted, an Ant script can handle that change for you and generate the static final constants that you're looking for. You can configure IntelliJ or Eclipse to make it almost seamless.

I tried to detail the different steps I took over here, let me know if it helps. I know I never have to make any manual changes before releasing, and it's a nice relief!


In eclipse ADT 17.0 and above there is a new feature for this. Check the BuildConfig.DEBUG that is automatically built with your code.

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


I can't recommend the IMEI method... the main problem with it is that not all Android devices will have IMEIs. A better way is to examine 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();
}


I use a slightly more mundane method (in case you're still interested in solutions).

At application launch my application checks for the existence of a text file stored in /sdcard/. Each application I have looks for a specific file like "applicationdebug.txt". If the file exists, then the application goes into debug mode and starts being verbose with log statements and using my debug Facebook key, etc.

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


Usually you will use 1 or 2 devices for debugging only. So you can set the DEBUG switch based on the Devices? So you can simply use the IMEI.

  1. add a new Application class to your project and have it initialize the field (suspect to put it in a Const class).

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

  2. Implement the 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);
    }
    
    }

    }

  3. Switch from constant field to constant Method.

    Const.isDebug()


With Eclipse I create 3 projects in the workspace :

  • ApplicationProject

It is a library project Contain all source code In values/refs.xml I add

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

Use ApplicationProject Overide AndroidManifest and other xml file with developement specific config In values/refs.xml I add

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

Use ApplicationProject Overide AndroidManifest and other xml file with production specific config In values/refs.xml I add

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

I signe APK from this project to put on the store

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜