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

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 !

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"
}
}

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"
}
}

Related

How configure a different buildType for a flavor with gradle

I have two different flavor's, RQT & PRD.
And for one of them, I want to disable proguard configuration (for RQT, disable proguard for both buildTypes).
Like add conditioning ? Is something like that is possible ?
android {
...
flavorDimensions("server")
productFlavors {
rqt {
dimension "server"
applicationIdSuffix ".rqt"
}
prd {
dimension "server"
applicationIdSuffix ".prd"
}
}
...
buildTypes {
debug {
(__if (flavor != RQT) then do proguard config...__)
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
proguardFile 'proguard-debug-project.txt'
}
release {
(__if (flavor != RQT) then do proguard config...__)
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
proguardFile 'proguard-release-project.txt'
}
}
...
}
You could create additional Build Types, e.g. debugNoProguard, releaseNoProguard, etc.
android {
variantFilter { variant ->
def needed = variant.name in [
'rqtDebugNoProguard',
'rqtReleaseNoProguard',
'prdDebug',
'prdRelease'
]
variant.setIgnore(!needed)
}
buildTypes {
debug {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
proguardFile 'proguard-debug-project.txt'
}
debugNoProguard {
debuggable true
minifyEnabled false
signingConfig signingConfigs.debug
}
....
}
}
Then instead of building rqtDebug you would build the variant rqtDebugNoProguard or prdDebugNoProguard when you wanted to run a debug build without ProGuard.

product flavors producing incorrect build variants

flavorDimensions 'publish', 'develop'
productFlavors {
live {
dimension 'publish'
}
dev {
dimension 'develop'
applicationIdSuffix ".dev"
}
}
buildTypes {
debug {
minifyEnabled false
shrinkResources false
zipAlignEnabled true
debuggable true
productFlavors.live.signingConfig buildSignConfig.debug
productFlavors.dev.signingConfig buildSignConfig.debug
}
release {
minifyEnabled true
shrinkResources true
zipAlignEnabled true
debuggable false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
productFlavors.live.signingConfig buildSignConfig.release
productFlavors.dev.signingConfig buildSignConfig.release
}
}
I have 2 library modules and in application gradle file, I have above flavorDomensions, productFlavors and buildTypes. This produces build variant incorrectly like liveDevDedug and liveDevRelease. What could be the possible errors? Using AS 3.0

Assign different build types to different Flavors in android

In my Gradle file, I've assigned buildTypes and productFlavors in the following way.
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
signingConfigs {
release{
//storeFile file("")
//storePassword ""
//keyAlias ""
//keyPassword ""
}
}
flavorDimensions "default"
productFlavors {
dev{
dimension "default"
applicationId "com.xxx.android.dev"
}
qa{
dimension "default"
applicationId "com.xxx.android.qa"
}
staging{
dimension "default"
applicationId "com.xxx.android.staging"
}
prod{
dimension "default"
applicationId "com.xxx.android.prod"
}
}
Now the problem is each flavors has the both build types which is release and debug , But I want to assign debug for build only so we cannot produce a release build when selecting debug variant. Any idea how to do it?
I managed to get it done by adding build targets. i didn't want to change any source codes for this . so no need to go with flavors
buildTypes {
qa {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField 'String', ‘BASE_URL', ‘"url“'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField 'String', ‘BASE_URL', ‘"url“'
}
debug {
buildConfigField 'String', ‘BASE_URL', ‘"url“'
}
}

Enable proguard in flavor

I'm using TeamCity to build version of application and upload it to HockeyApp. I want to enable proguard only on specific flavor and when building is on teamcity and uploading on HockeyApp, is it possible? Right now I defined variable in gradle file:
def runProguard = false
and set it in my flavors to false or true and then in build type I have:
if (project.hasProperty('teamcity') && runProguard.toBoolean()) {
minifyEnabled true
} else {
minifyEnabled false
}
but it doesn't work on teamcity and I have version without proguard on HockeyApp. How to fix it? Is it another way to do it for example defining gradle task with enabled proguard?
You should do something like this to achieve what you want:
android {
buildTypes {
debug {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-debug.pro'
}
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
mock {
initWith(buildTypes.release)
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
pro {
applicationId = 'com.example.app.pro'
}
free {
applicationId = 'com.example.app.free'
}
}
Also you can set some environment variable in teamcity and check if the build is happening on ci or it's on local machine:
if (!System.getenv("CI")) {
//do something on local machine
} else {
// do something on ci server
}

Gradle Android: Choose SignConfig according to BuildType

I want to add an if or something similar to my gradle build script, that decides wether to sign my App with the Debug-Key or Release Key accodring to what buildType I'm using. I can't do that in the buildTypes section, because if have different release keys for my product flavours. I tried it with something like buildType.name, but gradle does not know this property.
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.txt'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.txt'
}
}
signingConfigs {
app1 {
....
}
app2 {
....
}
}
productFlavors {
app1 {
applicationId "com.test.app1"
}
app2 {
applicationId "com.test.app2"
if (buildType.name == 'release'){
signingConfig signingConfigs.app2
}
}
}
Edit: Ok I managed to solve it via gradle.properties: I add a property and set it true or false in buildtypes, and an if in the productFlavour add the signConfig if true.

Categories

Resources