Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to set a gradle value from a know enum

Tags:

android

gradle

I use Logger library in my development, and I configure it in my Application class:

@Override
public void onCreate() {
    super.onCreate();
    sInstance = this;

    Logger.init(BuildConfig.LOGGER_TAG_NAME)
            //.setMethodCount(3)            // default 2
            //.hideThreadInfo()             // default shown
            .setLogLevel(LogLevel.NONE);  // default LogLevel.FULL

LogLevel is an enum (in Logger library).

But I want automatically to set the log level according my gradle build type; to do something like that:

buildTypes {

    debug {
        debuggable true

        buildConfigField "enum", "LOGGER_LEVEL", LogLevel.FULL
    }

    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        buildConfigField "enum", "LOGGER_LEVEL", LogLevel.NONE
    }
}

then:

Logger.init(BuildConfig.LOGGER_TAG_NAME)
            //.setMethodCount(3)            // default 2
            //.hideThreadInfo()             // default shown
            .setLogLevel(BuildConfig.LOGGER_LEVEL);  // default LogLevel.FULL

But it doesn't work:

Error:(31, 0) No such property: NONE for class: org.gradle.api.logging.LogLevel

It's the same with FULL enum value.

Thanks for your help guys !

like image 791
anthony Avatar asked Apr 21 '15 18:04

anthony


2 Answers

You must include the package and class name in both, property type and value:

buildTypes {
    debug {
        debuggable true
        buildConfigField "com.orhanobut.logger.LogLevel", "LOGGER_LEVEL", "com.orhanobut.logger.LogLevel.FULL"
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "com.orhanobut.logger.LogLevel", "LOGGER_LEVEL", "com.orhanobut.logger.LogLevel.NONE"
    }
}
like image 154
Gero Avatar answered Nov 18 '22 22:11

Gero


To avoid having to repeat the package name and to make the lines shorter, you can define some variable, like:

def LogLevel = "com.orhanobut.logger.LogLevel"

buildTypes {
    debug {
        debuggable true
        buildConfigField LogLevel, "LOGGER_LEVEL", "${LogLevel}.FULL"
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField LogLevel, "LOGGER_LEVEL", "${LogLevel}.NONE"
    }
}
like image 2
arekolek Avatar answered Nov 18 '22 22:11

arekolek