问题
I've been working on a project for some time now that's targeting a minimum of 4.0 ICS. Recently, I got a new Play Edition Moto G that, after running all updates, is running 4.4 (KK). So I decided I wanted to try and deploy the program on it as well as the other two test devices that I have.
Everything was going fine until my program got to a point where it was trying to access SharedPreference via PreferenceManager.getDefaultSharedPreferences(Context). After debugging and running through each frame, I was able to figure out that values are in fact written to the preferences file (or at least appear to be since the Map container in the SharedPreference object contains the values I put there previously). However when I attempt to read any of the values with their respective keys, I'm getting a INSTALL_FAILED_SHARED_USER_INCOMPATIBLE value instead and that's causing my program to crash (not directly but it's causing proceeding functions that require the proper output from Preferences to fail). This only happens on 4.4 to my knowledge.
I've scoured the net and SO for answers on this and I can't seem to find anything that solves my problem. My manifest doesn't declare a sharedUserId or sharedUserLabel so I don't think that would be causing the issue. I've uninstalled and re-installed the package quite a few times to no avail. I was under the impression that the XML file used for SharedPreference was written to the program's local directory which I understood to grant at least read/write access by the program itself (unless this is what changed in 4.4). And it doesn't appear to be storing that location on a external source since the Moto G has no SD Card in it.
Does anyone have any idea how to work around this? As it stands now, my program isn't able to be deployed on 4.4 devices and I would really like for that not to be the case.
回答1:
So after some sleuthing, I eventually figured out my problem. It was a culmination of multiple incoherent SO posts and just as many near worthless Google searches.
Some of my preferences need to be encrypted before they're written to the file and apparently since 4.2 Android has used OpenSSL as the new Security Provider over the traditional Crypto one. So after spending a few days researching, I was able to solve my problem by both enforcing the use of the Crypto SP and more clearly specifying the encryption and transformation algorithm/mode/padding to help not only make block size consistent but so that it's more clear what's going on instead of letting the API determine what might be the best. In this case, even after enforcing Crypto, it was still broken hence the further digging into A/M/P.
So my initial problem was masking the actual exception and it was super fun to figure that out.
来源:https://stackoverflow.com/questions/24697952/android-4-4-kitkat-sharedpreferences-returning-install-failed-shared-user-inco