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

2

14

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 ?

Concinnity answered 2/5, 2017 at 7:49 Comment(5)
you can check your library's build.gradle to detect if its debug or not but how are you going to check the consumer's build.gradle?Platinumblond
@KostasDrakonakis that's exactly the problem :)Concinnity
@FarhadFaghihi but what is the reason? The important point is that your library is not in debug. Everyone develop the app in debug mode.Taryntaryne
@GabrieleMariotti this is a security check for the release of the consumer application, which is going to be distributed to end users.Concinnity
You can get the value in buildconfig like this getApplicationContext().getResources().getString("BuildConfig Res Value")Kandrakandy
P
8

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!!!

Platinumblond answered 2/5, 2017 at 8:5 Comment(2)
seems legit. I'll try and let you know of the result.Concinnity
This will not work and return null if minifyEnabled is set to true in app's build.gradle. This is not a recommended method to use in a library.Chukar
A
12

Check debuggable tag on AndroidManifest file is the better way:

public static boolean isDebuggable(Context context) {
    return ((context.getApplicationInfo().flags 
            & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
}
Apomict answered 7/3, 2020 at 14:30 Comment(0)
P
8

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!!!

Platinumblond answered 2/5, 2017 at 8:5 Comment(2)
seems legit. I'll try and let you know of the result.Concinnity
This will not work and return null if minifyEnabled is set to true in app's build.gradle. This is not a recommended method to use in a library.Chukar

© 2022 - 2024 — McMap. All rights reserved.