我们用Robolectric测试的话需要在gradle中进行配置,国内的详细教程太过简易,而且很多是低版本下的配置方案。所以经过本人的仔细摸索,找到了现在高版本中的配置方案,主要还是参考了官网的配置教程:https://github.com/robolectric/robolectric-gradle-plugin

下面贴上完整的步骤,看完后就能配置成功了~

下面红字部分就是要添加的部分。

首先,在最外面的build.gradle中配置classpath

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
} allprojects {
repositories {
jcenter()
mavenCentral()
}
}

然后在,项目的build.gradle中进行如下配置:

apply plugin: 'com.android.application'
// 1.test plugin
apply plugin: 'org.robolectric'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1" defaultConfig {
applicationId "kale.testapplication"
minSdkVersion 15
targetSdkVersion 18
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
} packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'LICENSE.txt'
}
} dependencies {
androidTestCompile 'junit:junit:4.12'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
// 2. test-libs
androidTestCompile 'org.robolectric:robolectric:2.4'
} // 3. test settings
robolectric {
// Configure includes / excludes
include '**/*Test.class'
exclude '**/espresso/**/*.class' // Configure max heap size of the test JVM
maxHeapSize = '2048m' // Configure the test JVM arguments - Does not apply to Java 8
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier' // Specify max number of processes (default is 1)
maxParallelForks = 4 // Specify max number of test classes to execute in a test process
// before restarting the process (default is unlimited)
forkEvery = 150 // configure whether failing tests should fail the build
ignoreFailures true // use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for ${descriptor.name} with result: ${result.resultType}"
}
}

参考自:

http://blog.yohei.org/android-studio-gradle-robolectric1/

https://github.com/robolectric/robolectric-gradle-plugin

在Android Studio中用Gradle添加Robolectric的更多相关文章

  1. 在Android studio中用gradle打 jar 包(Mac下)

    这两天公司要重构项目,以前的项目在eclipse上,准备迁移到Android studio上,需要对项目打包,于是我学习了Android studio中gradle打包的内容.我在公司用的Mac,在家 ...

  2. Android studio下gradle Robolectric单元测试配置

    android studio下gradle Robolectric单元测试配置 1.Robolectric Robolectric是一个基于junit之上的单元测试框架.它并不依赖于Android提供 ...

  3. android studio 使用gradle 导出jar包,并打包assets目录

    警告:本文年久失修. 随着android studio的升级 ,gradle的升级,严格按照本文的代码去做可能不会成功,希望依然可以作为解决问题的思路. 最近项目在做一个sdk,供别的开发者使用,所以 ...

  4. Android Studio :Android Studio 与 Gradle 深入【二】

    转载:http://www.apkbus.com/forum.php?mod=viewthread&tid=255063&extra=page%3D2%26filter%3Dautho ...

  5. [转]--android studio 使用gradle 导出jar包,并打包assets目录

    转自: http://www.cnblogs.com/wuya/p/android-studio-gradle-export-jar-assets.html   最近项目在做一个sdk,供别的开发者使 ...

  6. Android 项目利用 Android Studio 和 Gradle 打包多版本APK

    在项目开发过程中,经常会有需要打包不同版本的 APK 的需求. 比如 debug版,release版,dev版等等. 有时候不同的版本中使用到的不同的服务端api域名也不相同. 比如 debug_ap ...

  7. 利用 Android Studio 和 Gradle 打包多版本APK

    在项目开发过程中,经常会有需要打包不同版本的 APK 的需求. 比如 debug版,release版,dev版等等. 有时候不同的版本中使用到的不同的服务端api域名也不相同. 比如 debug_ap ...

  8. 快速掌握 Android Studio 中 Gradle 的使用方法

    快速掌握 Android Studio 中 Gradle 的使用方法 Gradle是可以用于Android开发的新一代的 Build System, 也是 Android Studio默认的build ...

  9. Android studio的gradle

    1. gradle的基本概念 gradle构建* Android Studio使用`Gradle`构建工具,Eclipse的ADT插件使用的是`Ant`构建工具* 构建:生成app的过程,执行一些的命 ...

随机推荐

  1. VPN有什么用?(转载)

    VPN有什么用?转载自:http://www.chinaz.com/web/2012/0320/240819.shtml 当你非常非常想访问一个国外的网站,而这个网站被防火墙屏蔽的时候,你应该怎么做呢 ...

  2. .NET面试必备(整理)

    1.简述 private. protected. public. internal 修饰符的访问权限. private : 私有成员, 在类的内部才可以访问.public : 公共成员,完全公开,没有 ...

  3. MSSQL大数据量增加字段耗时对比

    单个数据表记录数为1亿4千万条. 一.测试同时增加两个允许为空的字段. ALTER TABLE [dbo].[XRecord] ADD [sType] int,[cType] int GO 开始时间: ...

  4. udp穿透简单讲解和实现(Java)

    在上一小节中了解到了通过浏览器自带的Webrtc功能来实现P2P视频聊天.在HTML5还没有普及和制定Webrtc标准的前提下,如果要在手机里进行视频实时对话等包括其他功能的话,还是要自己实现,还比较 ...

  5. PE渲染引擎 三

    加进了SSAO,讲真这个东西,很容易忽略他的存在.并且动态的话,会有闪烁. 下面两幅图,单独给你看一张,应该看不出去区别....依旧是浓重风格,这个tongmaping,哪位大神指教下.....

  6. STL中priority_queue小结

    (1)为了运用priority_queue,你必须包含头文件<queue>:#include<queue> (2)在头文件中priority_queue定义如下: namesp ...

  7. Tip8:Unity中诸如 Awake() Start() Update()等函数的 执行顺序

    Unity脚本中有很多的事件函数,下面是各种函数的执行顺序: 1.reset(); 2.Awake(); 3.OnEnable; 4.OnLevelWasLoaded(); 5.Start(); 6. ...

  8. gulp-imagemin图片压缩----gulp系列(三)

    本节实现图片压缩,在实现压缩前,先配置images任务,设置源目录和输出目录. 在系列(二)代码的基础上,再进行扩展. 1.找到gulp->config.js,对images进行源目录(src- ...

  9. Swift_3.0_取消杂乱无章的log输出

    一 举例: 输出的杂乱无章的东西 subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: , persist_lev ...

  10. MySQL5中大数据错误:Packet for query is too large (****** > ******). You can change this value on the server by setting the max_allowed_packet' variable.;

    使用的MySQL数据库版本:5.5 插入或更新字段有大数据时(大于1M),会出现如下错误: ### Cause: com.mysql.jdbc.PacketTooBigException: Packe ...