Android_AndroidX简介&Junit4

AndroidX简介:

Android 9.0(API level 28)发布时,AndroidX也作为一个新的支持库发布出来了。AndroidX时Jetpack的一部分,它包含了现有的支持库和最新的Jetpack组件。

AndroidX 与 Android 操作系统分开提供,并与各个 Android 版本向后兼容。AndroidX 完全取代了支持库(android.support.v7或android.support.v4),不仅提供同等的功能,而且提供了新的库。所以对于使用AndroidX后,不需要因为使用v4还是v7库而烦恼了。

修改项目中的gradle.properties文件中将下面两个属性置为true就能完全使用AndroidX的库了。
android.useAndroidX:使用相应的 AndroidX 库,而非支持库
android.enableJetifier:重写其二进制文件,自动迁移现有的第三方库以使用 AndroidX

 

更多具体信息及如何使用请到android官网查看文档:

https://developer.android.google.cn/jetpack/androidx

 

Junit4:

这里简单记录分享下

背景:Android Studio中调试Room相关时碰到的问题(如 java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" )

Room调试中使用的是AndroidX的库,而默认Junit使用的是非AndroidX库,导致出现各种报错。理清这个,修改下依赖库即可解决问题。

1.在默认情况下,Android Studio使用的是非AndroidX支持库中的Junit的相关库,build.gradle(Module:app) 中关于Junit大概配置如下:

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.flx.testjunit"
        minSdkVersion 27
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

2.AndroidX库关于Junit的配置如下。

如AndroidX简介中所述,gradle.properties(Project Properties)中添加:

android.useAndroidX=true
android.enableJetifier=true

build.gradle(Module:app) 中:

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.flx.testjunitandroidx"
        minSdkVersion 27
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

修改配置后 重新Build下工程,具体类中报错重新import对应的库类即可。

 

运行Junit:

下面是简单的示例图片

从上面看:

(1)点击1处的Structure即可出现2的窗口,右击2处即可执行

(2)4和5处已经左边有明显的结果提示,如上”passed:1 of 2“。5处有具体报错信息。

(3)3处有androidTes和test两个文件夹,有什么区别呢,

androidTest/java: Android单元测试,执行测试的时候需要连接Android设备(或AVD),需要Android环境。
test/java: JUnit单元测试,不需要Android依赖,运行在当前电脑中。

 

 下图是便捷的创建测试:

 

转载于:https://www.cnblogs.com/fanglongxiang/p/11401123.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值