Release-Debug Builds for Android Application

前端 未结 5 756
一生所求
一生所求 2021-01-04 21:22

In C++ I would normally setup 2 builds - debug and release with each having DEBUG and RELEASE predefined respectively. I would then use these defin

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 21:59

    If you are running the application from Eclipse, it will always be a debug.

    When you export the application (Android Tools -> Export (un)signed Application Package)

    If you want to know dynamically if its release or debug, you can use BuildConfig.DEBUG (Its located in the gen folder, I don't know if this is supported by all the API levels)

    Like as followed:

    if (BuildConfig.DEBUG) {
        Log.d(TAG, "Text");
    }
    

    If you look at the generated bytecodes you will see the following (In debug mode):

    public class Sample{
    
        private static final boolean LOG_ENABLED = true;
    
        public static void main(String args[]){
            if (BuildConfig.DEBUG){
                System.out.println("Hello World");
            }
        }
    }
    

    Produces the following bytecodes:

    public class Sample extends java.lang.Object{
        public Sample();
          Code:
           0:   aload_0
           1:   invokespecial   #1; //Method java/lang/Object."":()V
           4:   return
    
        public static void main(java.lang.String[]);
          Code:
           0:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
           3:   ldc #3; //String Hello World
           5:   invokevirtual   #4; //Method Java/io/PrintStream.println(Ljava/lang/String;)V
           8:   return
    
    }
    

    And if the BuildConfig.DEBUG is false

    public class Sample extends java.lang.Object{
        public Sample();
          Code:
           0:   aload_0
           1:   invokespecial   #1; //Method java/lang/Object."":()V
           4:   return
    
        public static void main(java.lang.String[]);
          Code:
           0:   return
    }
    

提交回复
热议问题