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

前端 未结 6 2156
情书的邮戳
情书的邮戳 2021-02-10 13:54

I\'m working on an Android app, specifically one that uses the Facebook Android SDK. In development mode, I\'m working with a test Facebook app that goes by one ID. However, i

6条回答
  •  梦如初夏
    2021-02-10 14:31

    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()

提交回复
热议问题