I have two applications.
One is declaring permission and having single Activity
:
Part of AndroidManifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:permission="your.namespace.permission.TEST" >
<activity
android:name=".DeclaringPermissionActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp"
android:host="myapp.mycompany.com" />
</intent-filter>
</activity>
</application>
The second declares that is uses permission
Part of AndroidManifest.xml
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="your.namespace.permission.TEST" />
<application
Part of Activity
:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("myapp://myapp.mycompany.com/index")));
}
I'm installing the application declaring permission, then I run the second application.
In a result I get security exception:
01-11 09:46:55.249: E/AndroidRuntime(347): java.lang.RuntimeException: Unable to start activity ComponentInfo{your.namespace2/your.namespace2.UsingPErmissionActivity}: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.VIEW dat=myapp://myapp.mycompany.com/index cmp=your.namespace/.DeclaringPermissionActivity } from ProcessRecord{407842c0 347:your.namespace2/10082} (pid=347, uid=10082) requires your.namespace.permission.TEST
I created a test code you can use it and test your permissions. There are two applications PermissionTestClient which declares permission and protects its activity with this permission. Here is its manifest file:
There is nothing special in Activity file so I will not show it here.
PermissionTestServer application calls activity from PermissionTestClient. Here is its manifest file:
And Activity:
To test it just remove uses-permission from Server application. You'll get security violation error.
Defining custom permission is done using
<Permission>
tag.. Please follow the link below to use user defined permissions in application:Declaring and Enforcing Permissions
As mentioned in the answers, you should also take into account the order you install the apps.
this is important because:
if the App that requests the permission (App B) is installed before the App that defines the permission (App A), then there will be no such defined permission in the specific device so the OS won't ask for the permission at all.
later on, when you install the App A and try to run App B, the latter will fail to access the secure component.
One workaround would be to define the same custom permission in both Apps, A and B in order to make sure that the permission exists in the device regardless of which App is installed first, so when the App A is installed, the permission will be already granted to App B.
In that case though, you should make sure that the protection level is the same in both declarations because this can lead to security risk.
(note here that from android 5.0 and on you cannot define the same permission in more than one App, except when those Apps are signed with the same signature key).
You need to create a permission in your base app's manifest by declaring it exclusively. For example:
And later make use of it in your desired app as:
Note: It is vital to maintain the order in which you install your applications with custom permissions. i.e You must need to install that app first which declares the permission and later install the one which makes use of it. Any disruption in this order may break the usage of custom. permissions.