One, foreword

Recently, we plan to build an equipment management system for the existing equipment of the company, collecting data of the equipment and sending instructions to the equipment.

During the process, I found that there was no response when the code executed the reboot command for a long time, but it was ok when I tried to execute the reboot command in ADB, and this situation never happened before. After repeated investigation, I found that the reason was that these devices were not root, so my APP did not have root permission and could not execute these commands

However, the app installed by the manufacturer can actually execute these commands

Originally in order to work smoothly, I do not intend to waste time by grasping this small point, but I am a person who likes to ask questions, so I have this blog today, this is my two days to consult all kinds of data and various debugging test results, I hope to help you

2. Access

Before we do something, we should make clear the concept of it so that we can understand it smoothly

There are two ways for our application to obtain system-level permissions:

(1) Device root

(2*) added in AndroidManifest

android:sharedUserId="android.uid.system"
Copy the code

The sharedUserId property allows multiple applications to run in the same process by setting the same User ID. And set sharedUserId to Android.uid. system can run the application and the system application in the same process, and then have the system permission

Add sharedUserId to sharedUserId

When sharedUserId is added to the Manifest file, we notice a problem

An INSTALL_FAILED_SHARED_USER_INCOMPATIBLE installation error is reported regardless of whether the installation is debugged or packaged

Here we need to explain the reasons for this problem, and let’s work backwards from the design idea

If any app with the same sharedUserId as you can run in the same process with your app and share data with your app, then who will guarantee the security of our app data? Surely not, so when you add this, the installation package cannot be installed

So how do you install it?

INSTALL_FAILED_SHARED_USER_INCOMPATIBLE (incompatible)

With sharedUserId we need to remember one concept

Applications using the same sharedUserId must use the same signature file

(1) Download relevant files

As a result, to install on an Android device, you need to find the same signature as the android native app

We can get what we want with Git at Google

Android.googlesource.com/platform/bu…

All you need are these two signature files

A special note needs to be made here

If you’re using a device manufacturer that hasn’t made this change, use the two that Google gave you

I have encountered some equipment because the manufacturer has done processing, so the document needs to be obtained from the manufacturer, otherwise it will still lead to the problem of installation failure

In addition, you need the KeyTool tool

Github.com/getfatday/k…

(2) Generate. JKS signature file

1. Create the signApk folder in the project directory and put the signature tool and signature file in this folder

2. Create the signature.sh script file in this folder to directly generate signatures

Writing signature files

./keytool-importkeypair -k giftedcat.jks -p 123456 -pk8 platform.pk8 -cert platform.x509.pem -alias key0
Copy the code

Giftedcat.jks is the name of the generated signature file

123456 is the password for the signature

Key0 is the alias of the signature

If you’re on Windows, double-click to get the signature file

3. Use in Android Studio

This step, in fact, I do not need to say too much, I believe that you are already familiar with the heart

At this point, we’re done with our app getting system-level permissions

I’m ready to run, but if I can’t run, I’m going to clean it up

Five, the conclusion

In the end, I would like to remind you that if you did not add this before, you need to uninstall the old application this time. After all, the signature has been changed, so if the existing online application needs to be updated, adding this will inevitably cause influence, which needs to be paid attention to