Copy/share configurations between paid/free versions of Android app?

后端 未结 3 1216
感情败类
感情败类 2021-02-03 13:32

My Android app comes both as a free and paid version. I have created a library project and two additional Application projects, one \'Free\' and one \'Paid\' version (signed wit

3条回答
  •  眼角桃花
    2021-02-03 13:40

    I've collected information from a number of stackoverflow answers to provide a way to copy all SharedPreference data from one app to another. In my particular case I'm using product flavours for a free and a pro app, and I want to copy from free to pro.

    CAUTION: This only works if you have not released either version on the play store. If you add (or remove) sharedUserId to your app after it is on the play store, your users won't be able to update without uninstalling. I learnt this the hard way. Thanks Google..

    Add sharedUserId to your manifest in both apps. Note that this will only work if both apps are signed with the same certificate.

    
    

    Then call this method when you first intialize the pro app.

    private void getSettingsFromFreeApp() {
        // This is a build config constant to check which build flavour this is
        if (BuildConfig.IS_PRO) {
            try {
                Context otherAppContext = this.createPackageContext("my.package.name.free", Context.MODE_PRIVATE);
                SharedPreferences otherAppPrefs = PreferenceManager.getDefaultSharedPreferences(otherAppContext);
    
                Map keys = otherAppPrefs.getAll();
                SharedPreferences.Editor editor = prefs.edit();
                for(Map.Entry entry : keys.entrySet()){
    
                    Object value = getWildCardType(entry.getValue());
    
                    Log.d("map values", entry.getKey() + ": " + entry.getValue());
                    if (entry.getValue() instanceof Boolean) {
                        editor.putBoolean(entry.getKey(), (boolean) value);
                        editor.apply();
                    } else if (value instanceof Long) {
                        editor.putLong(entry.getKey(), (long) value);
                        editor.apply();
                    } else if (value instanceof Float) {
                        editor.putFloat(entry.getKey(), (float) value);
                        editor.apply();
                    } else if (value instanceof Integer) {
                        editor.putInt(entry.getKey(), (int) value);
                        editor.apply();
                    } else if (value instanceof String) {
                        editor.putString(entry.getKey(), String.valueOf(value));
                        editor.apply();
                    }
                }
    
    
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    
    private Object getWildCardType(Object value) {
       return value;
    }
    

    Also, according to this answer you will want to call getSettingsFromFreeApp() before any other call to get preferences in your app.

提交回复
热议问题