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

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

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

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

  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2.  
  3. buildscript {
  4. repositories {
  5. jcenter()
  6. mavenCentral()
  7. }
  8. dependencies {
  9. classpath 'com.android.tools.build:gradle:1.1.0'
  10. classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'
  11. // NOTE: Do not place your application dependencies here; they belong
  12. // in the individual module build.gradle files
  13. }
  14. }
  15.  
  16. allprojects {
  17. repositories {
  18. jcenter()
  19. mavenCentral()
  20. }
  21. }

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

  1. apply plugin: 'com.android.application'
  2. // 1.test plugin
  3. apply plugin: 'org.robolectric'
  4. android {
  5. compileSdkVersion 22
  6. buildToolsVersion "22.0.1"
  7.  
  8. defaultConfig {
  9. applicationId "kale.testapplication"
  10. minSdkVersion 15
  11. targetSdkVersion 18
  12. versionCode 1
  13. versionName "1.0"
  14. }
  15. buildTypes {
  16. release {
  17. minifyEnabled false
  18. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  19. }
  20. }
  21.  
  22. packagingOptions {
  23. exclude 'META-INF/LICENSE'
  24. exclude 'META-INF/LICENSE.txt'
  25. exclude 'META-INF/NOTICE'
  26. exclude 'META-INF/NOTICE.txt'
  27. exclude 'LICENSE.txt'
  28. }
  29. }
  30.  
  31. dependencies {
  32. androidTestCompile 'junit:junit:4.12'
  33. compile fileTree(dir: 'libs', include: ['*.jar'])
  34. compile 'com.android.support:appcompat-v7:22.1.1'
  35. // 2. test-libs
  36. androidTestCompile 'org.robolectric:robolectric:2.4'
  37. }
  38.  
  39. // 3. test settings
  40. robolectric {
  41. // Configure includes / excludes
  42. include '**/*Test.class'
  43. exclude '**/espresso/**/*.class'
  44.  
  45. // Configure max heap size of the test JVM
  46. maxHeapSize = '2048m'
  47.  
  48. // Configure the test JVM arguments - Does not apply to Java 8
  49. jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'
  50.  
  51. // Specify max number of processes (default is 1)
  52. maxParallelForks = 4
  53.  
  54. // Specify max number of test classes to execute in a test process
  55. // before restarting the process (default is unlimited)
  56. forkEvery = 150
  57.  
  58. // configure whether failing tests should fail the build
  59. ignoreFailures true
  60.  
  61. // use afterTest to listen to the test execution results
  62. afterTest { descriptor, result ->
  63. println "Executing test for ${descriptor.name} with result: ${result.resultType}"
  64. }
  65. }

参考自:

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. Qt 添加资源文件

    *本人乃小白,博文主要用于个人记录,不保证内容准确无误* 我们编写的gui可能需要一些额外的资源(比如贴图用的图片),可用资源文件统一管理.以下以图片为例. 用qt creator 打开工程,为工程新 ...

  2. Android View绘制过程

    Android的View绘制是从根节点(Activity是DecorView)开始,他是一个自上而下的过程.View的绘制经历三个过程:Measure.Layout.Draw.基本流程如下图: per ...

  3. 安卓序列化漏洞 —— CVE-2015-3525

    在2014年,Jann Horn发现一个安卓的提权漏洞,该漏洞允许恶意应用从普通应用权限提权到system用户执行命令,漏洞信息与POC见(1].漏洞的成因源于在安卓系统(<5.0)中,java ...

  4. 同时支持控制台和MFC窗口程序的APP

    BOOL CMyApp::InitInstance() { if ( m_bShowGui==FALSE ) { FILE *stream = NULL; AllocConsole(); // 开辟控 ...

  5. WCF学习之二

    今天在照着网上教程做练习的时候,用IE浏览器运行的时候遇到了一些问题 “找不到类型“WCFService.Service1”,它在 ServiceHost 指令中提供为 Service 特性值,或在配 ...

  6. Entity Framework的默认值BUG解决方法

    在使用.Net 3.5里的Entity Framework开发网站的时候,遇到了一个问题:添加记录时,对于DateTime型的数据,无法使用数据库的默认值. 具体的情况是这样的,我的数据库有个User ...

  7. Sprint回顾-0525

    1.回顾组织 主题:“我们下次怎么样才能更加认真对待?” 时间:设定为1小时. 参与者:整个团队. 场所:宿舍走廊. 秘书:团队队长秘书,筹备.记录.整理.   2.回顾流程    Sprint总结: ...

  8. easyui-treegrid节点选择

    easyui-treegrid本身不能实现选中父节点子节点全选,必须通过另外的方法来实现,这里说下如何通过修改节点样式添加checkbox来实现级联选择效果 首先需要格式化节点的样式 formatte ...

  9. 给文本框添加模糊搜索功能(“我记录”MVC框架下实现)

    步骤: 1.在文本框中输入内容时,触发keyup事件: 2.在keyup事件的处理方法中,通过Ajax调用控制器的方法: 3.在控制器方法中,搜索满足条件的数据,这里分页获取数据,且只取第一页的数据, ...

  10. 三种对话框的示例(alert,confirm,prompt)

    示例代码 <h2>JavaScriptDialog</h2> <hr/> <buttononclick="btn_alert()"> ...