Manifest Permission
Application Development
Android Permissions
App Security
Mobile Development

How to add manifest permission to an application?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Adding manifest permissions to an application is a critical step in application development, especially for mobile platforms like Android. Permissions dictate what resources or data an application can access on a device. This ensures that users have control over which aspects of their device are used by apps they install. In this article, we'll delve into how to add manifest permissions, emphasizing Android applications, but many concepts apply universally across platforms.

Understanding Permissions

Permissions are categorizations that define what an application is allowed to access or perform on the operating system. For example, an application might need permission to access the camera, read contacts, or use the internet. These permissions need to be declared in the app’s manifest file before requesting them at runtime.

Types of Permissions

  1. Normal Permissions: These are permissions that have minimal risk to user privacy and system functionality. For instance, accessing the internet.
  2. Dangerous Permissions: These include permissions that can affect user data and privacy, like accessing contacts or camera.
  3. Signature Permissions: These are granted if the app is signed with the same certificate as the app that declares the permission.

Adding Permissions to Android Manifest

The Android Manifest file (AndroidManifest.xml) is a crucial file where permissions are declared. Let's go through the steps to add permissions.

Step-by-Step Process

  1. Open AndroidManifest.xml: This file is typically located in the app/src/main/ directory of your Android project.
  2. Declare Permissions: Use the <uses-permission> element to declare the required permission. Here’s how you can add internet access permission:
xml
1   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2       package="com.example.app">
3       
4       <uses-permission android:name="android.permission.INTERNET"/>
5       
6       <application
7           android:allowBackup="true"
8           android:icon="@mipmap/ic_launcher"
9           android:label="@string/app_name"
10           android:roundIcon="@mipmap/ic_launcher_round"
11           android:supportsRtl="true"
12           android:theme="@style/Theme.App">
13           
14           <!-- Your activities go here -->
15
16       </application>
17   </manifest>
  1. Multiple Permissions: You can declare multiple permissions by adding more <uses-permission> elements.
xml
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
   <uses-permission android:name="android.permission.READ_CONTACTS"/>
  1. Permissions at Runtime: For Android 6.0 (API level 23) and above, permissions, especially dangerous ones, must be requested at runtime.

Requesting Runtime Permissions

When dealing with dangerous permissions, you should handle them at runtime. Here's a basic example in Java to request a permission:

java
1if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
2        != PackageManager.PERMISSION_GRANTED) {
3    // Permission is not granted
4    // Should we show an explanation?
5    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
6            Manifest.permission.READ_CONTACTS)) {
7        // Show an explanation to the user
8    } else {
9        // No explanation needed; request the permission
10        ActivityCompat.requestPermissions(this,
11                new String[]{Manifest.permission.READ_CONTACTS},
12                MY_PERMISSIONS_REQUEST_READ_CONTACTS);
13    }
14}

Key Considerations

  • User Experience: Always explain why permission is necessary to the user to ensure transparency and better acceptance.
  • Testing: Test your application thoroughly to handle cases where permissions are not granted.
  • Security: Request only the permissions that are absolutely necessary for the application’s functionality.
  • OS Version Differences: Remember that newer versions of Android may introduce changes to how permissions are handled.

Summary Table

Permission TypeDescriptionExample
Normal PermissionsLow risk, automatically granted.INTERNET
Dangerous PermissionsHigh risk, need user approval at runtime.READ_CONTACTS
Signature PermissionsGranted if app is signed with the same certificate.Not commonly used in public apps
Runtime RequestEnsures user is prompted to grant permission at runtime.READ_CONTACTS for API Level >= 23

Conclusion

Permission management is an integral part of application security and privacy. By properly declaring and requesting permissions, developers can build applications that are respectful of user data and robust in terms of security. Understanding the nuances of permissions helps create a smoother and more trustworthy user experience.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.