Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check debuggable or debug build type in android library?

I have an Android AAR library. One security policy I want to impose on the consumer application of my library, is that it must not be able to use my library when debuggable is true or the apk is created using the debug buildType.

How can I check this programmatically in android ?

like image 967
Farhad Faghihi Avatar asked May 02 '17 07:05

Farhad Faghihi


People also ask

How do I know if I have debug build Android?

If you're running debug build BuildConfig. BUILD_TYPE. equals("debug"); returns true.

How do I know if my build is debug or release?

Android Debug Vs Release Build Check in Running Code - If you do, then the tools will automatically insert android:debuggable=true when building an APK to debug on an emulator or device. And when you perform a release build, such as Exporting APK, it will automatically set it to false.

What is build type in Gradle in Android?

Build types define certain properties that Gradle uses when building and packaging your app, and are typically configured for different stages of your development lifecycle. There are two build types defined by default, debug and release , and you can customize them and create additional build types.

How do you check if APK is debug or release programmatically?

Open your command prompt, write below line of code and execute. If you find this output, then its a debug build as it is written “CN=Android Debug”.


2 Answers

Check debuggable tag on AndroidManifest file is the better way:

public static boolean isDebuggable(Context context) {
    return ((context.getApplicationInfo().flags 
            & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
}
like image 120
duyhungws Avatar answered Sep 23 '22 11:09

duyhungws


There is a workaround with reflection in order to get the project's(not library's) BuildConfig value like this:

/**
 * Gets a field from the project's BuildConfig. This is useful when, for example, flavors
 * are used at the project level to set custom fields.
 * @param context       Used to find the correct file
 * @param fieldName     The name of the field-to-access
 * @return              The value of the field, or {@code null} if the field is not found.
 */
public static Object getBuildConfigValue(Context context, String fieldName) {
    try {
        Class<?> clazz = Class.forName(context.getPackageName() + ".BuildConfig");
        Field field = clazz.getField(fieldName);
        return field.get(null);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}

To get the DEBUG field, for example, just call this from library Activity:

boolean debug = (Boolean) getBuildConfigValue(this, "DEBUG");

I have not tried this yet and cannot guarantee it will work all the time but you can go ahead!!!

like image 20
matrix Avatar answered Sep 19 '22 11:09

matrix