How to remove all debug logging calls before building the release version of an Android app?

前端 未结 27 2026
有刺的猬
有刺的猬 2020-11-22 07:39

According to Google, I must \"deactivate any calls to Log methods in the source code\" before publishing my Android app to Google Play. Extract from section 3 of th

27条回答
  •  感动是毒
    2020-11-22 08:20

    I have improved on the solution above by providing support for different log levels and by changing the log levels automatically depending on if the code is being run on a live device or on the emulator.

    public class Log {
    
    final static int WARN = 1;
    final static int INFO = 2;
    final static int DEBUG = 3;
    final static int VERB = 4;
    
    static int LOG_LEVEL;
    
    static
    {
        if ("google_sdk".equals(Build.PRODUCT) || "sdk".equals(Build.PRODUCT)) {
            LOG_LEVEL = VERB;
        } else {
            LOG_LEVEL = INFO;
        }
    
    }
    
    
    /**
     *Error
     */
    public static void e(String tag, String string)
    {
            android.util.Log.e(tag, string);
    }
    
    /**
     * Warn
     */
    public static void w(String tag, String string)
    {
            android.util.Log.w(tag, string);
    }
    
    /**
     * Info
     */
    public static void i(String tag, String string)
    {
        if(LOG_LEVEL >= INFO)
        {
            android.util.Log.i(tag, string);
        }
    }
    
    /**
     * Debug
     */
    public static void d(String tag, String string)
    {
        if(LOG_LEVEL >= DEBUG)
        {
            android.util.Log.d(tag, string);
        }
    }
    
    /**
     * Verbose
     */
    public static void v(String tag, String string)
    {
        if(LOG_LEVEL >= VERB)
        {
            android.util.Log.v(tag, string);
        }
    }
    
    
    }
    

提交回复
热议问题