GrantPermissionCallable:权限:无法授予 android.permission.SET_TIME

GrantPermissionCallable: Permission: android.permission.SET_TIME cannot be granted

我正在尝试使用新的 GrantPermissionRule,它是最新支持库的一部分。

在我的清单中,我声明如下:

<uses-permission android:name="android.permission.SET_TIME"/>
在我的代码中,我调用:

@Rule
public GrantPermissionRule mPermissionRule = GrantPermissionRule.grant(Manifest.permission.SET_TIME);

但是当我 运行 测试时,我看到了错误:

12-28 14:09:35.063 7193-7215/com.blah.test E/GrantPermissionCallable: Permission: android.permission.SET_TIME cannot be granted!
12-28 14:09:35.066 7193-7215/com.blah I/TestRunner: failed: maxOfflineWithChangingDeviceTime(com.blah)
12-28 14:09:35.066 7193-7215/com.blah I/TestRunner: ----- begin exception -----
12-28 14:09:35.073 7193-7215/com.blah I/TestRunner: junit.framework.AssertionFailedError: Failed to grant permissions, see logcat for details
                                                                              at junit.framework.Assert.fail(Assert.java:50)
                                                                              at android.support.test.runner.permission.PermissionRequester.requestPermissions(PermissionRequester.java:110)
                                                                              at android.support.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:108)
                                                                              at org.junit.rules.RunRules.evaluate(RunRules.java:20)
                                                                              at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
                                                                              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
                                                                              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
                                                                              at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
                                                                              at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
                                                                              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                                              at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
                                                                              at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
                                                                              at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                                              at org.junit.runners.Suite.runChild(Suite.java:128)
                                                                              at org.junit.runners.Suite.runChild(Suite.java:27)
                                                                              at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
                                                                              at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
                                                                              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                                              at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
                                                                              at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
                                                                              at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                                              at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                                                                              at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                                                                              at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
                                                                              at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
                                                                              at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
12-28 14:09:35.073 7193-7215/com.blah I/TestRunner: ----- end exception -----

我的 build.gradle 更新了以下几行:

androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test:rules:1.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'

有什么建议或解决方案吗?

SET_TIMEa signature|privileged-level permission。您不能在运行时请求它,无论是使用 requestPermissions() 还是 GrantPermissionRule.

并且,除非您正在构建自己的固件或计划让 root 用户在特权分区上安装您的应用程序,否则您最好从清单中删除 <uses-permission> 元素。

我想出了一个“操纵”系统时间的方法如果你使用模拟器

如果在启动模拟器之前set/change主机系统时间,模拟器将占用主机时间。

这样我就可以在 Windows 上将时间设置为我想要的时间,然后启动模拟器并执行测试。