最近因为因为工作的需要,简单了解了下Cordova这个hybird app应用平台。
因为用Cordova create出来的Android工程目录和Android Studio工作目录结构不一样,所以在加Test的时候也遇到一些坑,在此简单进行分享。
首先我们来看下

默认的Android工程目录结构:


从图中不难看出AndroidTest的目录结构是在src下的:src/androidTest/java/xxx
local unitTest默认的结构是src/test/java/xxx,local unit test和AndroidTest的切换在侧栏的Build Variants里面。在默认的工程里面我们不需要在sourceSets里面进行配置了。

Cordova创建的目录结构


在Cordova的工程中,可以看到讲assetsresAndroidManifest.xml文件都放在了与src目录同一级的目录下面,这个是与默认工程目录结构不一样的地方。当然如果这样放的话,我们就要添加sourceSets来配置了:

sourceSets {main {manifest.srcFile 'AndroidManifest.xml'java.srcDirs = ['src']resources.srcDirs = ['src']aidl.srcDirs = ['src']renderscript.srcDirs = ['src']res.srcDirs = ['res']assets.srcDirs = ['assets']jniLibs.srcDirs = ['libs']}androidTest {java.srcDirs = ['androidTest']}

其中指定了各种各样的文件所在的路径,在这里我们就不能将于Test相关的文件放到main下面了,除非你单独指定java.srcDirs=[‘src/xxx/xxx’]。在这里我单独将androidTest文件放在了外面。
引用依赖包:

androidTestCompile ‘com.android.support:support-annotations:23.0.1’ androidTestCompile ‘com.android.support.test:runner:0.4.1’ androidTestCompile ‘com.android.support.test:rules:0.4.1’

然后进行android Test run,直接在test中右键run test进行了。这时候我报错了:

trouble processing “java/com/dev/AppInfoTest.class”:
Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.
This is often due to inadvertently including a core library file
in your application’s project, when using an IDE (such as
Eclipse). If you are sure you’re not intentionally defining a
core class, then this is the most likely explanation of what’s
going on.
However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.
If you really intend to build a core library – which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application – then use
the “–core-library” option to suppress this error message.
If you go ahead and use “–core-library” but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.
If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.
1 error; aborting
Error:Execution failed for task ‘:dexDebugAndroidTest’.

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java” finished with non-zero exit value 1

然后就开始google…
终于找到了答案,我将androidTest拷贝到与src目录同级下面,虽然在sourceSets中进行了指定,但是在这里与AS默认创建的工程不一样,androidTest下面不能再加上java的目录,如果加上了,在test里面package的名字就变成了,package java.xxx.xx这个是默认的工程里面是xxx.xxx所以默认的test目录结构没有问题,在这里我们需要把java的目录给删了同时去掉test中package java.xxx.xxx为xxx.xxx,估计和jdk的package冲突。 详见:StackOverFlow
下一步,还是开始运行test,提示我们“no test found”,对比了下Android的官方文档

android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } }

发现少了上面那个,见Android Test
最后run一次,就ok了

