sources of Testing Support Library in Android Studio - android

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 ?

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.
Click the Gradle tab on the right.
Collapse your Android module and brow the Tasks node.
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.

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.

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'
}
}

Related

dexmaker gradle sync fails, and symbol not resolved

gradle sync keeps failing with my android studio. need help
https://github.com/linkedin/dexmaker This is the open source i am trying to use called dexmaker.
I tried to download by using
androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
but it gets errors like this
Failed to resolve: com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.1
I finally tried
androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.19.1'
this works but in my java code
The problem is even though i succeeded on syncing gradle, i still can't use the opensource.
DexMaker dexMaker = new DexMaker();
DexMaker gets red lines and if i click it it says
cannot resolve symbol 'DexMaker'
what's the problem?
I'm not sure, but as I understand your situation:
You add dependency using androidTestCompile directive. That means that this package will become available only inside android test classes. For more understanding each dependency could be added in three ways: compile, testCompile and androidTestCompile.
compile : Dependency is available everywhere in your application code.
testCompile : Dependency available only in regular tests.
androidTestCompile : Dependency available only in android tests.
So if you want that dependency to be available in your application - replace androidTestCompile with compile. But I not sure, that you SHOULD do that, cause that library is for tests.
P.S. Using compile directives is deprecated and you should use implementation, testImplementation and androidTestImplementation.

build.gradle warning 'avoid using + in version numbers'

This is my build.gradle app-level file.
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "it.myco.app.myproj"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Android Studio highlights the line
compile 'com.android.support:appcompat-v7:26.+'
with the message
avoid using + in version numbers can lead to unpredictable and
unrepeatable builds
This line is auto-generated by Android Studio. Why this warning message? What I need to write to solve the warning ?
Someday, someone else may do a git pull on your code and then try build an apk. This apk may have different behavior or even get a build error or run time error, because the dependencies version can be different than the dependencies used when you created this code.
You should use an explicit dependency version. I suggest use the newest version. You can search on bintray jcenter to see the newest version. If you are using Android Studio, File -> Project Structure -> Modules -> (your module, app usually) -> Dependencies -> Add library dependency (click the + sign) will give you the newest version, even add library automatically.
You can use something like
compile 'com.android.support:appcompat-v7:26.1.0'
Replace it with the latest version of the library. Usually AS shows the latest version when you alt + enter on the warning.
More info on the reasons
Don't use + in dependency version. it means Android studio will use the latest library, which may cause problem in future. In android studio it's recommended to use all android library with same version, suppose you are using + and if any single library got updated then android studio will use that updated library, so the version for this library will be change. that cause problem to build application.
That's why gradle giving this warning. it's best practice to write full version number instead of +.
Why this warning message?
Here you can find a good blog about this topic.
Dynamic versions add nondeterminism to your build:
Dependencies can unexpectedly introduce behavior changes to your app.
The same source built on two
different machines can differ.
Similarly, builds built on the same machine but
at different times can differ.
Past builds cannot be
reproduced perfectly. This makes it difficult to revert safely.
There
are security implications if a bad actor introduces a malicious
version of a dependency.
What I need to write to solve the warning ?
Always specify your dependency versions explicitly.
It's a normal warning.
It's better you to choose a concrete version than going that way. This is because if you use "+" it will choose the newest, so in the new version it's possible you to get some deprecated or unexpected changes that will do your app to die. Think that if you are using it in a huge project you will have a lot of dependencies so it will be a chaotic environment.
Use specific versions and if there is a new one... update it manually.
To solve the warning and choose the last version, if you are using Windows, click Alt+Enter and it will choose the latest version.
If it doesn't work you will have to search for it on the Internet or in Project Structure > Modules >(your module) > Dependencies > +

Android Studio 3.0 Canary 1: Gradle Sync Error

