Since Android Marshmallow, developers have to request permissions the Android SDK considers dangerous, aka Dangerous Permissions.
This plugin generates a class to help with that task. It parses the Android Manifest on build and generates a helper class with the list of the Dangerous Permissions plus a few utility methods.
If your manifest contains those three permissions:
<manifest package="com.example.company" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
The plugin will generate this helper class named PermissionsHelper:
public class PermissionsHelper {
public static final String [] REQUIRED_PERMISSIONS = {
Manifest.permission.GET_ACCOUNTS,
Manifest.permission.READ_PHONE_STATE
};
(...)
}
The generated class contains a static String [] of all the dangerous permissions from the manifest.
Note that you can see here only two declared permissions as the permission android.permission.INTERNET
is a not a dangerous permission.
In your root build.gradle add the plugin, published in jCenter:
buildscript {
repositories {
jcenter()
}
dependencies {
(...)
classpath 'il.co.galex.tools.build:permissions:2.0.1'
}
}
In every Android Application or Library module, apply the plugin:
apply plugin: 'il.co.galex.permissions'
The generated class helper is named by default PermissionsHelper
and the default package name depends on variant.applicationId
.
If variant.applicationId
is for instance com.example.company
, the package name will be com.example.company.helper
.
You can change the package name and the class name to suit your needs. For instance if you prefer the 'utils' convention:
android {
(...)
}
permissions {
helperPackage android.defaultConfig.applicationId + ".util"
helperClassName "PermissionsUtils"
}
The class contains utility methods to ease the work with the System Permissions API.
public static boolean granted(Context context) {}
public static boolean granted(Context context, String... permissions){}
public static String [] filterGranted(Context context) {}
public static String [] filterGranted(Context context, String... permissions){}
public static String [] filterNotGranted(Context context) {}
public static String [] filterNotGranted(Context context, String... permissions) {}
- 2.0.1 Bug fix where the parameter "helperPackage" was not used as import
- 2.0.0 Entirely rewritten in Kotlin, yay!
- 1.1.0 Compatibility fix for the Android Gradle Plugin 3.0.0-alpha5
- 1.0.7 Compile plugin with JDK 6 to avoid a compatibility error
- 1.0.6 First release
- Get rid of templates for the code generation and use JavaPoet
- Add an option to generate a Kotlin helper class via KotlinPoet instead of the Java helper class
This plugin is available under the Apache License, Version 2.0.
(c) All rights reserved Alexander Gherschon