Android Parse SDK - 保护凭据

时间:2015-08-10 19:10:39

标签: android security parse-platform credentials api-key

我正在使用Parse Android SDK作为我的应用,我正在做这样的事情。

@Override
public void onCreate() {
    super.onCreate();

    JSONObject credentials = readCredentials();

    String applicationId = credentials.getString("applicationId"); 
    String clientKey = credentials.getString("clientKey");

    Parse.initialize(this, applicationId, clientKey);
    ParseInstallation.getCurrentInstallation().saveInBackground();
}

如您所见,我必须提供应用程序ID和客户端密钥。我目前正在从JSON文件中加载它们。以某种方式存储这些值的最佳方法是,如果有人反编译我的源并且可以看到JSON文件,则无法获取这些值?我假设JSON文件是存储这些文件的一种不好的方法,如果我担心这里的安全性。我曾考虑使用ProGuard来混淆我的代码,但这不是最佳解决方案,因为我的项目是开源的。我不在乎人们是否可以根据他们的手机和decompile来源(对他们开玩笑,这是在GitHub上);我只是不希望他们看到这些凭据。目前,我是Git忽略GitHub的凭证文件,但显然我不能为实际的应用程序做这个。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将密钥存储在app的build.gradle中,如下所示:

buildTypes {
    debug {
        minifyEnabled false

        resValue "string", "app_id", "67604hghhfhfhgh"
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    release {
        minifyEnabled true

        resValue "string", "app_id", "gfhdgldd6567567567hjl"
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

你可以像这样在string.xml中引用:

<string name="key">@string/app_id</string>

现在你可以这样打电话:

android:value="@string/key"/>