I'm getting this error in my Kotlin project:
Here is my app's Gradle files:
I haven't really done anything to the project yet except add Kotlin and Anko dependencies. Not sure what's happening...
This is a well known issue with Anko. It is mentioned here.
You can try to exclude implicit com.google.android:android dependency from Anko's dependencies:
compile("org.jetbrains.anko:anko-appcompat-v7:$anko_version") {
exclude group: 'com.google.android', module: 'android'
}
(Keep in mind as you are using separate Anko libraries - you may need to use exclusion in multiple pleces).
You can also try to update Gradle plugin:
classpath 'com.android.tools.build:gradle:3.0.0-alpha2'
Add the dependency inside you app-level app module :
// Anko
compile 'org.jetbrains.anko:anko-sdk15:0.8.2' // sdk19, sdk21, sdk23 are also available
compile 'org.jetbrains.anko:anko-support-v4:0.8.2' // In case you need support-v4 bindings
compile 'org.jetbrains.anko:anko-appcompat-v7:0.8.2' // For appcompat-v7 bindings
As In your screenshot I can see that while adding Anko dependencies you didn't mention the Anko version , kindly mention that it should work fine post that.
Add like this too.
flavorDimensions "default"
productFlavors {
debug {
dimension "default"
...
}
release {
dimension "default"
...
}
foss {
dimension "default"
...
}
}
AFAIK, its Google Repository which is missing. As you can see the ide itself is informing you about it.
Failed to resolve: com.google.android:android.2.3.1 which is Google Repository.
If you are connected to internet then simply click on Install Repository and Sync project in the Gradle Sync window. It will download the google repository and sync your project.

How can I find the dependencies in my app that are using findbugs? (findbugs conflict)

I'm trying to discover the specific conflict when adding espresso to my app's gradle file:
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.1') {
exclude group: 'com.android.support', module: 'support-annotations'
}
Android Studio states "Warning:Conflict with dependency 'com.google.code.findbugs:jsr305'.
Dependency conflict error in my Android app which has Android Tests states the error means the dependency I am using in my app is version 3.0.0 while the one in my test app is 2.0.1.
However, my gradle never explicitly adds "com.google.code.findbugs", indicating it was part of another dependency I added to my "compile" and "androidTestCompile" statements. How do I find the dependencies in my app that are using findbugs?
Check your dependencies:
HelloApp/
app/
- build.gradle // local gradle config (for app only)
...
- build.gradle // global gradle config (for whole project)
- settings.gradle
- gradle.properties
Check here:
dependencies {
compile project(':libraries:lib')
}
Later check this LINK you have Unit testing support orientation
Execute the following command:
./gradlew app:dependencies
This will print a (large) graph of dependencies.
For the meaning of the arrows and stars, please refer to this SO answer.

How to run ProGuard on Android tests?

We have a project with some library (and native) dependencies, like this:
Native SDK ← Library (Wrapper) ← Main Project
To start with, this structure cannot be changed, as we are reusing the parts. The problem I am facing is passing the 65k reference limit. This, of course, has a workaround - enable ProGuard. With it enabled, the project compiles.
Since we are transitioning to the Android's default testing framework, we need to add some more dependencies to the testing config, so in dependencies we now have:
compile 'com.google.android.gms:play-services-base:7.5.0'
compile 'com.google.android.gms:play-services-gcm:7.5.0'
compile 'com.google.android.gms:play-services-safetynet:7.5.0'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1'
compile files('libs/small-library1.jar')
compile files('libs/small-library2.jar')
compile files('libs/small-library3.jar')
compile files('libs/medium-library1.jar')
compile files('libs/medium-library2.jar')
compile files('libs/medium-library3.jar')
compile files('libs/huge-library1.jar')
compile files('libs/huge-library2.jar')
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
We are using SDK (API) 22, so everything is pretty much at the latest version. The problem is, our native SDK has a bunch of code in the protobuf layer, and the library wrapper is big. With all other JARs, we are too high over the 65k limit (but as I said, ProGuard fixes this barely). Multi-dex is out of the question as it works well only on Android 5.0+.
We're trying to reduce the codebase, but even then, Android Tests are failing with method reference overflow issues (i.e. not compiling).
Is there any way to enable ProGuard for tests as well?
One option is to change your test build using testBuildType to the release build or another build variant that has ProGuard enabled. See Gradle User Guide. You can also try the solution here, but I have not tried that myself.

Categories

Resources