两个应用程序使用sharedUserID - createPackageContext问题

gre*_*qrl 0 android shared-data

我有两个单独的应用程序.我希望appB能够访问appA代码中的数据库.我不想在这个例子中使用内容提供者.

我已经阅读了有关这方面的帖子,并按照说明(在两个Android应用程序之间共享SQLite数据库?).

我做了什么:

AppA的Android Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.micode.primaryapplication">

    <application
        android:sharedUserId="com.micode.sharedid"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

AppB的Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.micode.secondaryapplication">

    <application
        android:sharedUserId="com.micode.sharedid"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

AppA创建了一个数据库,AppB需要访问该数据库.

AppB中使用以下代码:

package com.micode.secondaryapplication;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button btnExit;
    TextView txtViewDisplay;
    Context sharedContext;

    SQLiteDatabase db = null;
    private static String DBNAME = "Contacts.db";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupContext();
    }

    private void setupContext()
    {
        try {
            sharedContext = this.createPackageContext("com.micode.primaryapplication", Context.CONTEXT_INCLUDE_CODE);
            if (sharedContext == null) {
                return;
            }
        } catch (Exception e) {
            String error = e.getMessage();
            System.out.println(error);
            return;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我尝试使用完全相同的签名密钥构建两个应用程序,即构建/生成签名APK并在安装之前卸载任何先前的实例.

我也试图在调试模式下运行它,即在调试模式下安装AppA,然后在调试模式下安装AppB.

在后一种情况下,我看到以下错误:

从com.micode.primaryapplication(使用uid 10226)请求代码在进程com.micode.secondaryapplication中运行(使用uid 10227)

执行以下行时会触发此错误.

this.createPackageContext("com.micode.primaryapplication", Context.CONTEXT_INCLUDE_CODE);
Run Code Online (Sandbox Code Playgroud)

它让我相信问题在于安装,即在我看来,shareduserid已被忽略,因此两个应用程序不共享相同的UID,而是具有唯一的UID.

为了清楚起见,我只有两个使用这些共享用户ID的应用程序.

任何帮助是极大的赞赏...

Com*_*are 6

首先,我真的不推荐这种技术.sharedUserId主要用于预安装的应用程序.特别是,一旦您发布应用程序,您就永远不能更改sharedUserId(包括定义之前没有应用程序的应用程序).如果您更改sharedUserId,并且用户从旧版升级sharedUserId到新版,则您的应用无法再访问应用自己的文件,因为它们归旧版所有sharedUserId.

话虽如此,android:sharedUserId是一个属性<manifest>,而不是<application>.