Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: Check for a custom build type

Tags:

I am trying to have a piece of code detect a BuildType, but I am a bit stuck. Whenever I type the code in for the IF statement, it says

Incompatible types. Required: Boolean. Found: java.lang.String

When I would have thought that it would have to be a string if there was .toString() at the end.

My code for detecting it is:

String buildtype = BuildConfig.BUILD_TYPE.toString(); if (buildtype = "admin") {     //Do some admin stuff here. } 

I have set up the admin BuildType in my build.gradle file like this:

admin {         debuggable true         jniDebuggable false         renderscriptDebuggable false         minifyEnabled false         zipAlignEnabled true     } 

Any help is greatly appreciated. Thanks

like image 886
Cowboy433 Avatar asked Mar 19 '16 17:03

Cowboy433


People also ask

How do I know my build type?

My code for detecting it is: String buildtype = BuildConfig. BUILD_TYPE.

How do I get the current build type in Gradle?

In your build. gradle (:app): tasks. all { Task task -> if (task.name == "preDebugBuild") { doFirst { //for debug build } } else if (task.name == "preReleaseBuild") { doFirst { //for release build } } } dependencies { ... }

Is a build type in Gradle?

By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.

What is Android build type?

Build Type refers to build and packaging settings like signing configuration for a project. For example, debug and release build types. The debug will use android debug certificate for packaging the APK file. While, release build type will use user-defined release certificate for signing and packaging the APK.


2 Answers

What you can use in case you want to go for a custom build type and not a product flavor is:

if (BuildConfig.BUILD_TYPE.contentEquals("admin")) {    // Do things related to the admin build type. } 
like image 142
Mahmoud Abou-Eita Avatar answered Sep 17 '22 14:09

Mahmoud Abou-Eita


You can look at your BuildConfig file. It is the file you will get after creating a build.

For your question. I think you should use BuildConfig.FLAVOR instead of BuildConfig.BUILD_TYPE. And remember their type are String, so don't need to convert to String with .toString()

Lastly, you should use string compare method. So, your code should be

if (BuildConfig.FLAVOR.contentEquals("admin")) {     //Do some admin stuff here. } 
like image 25
Pi Vincii Avatar answered Sep 20 '22 14:09

Pi Vincii