最近项目上有个需求,读取通讯录。当用户点了拒绝访问通讯录或者其他权限,导致无法使用,这时候我想重新打开权限设置,但是对于很多小白用户不知道怎么设置,这就会导致用户体验不友好的一面。
之前已经有人写过类似的文章,不过都比较分散,经实测将这些方法总结了一下。
要跳转的权限设置界面如图:目前手上只有华为和小米作为测试
最近项目上有个需求,读取通讯录。当用户点了拒绝访问通讯录或者其他权限,导致无法使用,这时候我想重新打开权限设置,但是对于很多小白用户不知道怎么设置,这就会导致用户体验不友好的一面。
之前已经有人写过类似的文章,不过都比较分散,经实测将这些方法总结了一下。
要跳转的权限设置界面如图:目前手上只有华为和小米作为测试
最近在 Android R(Android 11)系统上执行以前写的Espresso UI(AndroidX)单元测试用例的时候,发现在 联想拯救者电竞手机Pro(128GB) 系统版本 12.5.225上,如果测试用例申请了通讯录写入权限,如下:
1 |
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> |
那么测试用例执行的时候,会报告如下错误:
1 |
Test running failed:Process crashed. |
详细的logcat日志如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
D/Zygote: Forked child process 10664 I/ActivityManager: Start proc 10664:xx.test/u0a274 for added application xx.test I/AutoRun: AutoRunServices: ProcessStateHandler: sendKillAppRequest: [uid = 10274, pid = 10664, package name = xx.test mOtherPids = null, mStartupTime = 0, mDelay = 0, mCount = 0] I/adbd: jdwp connection from 10664 D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar I/yboard.biz.tes: The ClassLoaderContext is a special shared library. D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar I/Perf: Connecting to perf service. D/NetworkSecurityConfig: No Network Security Config specified, using platform default D/NetworkSecurityConfig: No Network Security Config specified, using platform default I/MonitoringInstr: Instrumentation started! I/AndroidJUnitRunner: Waiting for debugger to connect... I/System.out: Sending WAIT chunk I/ActivityManager: Killing 10664:xx.test/u0a274 (adj 0): permissions revoked I/Zygote: Process 10664 exited due to signal 9 (Killed) I/libprocessgroup: Successfully killed process cgroup uid 10274 pid 10664 in 44ms |
目前测试发现,即使通过GrantPermissionRule授予权限,也是没用的,代码根本来不及执行,应用就一下被系统杀死了。
1 2 |
@Rule public final GrantPermissionRule rule = GrantPermissionRule.grant(android.Manifest.permission.WRITE_CONTACTS); |
在OPPO/VIVO/小米等手机上,需要手工开启 后台弹出界面权限 才能执行单元测试。有个思路是通过UI Automator来自动开启这个权限,暂时还需要测试一下。
在Android 11执行如下代码:
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 |
/** * 保存拍照的图片是否已经完成 * * @param context Context 对象 * @return 查询成功返回 true ,否则返回false */ private static boolean isSavePictureComplete(@NonNull final Context context, long takeTime) { //扫描图片 final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final ContentResolver resolver = context.getContentResolver(); final Cursor cursor = resolver.query(uri, new String[]{ MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media._ID, MediaStore.Images.Media.SIZE }, MediaStore.MediaColumns.SIZE + ">0", null, MediaStore.Files.FileColumns._ID + " DESC limit 1 offset 0"); //读取扫描到的图片 if ((null != cursor) && (cursor.getCount() > 0) && cursor.moveToFirst()) { //获取图片时间 long time = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)); int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID)); if (String.valueOf(time).length() < 13) { time *= 1000; } cursor.close(); // 如果照片的插入时间大于相机的拍照时间,就认为是拍照图片已插入 return time + 1000 < takeTime; } return true; } |
会出现如下错误:
1 2 3 4 5 6 7 |
java.lang.IllegalArgumentException: Invalid token limit at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142) at android.content.ContentProviderProxy.query(ContentProviderNative.java:472) at android.content.ContentResolver.query(ContentResolver.java:1197) at android.content.ContentResolver.query(ContentResolver.java:1128) at android.content.ContentResolver.query(ContentResolver.java:1084) |
原因为 Android 11 上使用 ContentResolver 的时候,已经不允许进行 SQL语句拼接,主要是为了防止 SQL注入攻击。
代码参考如下:
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/** * 保存拍照的图片是否已经完成 * * @param context Context 对象 * @return 查询成功返回 true ,否则返回false */ private static boolean isSavePictureComplete(@NonNull final Context context, long takeTime) { final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final String[] columns = new String[]{ MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media._ID, MediaStore.Images.Media.SIZE}; //扫描图片 final ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q/*29*/) { final Bundle bundle = new Bundle(); bundle.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, MediaStore.MediaColumns.SIZE + " > ?"); bundle.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, new String[]{"0"}); bundle.putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, new String[]{MediaStore.Files.FileColumns._ID}); bundle.putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING); bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, 1); bundle.putInt(ContentResolver.QUERY_ARG_OFFSET, 0); cursor = contentResolver.query(uri, columns, bundle, null); } else { cursor = contentResolver.query(uri, columns, MediaStore.MediaColumns.SIZE + ">0", null, MediaStore.Files.FileColumns._ID + " DESC limit 1 offset 0"); } //读取扫描到的图片 if ((null != cursor) && (cursor.getCount() > 0) && cursor.moveToFirst()) { //获取图片时间 long time = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)); if (String.valueOf(time).length() < 13) { time *= 1000; } // 如果照片的插入时间大于相机的拍照时间,就认为是拍照图片已插入 return time + 1000 < takeTime; } } finally { if (null != cursor) { cursor.close(); } } return true; } |
limiting number of rows in a ContentResolver.query() function
在编写单元测试用例的时候,如果被测试类是与AIDL相关的类,不管是直接调用,还是被测试类继承了AIDL生成的类,由于AIDL类是在编译期间动态生成的,并且每次构建都会重新生成一次(类名相同,但是Hash可能会变,文件修改日期必然变更)。
然而,由于PowerMock调用Objenesis库为了加快编译速度,会缓存同名的类,这样导致在运行时进行类型转换的时候无法通过校验,报告如下错误:
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
org.mockito.exceptions.base.MockitoException: ClassCastException occurred while creating the mockito proxy : class to mock : 'com.squareup.otto.Bus', loaded by classloader : 'org.robolectric.internal.bytecode.InstrumentingClassLoader@1593948d' created class : 'com.squareup.otto.Bus$$EnhancerByMockitoWithCGLIB$$82a3b196', loaded by classloader : 'org.robolectric.internal.bytecode.InstrumentingClassLoader@1593948d' proxy instance class : 'com.squareup.otto.Bus$$EnhancerByMockitoWithCGLIB$$82a3b196', loaded by classloader : 'org.mockito.internal.creation.util.SearchingClassLoader@618ff5c2' instance creation by : ObjenesisInstantiator You might experience classloading issues, disabling the Objenesis cache *might* help (see MockitoConfiguration) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:61) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49) at org.powermock.api.mockito.repackaged.CglibMockMaker.createMock(CglibMockMaker.java:24) at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:45) at com.acme.android.myapp.services.gcm.handlers.RequestLogoutHandlerTest.setup(RequestLogoutHandlerTest.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152) 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$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) Caused by: java.lang.ClassCastException: Cannot cast com.squareup.otto.Bus$$EnhancerByMockitoWithCGLIB$$82a3b196 to com.squareup.otto.Bus at java.lang.Class.cast(Class.java:3369) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:59) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49) at org.powermock.api.mockito.repackaged.CglibMockMaker.createMock(CglibMockMaker.java:24) at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:45) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59) at org.mockito.Mockito.mock(Mockito.java:1285) at org.mockito.Mockito.mock(Mockito.java:1163) ... 36 more org.mockito.exceptions.base.MockitoException: ClassCastException occurred while creating the mockito proxy : class to mock : 'com.squareup.otto.Bus', loaded by classloader : 'org.robolectric.internal.bytecode.InstrumentingClassLoader@1593948d' created class : 'com.squareup.otto.Bus$$EnhancerByMockitoWithCGLIB$$82a3b196', loaded by classloader : 'org.robolectric.internal.bytecode.InstrumentingClassLoader@1593948d' proxy instance class : 'com.squareup.otto.Bus$$EnhancerByMockitoWithCGLIB$$82a3b196', loaded by classloader : 'org.mockito.internal.creation.util.SearchingClassLoader@618ff5c2' instance creation by : ObjenesisInstantiator You might experience classloading issues, disabling the Objenesis cache *might* help (see MockitoConfiguration) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:61) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49) at org.powermock.api.mockito.repackaged.CglibMockMaker.createMock(CglibMockMaker.java:24) at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:45) at com.acme.android.myapp.services.gcm.handlers.RequestLogoutHandlerTest.setup(RequestLogoutHandlerTest.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152) 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$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:6 |
解决方法就是阻止Objenesis的缓存功能。在测试用例的根目录下新建文件,如下:
1 2 3 4 5 6 7 8 9 |
package org.mockito.configuration; public class MockitoConfiguration extends DefaultMockitoConfiguration { @Override public boolean enableClassCache() { return false; } } |
参照 Android Studio 3.6.3/4.0/4.1/4.2配置Robolectric-3.8/4.3.1/4.5.1/4.6.1 Powermock-1.6.6单元测试环境 进行单元测试,不过,由于工程的限制(不能依赖 AndroidX),我们只能在 Robolectric 3.8 。
Android Studio 4.1.3,JDK使用 Java 1.8
在执行如下测试用例的时候,发现当被测试代码使用 Executors 在子线程执行的时候,如果使用 Powermock 的 WhenNew 对被测试对象进行仿真的时候,无法正确的被 Mock。
被测试代码如下:
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 33 34 35 36 37 38 39 40 41 42 |
import android.support.annotation.NonNull; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CustomExecutor { private final static String TAG = "CustomExecutor"; private static volatile CustomExecutor instance; private CustomMocker custom; private final ExecutorService executor = Executors.newSingleThreadExecutor(); private CustomExecutor() { executor.submit(new Runnable() { @Override public void run() { custom = new CustomMocker(); } }); } @NonNull public static CustomExecutor getInstance() { if (null == instance) { synchronized (CustomExecutor.class) { if (null == instance) { instance = new CustomExecutor(); } } } return instance; } public String getMessage() throws Exception { final Future<String> future = executor.submit(new Callable<String>() { @Override public String call() { return custom.getMessage(); } }); return future.get(); } } |
1 2 3 4 5 |
public class CustomMocker { public String getMessage() { return "I am not mocked"; } } |
单元测试代码如下:
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 33 34 35 |
import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.rule.PowerMockRule; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; @RunWith(RobolectricTestRunner.class) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) @PrepareForTest({CustomMocker.class, CustomExecutor.class}) @Config(manifest = Config.NONE, constants = BuildConfig.class, sdk = 26) public class CustomExecutorTest { @Rule public final PowerMockRule rule = new PowerMockRule(); @Before public void setup() throws Exception { final CustomMocker clz = PowerMockito.mock(CustomMocker.class); PowerMockito.when(clz.getMessage()).thenReturn("I am mocked!"); PowerMockito.whenNew(CustomMocker.class).withAnyArguments().thenReturn(clz); } @Test public void testCustomExecutors() { final CustomExecutor executor = CustomExecutor.getInstance(); Assert.assertNotNull(executor); Assert.assertEquals(expStr, executor.getMessage()); } } |
上述的测试代码在执行的时候,完全不会生效。
网上查询了一下,根据 Unable to get mocked instance of Executor in separate class 的介绍,我们需要在测试用例里面的@PrepareForTest
里面增加 Executors.class
,于是修改测试用例为如下:
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunnerDelegate; import org.powermock.modules.junit4.rule.PowerMockRule; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(RobolectricTestRunner.class) @PowerMockIgnore({"jdk.internal.reflect.*", "org.apache.commons.*", "org.mockito.*", "org.robolectric.*", "android.*"}) @PrepareForTest({ExecutorService.class, Executors.class, CustomMocker.class, CustomExecutor.class}) @Config(sdk = 26) public class CustomExecutorTest { private final static String expStr = "I am mocked!"; @Rule public final PowerMockRule rule = new PowerMockRule(); @Before public void setup() throws Exception { PowerMockito.mockStatic(Executors.class); PowerMockito.mockStatic(ExecutorService.class); PowerMockito.when(Executors.newSingleThreadExecutor()).thenCallRealMethod(); PowerMockito.mockStatic(CustomExecutor.class); final CustomMocker clz = PowerMockito.mock(CustomMocker.class); PowerMockito.when(clz.getMessage()).thenReturn(expStr); PowerMockito.whenNew(CustomMocker.class).withAnyArguments().thenReturn(clz); PowerMockito.mockStatic(CustomExecutor.class); PowerMockito.when(CustomExecutor.getInstance()).thenCallRealMethod(); } @Test public void testExecutorsMock() throws Exception { final CustomExecutor executor = CustomExecutor.getInstance(); Assert.assertNotNull(executor); Assert.assertEquals(expStr, executor.getMessage()); } } |
结果出现一个诡异的现象,如果在创建对象的地方设置断点进行调试跟踪,就是正确的,去掉断点,就会失败。
这个问题卡住很久,结果突然想起以前写的一个差不多功能的被测试类,是能正常工作的,于是进行了代码对比,结果发现一个奇怪的现象,只要内部定义一个类来中继一下,就可以完美解决问题。于是修改被测试类,代码如下:
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import android.support.annotation.NonNull; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class CustomExecutor { private static volatile CustomExecutor instance; @NonNull private final ExecutorService executor = Executors.newSingleThreadExecutor(); private Delegate delegate; private CustomExecutor() { executor.submit(new Runnable() { @Override public void run() { delegate = new Delegate(); } }); } @NonNull public static CustomExecutor getInstance() { if (null == instance) { synchronized (CustomExecutor.class) { if (null == instance) { instance = new CustomExecutor(); } } } return instance; } public String getMessage() throws Exception { final Future<String> future = executor.submit(new Callable<String>() { @Override public String call() { return delegate.getMessage(); } }); return future.get(); } private static class Delegate { private final CustomMocker mocker; public Delegate() { mocker = new CustomMocker(); } public String getMessage() { return mocker.getMessage(); } } } |
也就是内部定义来一个代理子类,初始化的时候,通过内部子类进行初始化。
这个现象比较奇怪,目前能解决问题,具体原因还是不详。
使用webview.loadUrl
给JavaScript
传Base64
格式的较大图片时,有的Android版本上不反馈任何错误信息,只是页面接收不到任何信息,传入的脚本不执行任何操作。
有的则报错,如下:
1 |
Refusing to load URL as it exceeds 2097152 characters |
今天升级到 Android Studio Arctic Fox | 2020.3.1 出现了如下错误:
1 2 3 4 5 6 7 |
Corrputed Installation Missing essential plugin: org.jetbrains.android Please reinstall Android Studio from scratch. |
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
Public Function UploadFileTo(sFileName As String, uURL As String, nameFieldID As String, dataFieldID As String, formID As String) As Boolean Const STR_BOUNDARY As String = "a832972453175" Dim nFile As Integer Dim baBuffer() As Byte Dim sPostData As String Dim WinHttpReq As Object Set WinHttpReq = CreateObject("Microsoft.XMLHTTP") UploadFileTo = False '--- read file nFile = FreeFile Open sFileName For Binary Access Read As nFile If LOF(nFile) > 0 Then ReDim baBuffer(0 To LOF(nFile) - 1) As Byte Get nFile, , baBuffer sPostData = StrConv(baBuffer, vbUnicode) End If Close nFile sPostData = "--" & STR_BOUNDARY & vbCrLf & _ "Content-Disposition: form-data; name=""file""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _ "Content-Type: text/plain" & vbCrLf & vbCrLf & _ sPostData & vbCrLf & vbCrLf & _ STR_BOUNDARY & vbCrLf & _ "Content-Disposition: form-data; name=""Action""" & vbCrLf & _ vbCrLf & "Send File" & vbCrLf & _ "--" & STR_BOUNDARY & "--" With WinHttpReq 'UPLOAD REQUEST .Open "POST", uURL, False .setRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY .setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" .send (sPostData) End With ''--------------------------- UploadFileTo = True End Function Private Sub SelFileButton_Click() '新建一个对话框对象 Set oFD = Application.FileDialog(msoFileDialogFilePicker) '配置对话框 With oFD .Title = "请选择要查找的文件夹" .InitialFileName = "C:\" .Filters.Clear .Filters.Add "All Files", "*.*", 1 End With '显示对话框 oFD.Show '获取选择对话框选择的文件夹 Set fs = oFD.SelectedItems For Each f In fs SelFileTextBox.Value = f Next End Sub Private Sub UploadButton_Click() If Trim$(SelFileTextBox.Value) = vbNullString Then MsgBox "没有选择需要上传的文件" Else If UploadFileTo(SelFileTextBox.Value, "http://127.0.0.1:8000/", "myFileNameField", "myFileData", "myDistantForm") Then MsgBox "文件上传成功" Else MsgBox "文件上传失败" End If End If End Sub |
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# -*- coding: utf-8 -*- #!/usr/bin/env python3 """Simple HTTP Server With Upload. This module builds on BaseHTTPServer by implementing the standard GET and HEAD requests in a fairly straightforward manner. see: https://gist.github.com/UniIsland/3346170 """ __version__ = "0.1" __all__ = ["SimpleHTTPRequestHandler"] import os import posixpath import http.server import urllib.request, urllib.parse, urllib.error import cgi import html import shutil import mimetypes import re from io import BytesIO class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler): """Simple HTTP request handler with GET/HEAD/POST commands. This serves files from the current directory and any of its subdirectories. The MIME type for files is determined by calling the .guess_type() method. And can reveive file uploaded by client. The GET/HEAD/POST requests are identical except that the HEAD request omits the actual contents of the file. """ server_version = "SimpleHTTPWithUpload/" + __version__ def do_GET(self): """Serve a GET request.""" f = self.send_head() if f: self.copyfile(f, self.wfile) f.close() def do_HEAD(self): """Serve a HEAD request.""" f = self.send_head() if f: f.close() def do_POST(self): """Serve a POST request.""" r, info = self.deal_post_data() print((r, info, "by: ", self.client_address)) f = BytesIO() f.write(b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">') f.write(b"<html>\n<title>Upload Result Page</title>\n") f.write(b"<body>\n<h2>Upload Result Page</h2>\n") f.write(b"<hr>\n") if r: f.write(b"<strong>Success:</strong>") else: f.write(b"<strong>Failed:</strong>") f.write(info.encode()) f.write(("<br><a href=\"%s\">back</a>" % self.headers['referer']).encode()) f.write(b"<hr><small>Powerd By: bones7456, check new version at ") f.write(b"<a href=\"http://li2z.cn/?s=SimpleHTTPServerWithUpload\">") f.write(b"here</a>.</small></body>\n</html>\n") length = f.tell() f.seek(0) self.send_response(200) self.send_header("Content-type", "text/html") self.send_header("Content-Length", str(length)) self.end_headers() if f: self.copyfile(f, self.wfile) f.close() def deal_post_data(self): content_type = self.headers['content-type'] if not content_type: return (False, "Content-Type header doesn't contain boundary") boundary = content_type.split("=")[1].encode() remainbytes = int(self.headers['content-length']) line = self.rfile.readline() remainbytes -= len(line) if not boundary in line: return (False, "Content NOT begin with boundary") line = self.rfile.readline() remainbytes -= len(line) fn = re.findall(r'Content-Disposition.*name="file"; filename="(.*)"', line.decode()) if not fn: return (False, "Can't find out file name...") path = self.translate_path(self.path) fn = os.path.join(path, fn[0]) line = self.rfile.readline() remainbytes -= len(line) line = self.rfile.readline() remainbytes -= len(line) try: out = open(fn, 'wb') except IOError: return (False, "Can't create file to write, do you have permission to write?") preline = self.rfile.readline() remainbytes -= len(preline) while remainbytes > 0: line = self.rfile.readline() remainbytes -= len(line) if boundary in line: preline = preline[0:-1] if preline.endswith(b'\r'): preline = preline[0:-1] out.write(preline) out.close() return (True, "File '%s' upload success!" % fn) else: out.write(preline) preline = line return (False, "Unexpect Ends of data.") def send_head(self): """Common code for GET and HEAD commands. This sends the response code and MIME headers. Return value is either a file object (which has to be copied to the outputfile by the caller unless the command was HEAD, and must be closed by the caller under all circumstances), or None, in which case the caller has nothing further to do. """ path = self.translate_path(self.path) f = None if os.path.isdir(path): if not self.path.endswith('/'): # redirect browser - doing basically what apache does self.send_response(301) self.send_header("Location", self.path + "/") self.end_headers() return None for index in "index.html", "index.htm": index = os.path.join(path, index) if os.path.exists(index): path = index break else: return self.list_directory(path) ctype = self.guess_type(path) try: # Always read in binary mode. Opening files in text mode may cause # newline translations, making the actual size of the content # transmitted *less* than the content-length! f = open(path, 'rb') except IOError: self.send_error(404, "File not found") return None self.send_response(200) self.send_header("Content-type", ctype) fs = os.fstat(f.fileno()) self.send_header("Content-Length", str(fs[6])) self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) self.end_headers() return f def list_directory(self, path): """Helper to produce a directory listing (absent index.html). Return value is either a file object, or None (indicating an error). In either case, the headers are sent, making the interface the same as for send_head(). """ try: list = os.listdir(path) except os.error: self.send_error(404, "No permission to list directory") return None list.sort(key=lambda a: a.lower()) f = BytesIO() displaypath = html.escape(urllib.parse.unquote(self.path)) f.write(b'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">') f.write(("<html>\n<title>Directory listing for %s</title>\n" % displaypath).encode()) f.write(("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath).encode()) f.write(b"<hr>\n") f.write(b"<form ENCTYPE=\"multipart/form-data\" method=\"post\">") f.write(b"<input name=\"file\" type=\"file\"/>") f.write(b"<input type=\"submit\" value=\"upload\"/></form>\n") f.write(b"<hr>\n<ul>\n") for name in list: fullname = os.path.join(path, name) displayname = linkname = name # Append / for directories or @ for symbolic links if os.path.isdir(fullname): displayname = name + "/" linkname = name + "/" if os.path.islink(fullname): displayname = name + "@" # Note: a link to a directory displays with @ and links with / f.write(('<li><a href="%s">%s</a>\n' % (urllib.parse.quote(linkname), html.escape(displayname))).encode()) f.write(b"</ul>\n<hr>\n</body>\n</html>\n") length = f.tell() f.seek(0) self.send_response(200) self.send_header("Content-type", "text/html") self.send_header("Content-Length", str(length)) self.end_headers() return f def translate_path(self, path): """Translate a /-separated PATH to the local filename syntax. Components that mean special things to the local file system (e.g. drive or directory names) are ignored. (XXX They should probably be diagnosed.) """ # abandon query parameters path = path.split('?',1)[0] path = path.split('#',1)[0] path = posixpath.normpath(urllib.parse.unquote(path)) words = path.split('/') words = [_f for _f in words if _f] path = os.getcwd() for word in words: drive, word = os.path.splitdrive(word) head, word = os.path.split(word) if word in (os.curdir, os.pardir): continue path = os.path.join(path, word) return path def copyfile(self, source, outputfile): """Copy all data between two file objects. The SOURCE argument is a file object open for reading (or anything with a read() method) and the DESTINATION argument is a file object open for writing (or anything with a write() method). The only reason for overriding this would be to change the block size or perhaps to replace newlines by CRLF -- note however that this the default server uses this to copy binary data as well. """ shutil.copyfileobj(source, outputfile) def guess_type(self, path): """Guess the type of a file. Argument is a PATH (a filename). Return value is a string of the form type/subtype, usable for a MIME Content-type header. The default implementation looks the file's extension up in the table self.extensions_map, using application/octet-stream as a default; however it would be permissible (if slow) to look inside the data to make a better guess. """ base, ext = posixpath.splitext(path) if ext in self.extensions_map: return self.extensions_map[ext] ext = ext.lower() if ext in self.extensions_map: return self.extensions_map[ext] else: return self.extensions_map[''] if not mimetypes.inited: mimetypes.init() # try to read system mime.types extensions_map = mimetypes.types_map.copy() extensions_map.update({ '': 'application/octet-stream', # Default '.py': 'text/plain', '.c': 'text/plain', '.h': 'text/plain', }) def test(HandlerClass = SimpleHTTPRequestHandler, ServerClass = http.server.HTTPServer): http.server.test(HandlerClass, ServerClass, "HTTP/1.0", 8000, '127.0.0.1') if __name__ == '__main__': test() |
借助 Android Plugin for Gradle,您可以通过命令行在 Gradle 项目中运行单元测试。如需详细了解如何为应用构建单元测试,请参阅构建有效的单元测试。
下表总结了如何使用 Gradle 运行单元测试:
单元测试类型 | 要运行的命令 | 测试结果所在的位置 | ||
---|---|---|---|---|
本地单元测试 | 调用test 任务:
|
HTML 测试结果文件:path_to_your_project/module_name/build/reports/tests/ 目录。
XML 测试结果文件: |
||
插桩单元测试 | 调用 connectedAndroidTest 任务:
|
HTML 测试结果文件:path_to_your_project/module_name/build/reports/androidTests/connected/ 目录。
XML 测试结果文件: |
参照 Android Studio 3.6.3/4.0/4.1/4.2配置Robolectric-4.5.1,Powermock-1.6.6单元测试环境 配置执行单元测试。
项目使用 Android Studio 4.1.3 ,Android Gradle构建工具(AGP)版本
1 |
com.android.tools.build:gradle:4.1.3 |
单元测试使用的 Robolectric 从 4.5.x 升级到 4.6.x ,编译时产生如下报错:
1 2 3 |
Failed to transform bcprov-jdk15on-1.68.jar (org.bouncycastle:bcprov-jdk15on:1.68) to match attributes {artifactType=processed-jar, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}. > Execution failed for JetifyTransform: /home/runner/.gradle/caches/modules-2/files-2.1/org.bouncycastle/bcprov-jdk15on/1.68/46a080368d38b428d237a59458f9bc915222894d/bcprov-jdk15on-1.68.jar. > Failed to transform '/home/runner/.gradle/caches/modules-2/files-2.1/org.bouncycastle/bcprov-jdk15on/1.68/46a080368d38b428d237a59458f9bc915222894d/bcprov-jdk15on-1.68.jar' using Jetifier. Reason: IllegalArgumentException, message: Unsupported class file major version 59. (Run with --stacktrace for more details.) |
问题排查了一下,发现 Robolectric 4.6.x 引入了 bcprov-jdk15on-1.68.jar ,这个库是使用 Java 15 编译的。
如果项目在 gradle.properties 中配置了 android.enableJetifier=true (目的是转换第三方不支持 AndroidX 的依赖,用以使整个工程升级到 AndroidX ), 当 Jetifier 小于等于 Jetifier 1.0.0-beta10时,无法反编译高于 Java 13 编译的代码库,进而引起上面的报错。
目前(2021/07/22)最新的 Android Studio 4.2.2 ,Android Gradle构建工具(AGP)版本
1 |
com.android.tools.build:gradle:4.2.2 |
依旧没有完成适配工作。
官方回复是 Android Gradle构建工具(AGP) 7.x版本修复,估计要到Android Studio 4.3 版本了。
目前的解决方法是在 gradle.properties 中配置 Jetifier 忽略 bcprov 库。比较庆幸的是 bcprov 库跟 AndroidX 没什么关系,因此忽略这个库是安全的。
如下:
1 2 3 4 |
android.enableJetifier=true android.jetifier.blacklist=org.robolectric.*,bcprov # Android Studio 4.2 版本开始使用 android.jetifier.ignorelist 替代 android.jetifier.blacklist |