2

How do you differentiate between release and debug builds in your Android development taking into consideration you can't rely on BuildConfig.DEBUG flag?

I'm using Android Studio with Gradle and prefer to produce release/debug builds using Gradle tasks, I also need it to produce those on TeamCity.

For example, I need to use different String values for release and debug. What I'm going to do is to store them in main and debug folders separately so the proper one is picked up depending on the build. This is kind of okay, but what if I need to have a flag in if/else to decide which way to go?

Community
  • 1
  • 1
Eugene
  • 59,186
  • 91
  • 226
  • 333

3 Answers3

0

add in your build.gradle a suffix to debug versions

android {
    signingConfigs {
        buildTypes {
            debug {
               versionNameSuffix = "-DEBUG"
            }
        }
    }
}

and then on runtime you check the version name:

     PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
     mBuildVersion = pInfo.versionName;
     if(mBuildVersion.contains("DEBUG")){
          // DEBUG
     }
Budius
  • 39,391
  • 16
  • 102
  • 144
0

You can check if your application is debuggable by something like this

boolean isDebuggable = (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));
Dmitry
  • 864
  • 7
  • 15
  • 1
    Please avoid posting code only answers. There is no explanation of what this code does and as such your answer is potentially liable to removal. – arco444 Oct 28 '14 at 10:28
0

You can check if obfuscation has been made:

package com.example.customandroid;

public abstract class DebugBuildChecker {
    public static boolean isDebugBuild() {
        //Log.d("~~~","class="+DebugBuildChecker.class.getName());
        return DebugBuildChecker.class.getName().endsWith(".DebugBuildChecker");
    }
}

After obfuscation, the class name will be something like com.example.a.a and the function will return false. Of course, one can tell proguard to keep the name of this class; just don't do it, and the check will work.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127