AndroidManifest之android:sharedUserId

摘要

通过两个demo(firstProject,secondPorject)测试

1.包名一致,签名不一致

2.包名一致,签名一致

3.包名不一致,签名不一致

4.包名一致,签名一致

四种情况下,APK的安装情况以及UserId和PID是否相同。

包名一致,签名不一致,无法安装

first安装:

u0_a185 5459 253 578740 38300 ffffffff b767b07b S com.example.xiongcen.firstproject

second安装:无法安装

Failure [INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES]

包名一致,签名一致

包名一致,签名一致,默认不设置android:sharedUserId,覆盖安装成功,UserId默认相同,但PID不同

first安装:

u0_a187 5692 253 579784 38632 ffffffff b767b07b S com.example.xiongcen.firstproject

second安装:覆盖安装成功

u0_a187 5748 253 579804 38712 ffffffff b767b07b S com.example.xiongcen.firstproject

包名一致,签名一致,设置不同的android:sharedUserId,无法安装

给first设置android:sharedUserId=”xiongcen.uid”

给second设置android:sharedUserId=”xiongcen.uid2”

first安装:

u0_a186 5589 253 583152 38868 ffffffff b767b07b S com.example.xiongcen.firstproject

second安装:无法安装

Failure [INSTALL_FAILED_UID_CHANGED]

包名不一致,签名不一致

包名不一致,签名不一致,默认不设置android:sharedUserId,安装成功,UserId和PID都不相同

first安装:

u0_a188 5846 253 578744 38636 ffffffff b767b07b S com.example.xiongcen.firstproject

second安装:

u0_a188 5846 253 572080 38704 ffffffff b767b07b S com.example.xiongcen.firstproject

u0_a189 5927 253 578732 38256 ffffffff b767b07b S com.example.xiongcen.secondproject

包名不一致,签名不一致,设置相同android:sharedUserId=”xiongcen.uid”,无法安装

first安装:

u0_a190 6087 253 578740 38320 ffffffff b767b07b S com.example.xiongcen.firstproject

second安装:

Failure [INSTALL_FAILED_SHARED_USER_INCOMPATIBLE]

包名不一致,签名不一致,安装成功,UserId和PID都不相同

给first设置android:sharedUserId=”xiongcen.uid”

给second设置android:sharedUserId=”xiongcen.uid2”

first安装:

u0_a191 6197 253 578736 38296 ffffffff b767b07b S com.example.xiongcen.firstproject

second安装:

u0_a191 6197 253 572276 39312 ffffffff b767b07b S com.example.xiongcen.firstproject

u0_a182 6260 253 579776 38572 ffffffff b767b07b S com.example.xiongcen.secondproject

包名不一致,签名一致

包名不一致,签名一致,默认不设置android:sharedUserId,安装成功,UserId和PID都不相同

first安装:

u0_a192 6360 253 578740 38284 ffffffff b767b07b S com.example.xiongcen.firstproject

second安装:

u0_a192 6360 253 572076 38352 ffffffff b767b07b S com.example.xiongcen.firstproject

u0_a193 6411 253 578728 38584 ffffffff b767b07b S com.example.xiongcen.secondproject

包名不一致,签名一致,设置相同android:sharedUserId=”xiongcen.uid”,安装成功,UserId相同,PID不同,并且可以共享数据

first安装:

u0_a194 6531 253 578740 38276 ffffffff b767b07b S com.example.xiongcen.firstproject

second安装:

u0_a194 6531 253 573204 39332 ffffffff b767b07b S com.example.xiongcen.firstproject

u0_a194 6588 253 593036 40224 ffffffff b767b07b S com.example.xiongcen.secondproject

共享方法测试:
在firstProject中:

1
2
3
4
5
6
7
setValue("xiongcen", "test_share_data", "测试数据共享");
public void setValue(String group, String key, String defaultValue) {
SharedPreferences sp = getSharedPreference(group);
SharedPreferences.Editor editor = sp.edit().putString(key, defaultValue);
editor.apply();
}

在secondProject中:

1
2
3
4
5
6
7
8
9
try {
Context context1 = MainActivity.this.createPackageContext("com.example.xiongcen.firstproject", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = context1.getSharedPreferences("xiongcen", Context.MODE_MULTI_PROCESS);
String data = sharedPreferences.getString("test_share_data", "test_share_data");
text2.setText(data);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
text2.setText("出错了!");
}

在secondProject可以正确展示“测试数据共享”字样。

demo地址 firstProject+secondProject