Android测试中遇到的问题及解决方案

1.build报了一个错误:Failed to resolve : com.android.support:appcompat-v7:26.4.0

运行项目时报了一个错误,如下图

报错图片

应用位置

产生原因:

分析错误的英文,未能解析,由于对安卓开发并不是很了解,百度了很多资料,
1.检查了一下SDK的版本号以及主gradle的配置文件中的引用版本,确认没有问题
2.根据配置文件中的私服地址,去maven的私服上看没有找到要引用的包,确认了是由于下载不了包导致的问题

解决方案:

    implementation 'com.android.support:appcompat-v7:26.4.0' 变为 implementation 'com.android.support:appcompat-v7:26.1.0'

2.升级了AS和SDK后,报了很多错,如下:
2.1.can't resolve symbol R

不好用的解决方案:
将idea.properties 中的idea.max.intellisense.filesize=5000 未解决问题

2.2.Caused by: com.android.ide.common.process.ProcessException: Failed to execute aapt

问题截图:

image.png

2.3.Failed to resolve: com.android.support:appcompat-v4:28.1.0

问题截图:

image.png

image.png

问题分析:
因为升级了AS和SDK 所以原因大概在环境上
2.1,2.1,2.3的解决方案:
因为代码是通过高优先级的AS和SDK调试的,拷到旧项目中会有问题,所以更新了项目所有模块当中的build.gradle配置文件,
1.将编译版本加到28 [compileSdkVersion]
2.将支持最大版本加到28[targetSdkVersion]
3.引入的包的版本号改到27 红色标记3
如下图:
image.png

全都是环境问题 哎~


3.Compilation failed; see the compiler error output for details.

错误: 无法访问InstrumentationTestRunner
找不到android.test.InstrumentationTestRunner的类文件

运行UIAutomator Case报错,如下图

image.png

问题原因:
Uiautomator 2.0没有UiAutomatorTestCase的方法了
后续查看原因

4.java.lang.RuntimeException: Minimum supported Gradle version is 4.6. Current version is 4.4. If using the gradle wrapper, try editing the distributionUrl in

/user/project/gradle/wrapper/gradle-wrapper.properties to gradle-4.6-all.zip

看英文说明 项目的Gradle和配置中的不一致
解决方案:
gradle-4.4-all 改成/user/project/gradle/wrapper/gradle-wrapper.properties to gradle-4.6-all.zip

Android5.0.2系统上面使用uiautomator dump 命令生成的xml文件中文乱码

4.4以及5.1都没有这个问题,对比Android5.0.2与4.4的源码得出,在/frameworks/testing/uiautomator/library/core-src/com/android/uiautomator/core/AccessibilityNodeInfoDumper.java的private static String stripInvalidXMLChars(CharSequence cs)方法存在差异,改方法就是将传入的字符转换为字符串

5.0.2的源码,方法中只支持英文字符,其它字符统统返回问号,
解决方法:
就是修改该方法,然后重新编译刷机,自己测试中文正常显示。知道原因,解决问题,over。

在AndroidTest中取Context

想在AndroidTest中取Context
找了以下四个方法

//Null
//    public static Context context = getInstrumentation().getContext();
//Null
//    Context context = InstrumentationRegistry.getContext();
//Null
//    Context context=InstrumentationRegistry.getInstrumentation().getContext();
// Not Null
    public static Context context = InstrumentationRegistry.getTargetContext();

你可能感兴趣的:(Android测试中遇到的问题及解决方案)