sources of Testing Support Library in Android Stud

2019-04-10 14:16发布

How to attach sources from android.support.test.* for debugging in AS?
Tried downloading sources from https://android.googlesource.com/platform/frameworks/testing but the version doesn't seem to match my testing library version.

Testing sources (for instance AndroidJunitRunner) don't seem to be available via sdk manager, am I missing something ?

3条回答
Fickle 薄情
2楼-- · 2019-04-10 14:59

I encountered a similar problem and it took a quite amount of time to figure it out. It seems like a bug due to a missing Gradle task not executed because the SAME configuration used to work but not any more after upgrading to AS v1.2+.

First, the following dependency is obsolete.

androidTestCompile 'com.android.support.test:testing-support-lib:0.1'

And it is updated as follows in the documentation.

androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
...

The defaultConfig should include the following line as usual.

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Also, make sure the Android Support Repository is installed through the SDK Manager.

If android.support.test.* cannot be resolved, then manually execute the Gradle task as follows.

  1. Click the Gradle tab on the right.
  2. Collapse your Android module and brow the Tasks node.
  3. Double click to execute other->generateDebugAndroidTestSources

If succeeded, the issue may be solved. At least, it works for me.

UPDATE:

It seems like there are still chances this could happen on AS 2.1.2. To be noted, if you have more than one Android modules, running the gradle task generateDebugAndroidTestSources from one particular should be enough for all, especially the Android library module one.

enter image description here

查看更多
我命由我不由天
3楼-- · 2019-04-10 15:07

You can use Espresso for UI-tests and Robolectric + JUnit + Mockito for unit-tests.

Use AndroidJunitRunner, add it to build.gradle like:

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

Use some package android.support.test.* you need add dependencies like

dependencies {
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0') {
        exclude module: 'support-annotations'
    }
}
查看更多
SAY GOODBYE
4楼-- · 2019-04-10 15:16

I suffered this problem recently and I solve it now. After I add the following dependencies and Sync Project with Gradle file, I can not find any relative library in the External Library folder.

androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'

What I do is to replace all androidTestCompile to compile and Sync Project with Gradle file. and I can see the relative libraries.

Last, I replace compile back to androidTestCompile and Sync Project with Gradle file again. The relative libraries are still there.

I want to post images to make it more clearly, but I am new in here and can not post images. Hope this will help you.

查看更多
登录 后发表回答