两个APK 通过Content Provider 来共享数据,在Android 4.2.2 API 17 之前都是正常的,但是到Android 4.2.2 API 17 上面就会报告异常类似如下的异常信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
09-17 12:15:52.221: E/AndroidRuntime(4551): FATAL EXCEPTION: main 09-17 12:15:52.221: E/AndroidRuntime(4551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.identifier.gamecenter.gctictactoe/com.identifier.gamecenter.game.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.identifier.gamecenterapp.contentprovider.MyGamesContentProvider from ProcessRecord{42622078 4551:com.identifier.gamecenter.gctictactoe/u0a10108} (pid=4551, uid=10108) that is not exported from uid 10072 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.ActivityThread.access$600(ActivityThread.java:141) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.os.Handler.dispatchMessage(Handler.java:99) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.os.Looper.loop(Looper.java:137) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.ActivityThread.main(ActivityThread.java:5103) 09-17 12:15:52.221: E/AndroidRuntime(4551): at java.lang.reflect.Method.invokeNative(Native Method) 09-17 12:15:52.221: E/AndroidRuntime(4551): at java.lang.reflect.Method.invoke(Method.java:525) 09-17 12:15:52.221: E/AndroidRuntime(4551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 09-17 12:15:52.221: E/AndroidRuntime(4551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-17 12:15:52.221: E/AndroidRuntime(4551): at dalvik.system.NativeStart.main(Native Method) 09-17 12:15:52.221: E/AndroidRuntime(4551): Caused by: java.lang.SecurityException: Permission Denial: opening provider c.identifier.gamecenterapp.contentprovider.MyGamesContentProvider from ProcessRecord{42622078 4551:com.identifier.gamecenter.gctictactoe/u0a10108} (pid=4551, uid=10108) that is not exported from uid 10072 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.os.Parcel.readException(Parcel.java:1431) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.os.Parcel.readException(Parcel.java:1385) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2611) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.ActivityThread.acquireProvider(ActivityThread.java:4515) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2036) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1149) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.content.ContentResolver.query(ContentResolver.java:398) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.content.ContentResolver.query(ContentResolver.java:357) 09-17 12:15:52.221: E/AndroidRuntime(4551): at ch.ethz.csg.wlanopp.gapi.GameCenterController.getIdByGameTitle(GameCenterController.java:602) 09-17 12:15:52.221: E/AndroidRuntime(4551): at ch.ethz.csg.wlanopp.gapi.GameCenterController.isRegistered(GameCenterController.java:343) 09-17 12:15:52.221: E/AndroidRuntime(4551): at ch.ethz.csg.wlanopp.gapi.GameCenterController.addGame(GameCenterController.java:352) 09-17 12:15:52.221: E/AndroidRuntime(4551): at ch.ethz.csg.gamecenter.gctictactoe.MainActivity.onCreate(MainActivity.java:130) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.Activity.performCreate(Activity.java:5133) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 09-17 12:15:52.221: E/AndroidRuntime(4551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 09-17 12:15:52.221: E/AndroidRuntime(4551): ... 11 more 09-17 12:20:52.487: I/Process(4551): Sending signal. PID: 4551 SIG: 9 |
在AndroidManifest.xml中原来的定义如下
1 2 3 4 |
<provider android:name="com.identifier.gamecenterapp.contentprovider.MyGamesContentProvider" android:authorities="com.identifier.gamecenterapp.contentprovider" > </provider> |
并且没有定义 android:targetSdkVersion
查找了很多地方,最终发现原因
1 2 3 |
Content providers are no longer exported by default. That is, the default value for the android:exported attribute is now “false". If it’s important that other apps be able to access your content provider, you must now explicitly set android:exported="true". This change takes effect only if you set either android:targetSdkVersion or android:minSdkVersion to 17 or higher. Otherwise, the default value is still “true" even when running on Android 4.2 and higher. |
具体的网页在 http://developer.android.com/about/versions/android-4.2.html
因此修改为如下即可 也就是增加 android:exported="true"
1 2 3 4 5 |
<provider android:name="com.identifier.gamecenterapp.contentprovider.MyGamesContentProvider" android:authorities="com.identifier.gamecenterapp.contentprovider" android:exported="true"> </provider> |