Skip to content

Instantly share code, notes, and snippets.

@rahulk11
Last active May 19, 2018 23:35
Show Gist options
  • Save rahulk11/cadfd14809c21d37d75c7d9b8fb3a184 to your computer and use it in GitHub Desktop.
Save rahulk11/cadfd14809c21d37d75c7d9b8fb3a184 to your computer and use it in GitHub Desktop.
Add permissions to android project's manifest on build time
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.rahulkr.buildtimepermissions">
<!--Below commented tags are necessary because we are using regex to find and replace permissions-->
<!-- START:PERMISSIONS -->
<uses-permission android:name="android.permission.BATTERY_STATS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- END:PERMISSIONS -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
import java.util.regex.Matcher;
import java.util.regex.Pattern;
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "in.rahulkr.buildtimepermissions"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// products {
// android
// }
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.processManifest.doLast {
// 1. read the file permissions.json which contains a list of required permissions
// 2. parse the permissions from json to create a string containing permissions
// as they should appear in manifest
// 3. read the manifest file for each variant
// 4. match manifest content against the regex
// 5. replace the match with string from step 2
// 6. write the new content to manifest file
def inputFile = new File("app/permissions.json")
def json = new groovy.json.JsonSlurper().parseText(inputFile.text)
def permissions = "\n<!-- START:PERMISSIONS -->"
json.permissions.each{permissions+="\n<uses-permission android:name=\""+it.name+"\"/>"}
permissions+="\n<!-- END:PERMISSIONS -->"
def manifestFile = output.processManifest.manifestOutputFile
// If you get below error on build time, it's probably because your gradle version is newer than mine
// Error: Manifest Tasks does not support the manifestOutputFile property any more, please use the manifestOutputDirectory instead.
// comment above line which defines 'manifestFile' and uncomment below line
// def manifestFile = file("$manifestOutputDirectory/AndroidManifest.xml")
def content = manifestFile.getText()
def pattern = Pattern.compile("<!--\\sSTART:PERMISSIONS\\s-->.*<!--\\sEND:PERMISSIONS\\s-->", Pattern.DOTALL);
content = pattern.matcher(content).replaceAll(permissions)
manifestFile.write(content)
}
// variant.processResources.manifestFile = file("${buildDir}/filtered_manifests/${variant.dirName}/AndroidManifest.xml")
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.+'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
testCompile 'junit:junit:4.12'
}
{
"permissions": [
{"name": "android.permission.WRITE_EXTERNAL_STORAGE", "optional":"true", "description":""},
{"name": "android.permission.ACCESS_COARSE_LOCATION", "optional":"true", "description":""},
{"name": "android.permission.READ_EXTERNAL_STORAGE", "optional":"true", "description":""},
{"name": "android.permission.ACCESS_NETWORK_STATE", "optional":"true", "description":""},
{"name": "android.permission.CAMERA", "optional":"true", "description":""},
{"name": "android.permission.INTERNET", "optional":"true", "description":""},
{"name": "android.permission.BLUETOOTH", "optional":"true", "description":""}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment