A tutorial on using Shareuserid to get system privileges in the Android system _android

Source: Internet
Author: User

Android allocates a separate space for each APK process (for example, only access to files under/data/data/'s own package name), and generally prohibits access to data between apk. With the shared user ID, multiple apk with the same user ID can be configured to run in the same process. So the default is that you can access arbitrary data to each other. It can also be configured to run as a different process, with access to databases and files in other APK data directories. Just like accessing the data in this program (using the IPC mechanism, between different processes, such as aidl).

Using the same Shareuserid, multiple apk run to the same process to achieve data access between multiple apk
implementation effect: Copy the Session.log under the a.apk assets directory under/DATA/DATA/A package name/directory

a.apk

Androidmanifest.xml

<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
  package= "Com.example.demo1"
  Android:shareduserid= "Com.example"
  android:versioncode= "1"
  android:versionname= "1.0" >
  < USES-SDK
    android:minsdkversion= "8"
    android:targetsdkversion= "/>
  <application
    android: icon= "@drawable/ic_launcher"
    android:label= "@string/app_name"
    android:theme= "@style/apptheme" >
    <activity
      android:name= ". Mainactivity "
      android:label=" @string/title_activity_main ">
      <intent-filter>
        <action Android:name= "Android.intent.action.MAIN"/>
 
        <category android:name= "Android.intent.category.LAUNCHER" "/>
      </intent-filter>
    </activity>
  </application>
 
</manifest>

B.APK (Implementing access resources and copying)
Mainactivity.java

Package Com.example.demo2;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
 
Import Java.io.OutputStream;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.content.Context;
Import android.content.pm.PackageManager.NameNotFoundException;
Import Android.view.Menu;
Import Android.view.MenuItem;
 
Import Android.support.v4.app.NavUtils; public class Mainactivity extends activity {@Override public void onCreate (Bundle savedinstancestate) {SUPER.O
    Ncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Context context = NULL;
    InputStream input = null;
    OutputStream output = null;
 
      try {context = This.createpackagecontext ("Com.example.demo1", context.context_ignore_security);
      File File = new file ("/data/data/com.example.demo1/session.log");
      if (!file.exists ()) {file.createnewfile (); input = Context.getassets (). opEn ("session.log");
      Output = new FileOutputStream (file);
      byte[] buffer = new byte[1024];
      int readlength = 0;
      while ((Readlength = input.read (buffer))!=-1) {output.write (buffer, 0, readlength);
    The catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
          } finally{try {if (input!=null | | output!= null) {input.close ();
          Output.close ();
          input = null;
        output = null; @Override public boolean Oncrea for catch (Exception E2) {//Todo:handle Exception}}}
    Teoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.activity_main, menu);
  return true;
 }
 
}

Androidmanifest.xml

<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
  package= "Com.example.demo2"
  Android:versioncode= "1"
  android:versionname= "1.0"
  android:shareduserid= "Com.example" > 
  < USES-SDK
    android:minsdkversion= "8"
    android:targetsdkversion= "/>
  <application
    android: icon= "@drawable/ic_launcher"
    android:label= "@string/app_name"
    android:theme= "@style/apptheme" >
    <activity
      android:name= ". Mainactivity "
      android:label=" @string/title_activity_main ">
      <intent-filter>
        <action Android:name= "Android.intent.action.MAIN"/>
 
        <category android:name= "Android.intent.category.LAUNCHER" "/>
      </intent-filter>
    </activity>
  </application>
 
</manifest>

a.apk,b.apk Use the same shareduserid:com.example
Implementation effect:

Second, through the Shareduserid to obtain system permissions
(1) Add android:shareduserid= "Android.uid.system" in Androidmanifest.xml
(2) Add local_certificate to android.mk file: = Platform (use system signature)
(3) In the source below the MM compilation
The generated apk can obtain system permissions, create directories or files under any system permission directory, and access other APK resources (note that the created file (folder) is created only by the creator (except for System,root) has readable writable permission-rw-------).

Third, expand

All apk that use Android.uid.system as the shared UID in the system will first add android:shareduserid= "Android.uid.system" to the manifest node, Then add the local_certificate in the android.mk: = platform. You can see settings, etc.

All apk that use android.uid.shared as the shared UID in the system will add android:shareduserid= "android.uid.shared" to the manifest node, Then add Local_certificate in android.mk: = Shared. You can see launcher, etc.

All apk that use Android.media as the shared UID in the system will add android:shareduserid= "Android.media" to the manifest node and then increase android.mk in Local_ Certificate: = media. You can see gallery and so on.

Iv. Problem-solving

Finally, this Android:shareduserid property can not only put apk into the system process, but also can configure multiple apk run in a process, so that you can share data, should be useful

We can see android:shareduserid= "Android.uid.system" in the Androidmenifest.xml.
But with this sentence, the SD card can not read and write operations, such as in the SD card to create a new folder, is to create an unsuccessful. But if the android:shareduserid= "Android.uid.system" commented out, you can do IO operation in the SD card.
In the settings android:shareduserid= "Android.uid.system" is indispensable, less it many settings under the application directly open, or an error.

Workaround One:

Volume.cpp files in the Vold module
Make a change in the statement that calls Domount ~

Domount (DevicePath, Path, False, False, false,1000, 1015, 0702, True)
↓
domount (DevicePath, Path, False, True, FA lse,1000, 1015, 0002, True)

Try it after compiling

Workaround Two:
The function of SD card operation is independent, make an independent apk, and then call the change function on the original project.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.