Loading
0

Android Studio项目中使用AndroidX支持库及JUnit Test的相关配置说明

修改gradle.properties文件,加入下面2行:

android.useAndroidX=true
android.enableJetifier=true

修改app的build.gradle文件:

defaultConfig中加入

defaultConfig {
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"     
}

dependencies中加入

dependencies {    
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

建立一个测试基类如下:

@RunWith(AndroidJUnit4.class)
public class BaseTest {
    
    protected Context context;
    
    @Before
    public void before() {
        context = InstrumentationRegistry
                      .getInstrumentation()
                      .getTargetContext(); 
    }
}

测试类继承自该基类即可。

参考:

项目升级到AndroidX

最后编辑于:2020/3/30作者: joycode

我不入地狱,谁入地狱?

评论已关闭