应用程序在混淆后读取共享首选项时崩溃

时间:2018-04-02 05:14:24

标签: android obfuscation android-proguard android-security

在我的应用程序中,我使用SharedPreferences存储一些用户首选项。该应用程序未被混淆(在proguard文件中为-dontobfuscate)。

现在在应用程序的下一个版本中,我想启用模糊处理。当我尝试这个时,应用程序返回NullPointerException,同时从应用程序的先前版本读取SharedPreferences数据。错误日志没有用,因为代码已经过模糊处理,并且没有提供有意义的信息。但是,在尝试调试模式时,我发现崩溃可能是由于null context,这是代码中的静态变量!应该不是这种情况,因为如果SharedPreferences不存在,应用程序将运行文件。

该应用仍然可以从未经过模糊处理的版本中读取SharedPreferences数据吗?

编写/阅读SharedPreferences是非常标准的: 写作:

SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor prefsEditor = mPrefs.edit();
    prefsEditor.putString("userUnitsOption", "C");
    //apply the storage
    prefsEditor.apply();

读:

final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        return mPrefs.getString("userUnitsOption", "A");

2 个答案:

答案 0 :(得分:1)

如果您想在Android中使用共享偏好设置,请按照以下说明使用它:

首先,您必须存储您的偏好:

SharedPreferences.Editor editor = getSharedPreferences("mySharedPref", MODE_PRIVATE).edit(); 
            editor.putString("myName", "abc");
            editor.apply();

现在,要阅读或获取存储的共享首选项,请按以下方式编写代码:

SharedPreferences prefs = MainActivity.this.getSharedPreferences("mySharedPref", MODE_PRIVATE); // Here  MainActivity.this represent the context. So you can use your context in place  of MainActivity.this
            String strName = prefs.getString("myName","defaultName");

答案 1 :(得分:0)

在科特林

用法
私有val sharedPref = defaultPrefs(this)

要保存数据-> sharedPref[KEY] = *String data to save*

获取数据-> val userDetails = sharedPref[KEY, ""]

创建如下所示的共享首选项帮助程序类。

object PreferenceHelper {

    fun defaultPrefs(context: Context): SharedPreferences =
        PreferenceManager.getDefaultSharedPreferences(context)

    fun customPrefs(context: Context, name: String): SharedPreferences =
        context.getSharedPreferences(name, Context.MODE_PRIVATE)

    inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
        val editor = this.edit()
        operation(editor)
        editor.apply()
    }

    /**
     * puts a key value pair in shared prefs if doesn't exists, otherwise updates value on given [key]
     */
    operator fun SharedPreferences.set(key: String, value: Any?) {
        when (value) {
            is String? -> edit { it.putString(key, value) }
            is Int -> edit { it.putInt(key, value) }
            is Boolean -> edit { it.putBoolean(key, value) }
            is Float -> edit { it.putFloat(key, value) }
            is Long -> edit { it.putLong(key, value) }
            else -> throw UnsupportedOperationException("Not yet implemented")
        }
    }

    /**
     * finds value on given key.
     * [T] is the type of value
     * @param defaultValue optional default value - will take null for strings, false for bool and -1 for numeric values if [defaultValue] is not specified
     */
    inline operator fun <reified T : Any> SharedPreferences.get(
        key: String,
        defaultValue: T? = null
    ): T? {
        return when (T::class) {
            String::class -> getString(key, defaultValue as? String) as T?
            Int::class -> getInt(key, defaultValue as? Int ?: -1) as T?
            Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T?
            Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T?
            Long::class -> getLong(key, defaultValue as? Long ?: -1) as T?
            else -> throw UnsupportedOperationException("Not yet implemented")
        }
    }
}