How to check type of debug or debug in android library?

I have an Android AAR library. One security policy that I want to apply to my library’s consumer application is that it cannot use my library when debuggable is true or apk is created using debug buildType.

How can I check this programmatically in android?

+5
source share
1 answer

There is a workaround with reflection to get the BuildConfig value of the project (not the library):

 /** * Gets a field from the project 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 the Activity library:

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

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

+5
source

All Articles