Reputation: 113
I was reading about declaring permissions in activity . According to the documentation
You can use the manifest's tag to control which apps can start a particular activity. A parent activity cannot launch a child activity unless both activities have the same permissions in their manifest. If you declare a element for a particular activity, the calling activity must have a matching element.
To try this out, I created 2 sample Apps. First App will try to directly launch an activity of the second App, using an explicit intent, Also, the Second App will declare a permission for the particular activity which I'm launching from first App.
These are the steps I followed
<uses-permission android:name="permission.SHARE_POST"/>
in the Manifest
of senderNow , from a button click of Sender App, I'm calling Receivers Activity called ShareActivity
as follows
Intent intent = new Intent();
intent.setComponent(new ComponentName("basics.android.com.androidbasics","basics.android.com.androidbasics.ShareActivity"));
startActivity(intent);
NOTE: basics.android.com.androidbasics
is the package name of the receiver
Below given is the activity declaration in Second App's (Receiver) Manifest
<activity
android:name=".ShareActivity"
android:exported="true"
android:permission="permission.SHARE_POST"/>
Now, when I run both the Apps, and try to lauch ShareActivity
from sender, I get the following error
Caused by: java.lang.SecurityException: Permission Denial: starting Intent { cmp=basics.android.com.androidbasics/.ShareActivity } from ProcessRecord{e09a1fc 26267:sender.android.com.sender/u0a925} (pid=26267, uid=10925) requires permission.SHARE_POST
Seems like the sender does't have the permission permission.SHARE_POST
yet. But I have already declared it in the manifest of sender.
Whats happening here?
Upvotes: 2
Views: 776
Reputation: 1006744
Using custom permissions is a fairly advanced thing to do in Android. The basic recipe is:
permission.SHARE_POST
is not a good choice — add a prefix that is tied to your domain name or whatever else it is that you are using as the basis for your apps' applicationId
values.<permission>
element, with an android:name
attribute holding the permission name from step #1. Optionally, give it an android:protectionLevel
attribute (e.g., signature
, so only apps signed by the same signing key can work together).android:permission
attribute on the component (e.g., <activity>
), with a value of your permission name from step #1.<uses-permission>
attribute, with an android:name
attribute holding the permission name from step #1.minSdkVersion
to 21, as there are security problems with custom permissions on older versions.This will work, if the defender (step #2 and #3) will always be installed before the client (step #4). If you want the apps to be installable in either order, replace step #2 from above with:
<permission>
element, with an android:name
attribute holding the permission name from step #1. Optionally, give it an android:protectionLevel
attribute (e.g., signature
, so only apps signed by the same signing key can work together). Also, ensure that both apps are always signed by the same signing key, as otherwise they cannot both define the same permission.Upvotes: 2