primers
Recently, I have been conducting a research on cross-app data sharing. One of the research solutions is to realize cross-process data communication through ContentProvider. The demo can run normally on Android 10 mobile phones. When I use a mobile phone running Android11, I am directly prompted to find the corresponding ContentProvider.
Let’s get our heads together and canvass.
Check to see if the ContentProvider configuration has changed.
/ / the provider statement
<provider
android:authorities="com.dsw.ant.provider"
android:exported="true"
android:enabled="true"
android:writePermission="com.dsw.ant.writer"
android:name=".MyProvider">
</provider>
// The usage mode
ContentValues contentValues = new ContentValues();
contentValues.put("time", System.currentTimeMillis());
getContentResolver().insert(Uri.parse("content://com.dsw.ant.provider/data"), contentValues);
Copy the code
The Android: Exported property of the ContentProvider is configured. The only difference now is the system changes on Android11, so check out the update logs on Android11.
Because it has to do with storage, so I’m going to go straight to itThe storage mechanism in Android 11 has been updated, partition storage access level:
After a search, in the introduction of this part on the official website, ContentProvider is still recommended for data sharing across processes, which indicates that this is not the problem. I continue to look for the changes. The visibility of software packages in Android 11 has become the direction of my next investigation.
Package visibility in Android 11
The official introduction to package visibility:
Android 11 changes how apps query and interact with other apps that users have installed on the device. Using the
element, an application can define a set of other packages that it can access. This element helps encourage the minimum permission principle by telling the system what other packages to show your application. In addition, this element can help app stores such as Google Play evaluate the privacy and security that an app provides to users.
If your app is targeting Android 11 or higher, you may need to add the
element to your app’s manifest file. In the < Queries > element, you can specify packages by package name, intent signature, or provider authorization.
Android11 has changed the way apps interact with each other. You need to use the
tag in the Manifest to declare which apps you want to access.
In A demo, the package name of B demo was declared by
.
<queries>
<package android:name="com.dsw.ant.demo" />
</queries>
Copy the code
The operation is now working properly.
Note:<queries>The element needs to be used in Android Gradle Plugin 4.0.0 or later.Copy the code
Problem solved and that’s it? Too young to Simple, how can I stop there?
Software package visibility in Android 11
When we create an App, we need to consider the interactive access with other apps, such as access to system-level albums, phone, etc. In Android 10 and below, as long as the App is open and accessible to other apps, it can be directly accessed. For example, in Android 10 and earlier, an App can use the queryIntentActivities() method to get a list of all installed apps on the device. This is insecure and goes beyond what an App actually needs, so package addability was introduced in Android 11 to limit this. With the
element in Android11, applications can define a set of other packages that are themselves accessible.
1. Which applications can be directly accessed by other applications
Android11 for the App that meets some conditions do not need to add the
element can be accessed by other apps, mainly used to provide basic functions and common usage.
App
Access your own- Some systems
App
Media providers, for exampleAndroid
The core functions of. Learn more about how to determine which packages are automatically displayed on the device where your application is running. - use
startActivityForResult()
Pull up yourApp
的App
- Bind you
App
In theService
的App
- Anything that gets you
App
In theContentProvider
的App
- When you are
App
In theContentProvider
Set up theandroid:grantUriPermissionTime can be takenApp
access
2. Check which devices existApp
Be accessible
Usually, system applications can be directly accessed by other apps. We can view directly accessed apps on a device through the following commands.
adb shell dumpsys package queries
Copy the code
3. queries
The use of
The
tag specifies the name of the App you want to interact with. In the
element, you can specify the software package by its name, intent signature, or provider authorization.
Specify the package name
<manifest package="com.example.game">
<queries>
<package android:name="com.example.store" />
<package android:name="com.example.services" />
</queries>.</manifest>
Copy the code
The specifiedIntent
<manifest package="com.example.game">
<queries>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="image/jpeg" />
</intent>
</queries>.</manifest>
Copy the code
The specifiedProvider
<manifest package="com.example.suite.enterprise">
<queries>
<provider android:authorities="com.example.settings.files" />
</queries>.</manifest>
Copy the code
4. Interact with all apps
In rare cases where your App needs to interact with all apps installed on the device, you can do this by declaring QUERY_ALL_PACKAGES permissions.
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
Copy the code
The biggest feeling is to keep an eye on the feature descriptions that Android releases, although sometimes it takes a bit of digging to figure out what each description is for.
Reference:
- The storage mechanism in Android 11 has been updated
- Preparing your Gradle build for package visibility in Android 11