在Cordova中自定义AndroidTest(Instrumented Unit Test)相关推荐

  1. [Test apps on Android] Build instrumented unit tests

    本文是翻译的Test apps on Android的官方文档 Build instrumented unit tests 本文不照搬每一个单词,理解有误请跳转原文链接. Build instrume ...

  2. Cordova实现自定义下载插件和WPS在线预览

    Cordova实现自定义下载插件和WPS在线预览 简要描述 点击下载链接,请求URL,判断手机是否安装WPS,如果安装则调用wps在线编辑,没有安装则对文件进行下载,并在通知栏进行通知,显示下载进度( ...

  3. Android中自定义视图View

    标签: 前言 好长时间没写blog了,心里感觉有点空荡荡的,今天有时间就来写一个关于自定义视图的的blog吧.关于这篇blog,网上已经有很多案例了,其实没什么难度的.但是我们在开发的过程中有时候会用 ...

  4. Android中自定义视图View之---前奏篇

    前言 好长时间没写blog了,心里感觉有点空荡荡的,今天有时间就来写一个关于自定义视图的的blog吧.关于这篇blog,网上已经有很多案例了,其实没什么难度的.但是我们在开发的过程中有时候会用到一些自 ...

  5. Android 仪器化单元测试(instrumented unit tests) Androidx kotlin版本

    前言 近期需要进行单元测试,测试内容需要真机环境,所以需要使用instrumented unit tests,用来在跑在真机上进行测试. 本blog用于记录. 简介 仪器化单元测试(instrumen ...

  6. Android测试系列之Instrumented Unit Test-Espresso

    Instrumented unit tests are unit tests that run on physical devices and emulators, instead of the Ja ...

  7. Android Instrumented Unit Tests (AndroidTests)

    參考官方文件Build instrumented unit tests Cannot resolve symbol 'androidx.*' / 'org.junit.*' Android Studi ...

  8. spark中自定义UDAF函数实现的两种方式---UserDefinedAggregateFunction和Aggregator

    1.基于UserDefinedAggregateFunction实现平均数的计算 package com.bigdata.wb.sparkimport org.apache.spark.sql.Row ...

  9. 数据库中自定义排序规则,Mysql中自定义字段排序规则,Oracle中自定义字段排序规则,decode函数的用法,field函数的用法

    数据库中自定义排序 场景:有一张banner表,表中有一个status字段,有0, 1, 2三个状态位,我想要 1,0,2的自定义排序(这里是重点),然后再进行之上对sequence字段进行二次排序( ...

最新文章

  1. 学习-SQL查询连续号码段的巧妙解法--转载
  2. pyspark minHash LSH 查找相似度
  3. 如何通过三视图判断立方体个数_如何通过接触角判断疏水性?疏水性与亲水性的区别...
  4. 题解 guP2421 【[NOI2002]荒岛野人】
  5. cx+oracle+sql含中文,sql 判断字段值是是否包含中文字符
  6. li 字多出了省略号_文字溢出自动显示省略号css方法 -
  7. WPF 如何实现颜色值拾取
  8. 从Pycharm说起
  9. 从高排到低变成小楼梯儿歌_幼儿早教三字儿歌,帮助宝宝启蒙学说话
  10. mongodb windows的安装方法和添加到任务管理器中、检测是否成功、增删改查命令...
  11. 【转】性能测试设计和LR原理的探讨
  12. 使用用WCF中的双工(Duplex)模式将广告图片推送到每个Winform客户端机子上
  13. 中国政法大学政治与公共管理学院丛日云教授在毕业典礼上的演讲在网络上暴红,
  14. Spring Cloud 2020年路线图发布
  15. 我能取得成就的原因和不足之处
  16. 这双 Googler 设计的 Nike 鞋真的是──丑爆了
  17. 七战DTCC铸成ACE Director 去哪儿网周彦伟畅聊数据库的十八般武艺
  18. 快捷键没有响应的处理办法
  19. 微信公众号第三方平台开发PYTHON教程 PART 1
  20. 你知道的企业文化理念有哪些?

热门文章

  1. IT职业教育(13)什么是职业教育?
  2. JAVA 2022.7.12课程总结
  3. Android开发 之 曲线运动动画(贝塞尔曲线)
  4. 二.使用JDBC对数据库CRUD
  5. 用HT7333成功替代BL8505,稳定提供的3.3V供电-----关注静态电流!!!
  6. 视频号封禁大量网课培训 社群营销直播间
  7. ciscn 2022 华东北分区赛pwn duck
  8. 猜灯谜_全排列板子题(A 村的元宵节灯会上有一迷题: 请猜谜 × 请猜谜 = 请边赏灯边猜 小明想,一定是每个汉字代表一个数字,不同的汉字代表不同的数字。 请你用计算机按小明的思路算一下,然后)
  9. NodeJS 爬虫笔记一,熟悉爬虫相关模块
  10. 电流速断保护c语言程序,TQXDB-III多功能继电保护实验培训系统实验指导书2.doc