加速Robolectric下载依赖库

看到这篇博客的朋友也许已经看过简书写的一篇加速Robolectric下载依赖库及原理剖析,反正我是看过并且按照里面的步骤进行操作,发现不管是从Android Studio build sync还是命令行上gradlew test,均无法成功从阿里云下载镜像。从命令行执行打印日志可以发现抛出异常

java.lang.RuntimeException: Method clear in android.util.SparseArray not mocked. See http://g.co/androidstudio/not-mocked for details

看到这个莫名其妙的异常,找了报错的类以及打开那个超链接,也没明白需要我做什么,后来我又想起以前也是使用某某某镜像站地址,把地址的协议头https改成了http,所以就报错了,所以把

1
http://maven.aliyun.com/nexus/content/groups/public/修改为https://maven.aliyun.com/nexus/content/groups/public/

果不其然就下载成功了。也许17年的镜像站没有上https所以才没报错吧,反正现在好了。后来发现从robolectric官方配置发现并不需要重写类这么繁琐的方法,只需要在build.gradle加入以下配置:

1
2
3
4
5
6
7
8
9
10
testOptions {
    unitTests {
        includeAndroidResources true
        all{
            systemProperty 'robolectric.dependency.repo.url', 'https://maven.aliyun.com/nexus/content/groups/public/'
            systemProperty 'robolectric.dependency.repo.id', 'alimaven'
        }

    }
}

就可以重写默认的镜像地址,达到一样的效果,注意要在终端执行gradlew test先把包下载下来,以后Android Studio可正常调试,第一次直接Android Studio运行还是会从默认镜像地址下载,原因未知,欢迎知道的朋友解答一下。

1