Add system-level permissions android:sharedUserId="android.uid.system"

When we add system-level permissions to the project, there will be an error message at runtime as follows:

Installation did not succeed.
The application could not be installed: INSTALL_FAILED_SHARED_USER_INCOMPATIBLE

The reason why the error is reported is that after adding system-level permissions, the platform key of the target system is required to re-sign the apk file. The signature file I use here is found from the target system, not generated by myself. (The target system is the Android system used by the Android device where your apk will be installed.)
Here we take the android.uid.system permission as an example to illustrate how the Android application we developed obtains this permission.

1. Add system-level permissions to the Android project

1. Add system-level permissions

Add sharedUserId attribute in AndroidManifest.xml

android:sharedUserId="android.uid.system"

insert image description here

2. Add the existing signature to build.gradle

Add storePassword (keystore key), keyAlias ​​(key identification name), keyPassword (key password) of the signature file

android {
    
    
    signingConfigs {
    
    
        platform {
    
    
            storeFile file('D:\\aaa.keystore')
            storePassword '123456'
            keyAlias 'aaa'
            keyPassword '123456'
        }
    }
}

3. Android Studio选择File->Project Structure->Modules

In Signing Configs, you can see that the platform has been added to Modules. Just confirm that the addition is successful, do not make any changes
insert image description here
. The Build Variants column is the configuration for generating apk. There are two built-in methods for generating apk, namely debug and release
debug for debugging. Usually we write the project code and compile it. The default is to use the apk generated by debug;
release is used for official release for download and installation. This method needs to create a keystore first (the keystore is the certificate of the apk. When the apk is updated, the certificate must be matched to be updated. ), fill in a string of information to generate apk through Build->Build Bundles/APKS->Build APKs

Back to the topic, after selecting the generation method in Build Variants, add the signature to be used in Signing Config, save and exit the window.
insert image description here
Finally, modify the release version. In the lower left corner of the android studio interface, modify the build variant, and then run Run the project, you can find that the IDE no longer reports an error, and the android:sharedUserId="android.uid.system" permission has been added to the application

2. Introduction to the sharedUserId attribute

Generally, each apk has a userid. For the apk file installed in the device, Android will assign a Linux user ID to it according to the userid for management, and create a sandbox for it to prevent other applications from being affected ( or be affected by other applications), that is, apks belonging to different Linux user IDs cannot communicate with each other.
However, sometimes we develop multiple apks and need them to share resources with each other, so we need to set the shareUserId to achieve it. You only need to ensure that the shareUserId of these apks is the same, and the value can be customized, such as android:sharedUserId= "my_id"

3. Different sharedUserId permissions

All APKs in the system that use android.uid.system as the shared UID will first add android:sharedUserId="android.uid.system" in the AndroidManifest.xml node, and then add LOCAL_CERTIFICATE := platform in Android.mk

All APKs in the system that use android.uid.nfc as the shared UID will first add android:sharedUserId="android.uid.nfc" in the AndroidManifest.xml node, and then add LOCAL_CERTIFICATE := platform in Android.mk

All APKs that use android.uid.se as the shared UID in the system will first add android:sharedUserId="android.uid.se" in the AndroidManifest.xml node, and then add LOCAL_CERTIFICATE := platform in Android.mk

All APKs in the system that use android.uid.shared as the shared UID will add android:sharedUserId="android.uid.shared" in the AndroidManifest.xml node, and then add LOCAL_CERTIFICATE := shared in Android.mk

All APKs in the system that use android.media as the shared UID will add android:sharedUserId="android.media" in the AndroidManifest.xml node, and then add LOCAL_CERTIFICATE := media in Android.mk

Guess you like

Origin blog.csdn.net/qq_34205684/article/details/122985522