'How to configure android logs based on build variant

I want to enable disable the logs based on build variant. e.g. I want to enable the logs in debug mode and disable the logs in release mode.



Solution 1:[1]

I find the answer. I was trying since two days. So I am writing the working solution. So others can immediate solution. Here are working solution.

first way.

  1. create a prop file inside app. e.g. proguard-disabling-logs.pro add below code in that. -assumenosideeffects class android.util.Log{*;}

  2. go to your app level gradle file. buildTypes { release {
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-disabling-logs.pro','proguard-rules.txt' } } } debug {
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } }

This will disable logs in release mode and enable the logs in debug mode.

Second way:

  1. go to app level gradle file add below code. buildTypes { release {
    buildConfigField "Boolean", "isLogEnabled", "false"
    } debug { buildConfigField "Boolean", "isLogEnabled", "true"
    } }

isLogEnabled is custom variable, you can rename whatever you want. Only add in existing code. Don't remove existing code inside debug/release block. 2. Build the code. It will create a isLogEnabled field inside BuildConfig class. 3. Now keep condition before log. e.g. if(BuildConfig.isLogEnabled) Log.d(TAG,"test")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sudhanshu