Use provide gradle dependency in Android Unit Test - android

I'm trying to integrate Dagger 2 to be used in unit tests. Unfortunately whenever I run my test I get java.lang.NoClassDefFoundError: javax/inject/Provider.
I have the javax dependency added in Gradle like this:
depdencencies {
...
provided 'javax.annotation:jsr250-api:1.0'
}
What I tried:
Changing provided to compile. Adding both compile and testCompile with this dependency. I also tried testProvided but there is no such thing so it didn't work either.
Apparently the problem is that by using provided this dependency is available only compile time. Unfortunately nothing else worked so I am open to any suggestions. If more information is needed, please ask. Thanks!

I use in AndroidStudio 2.2.3 dagger2 for unit testing with app/build.gradle
....
apt 'com.google.dagger:dagger-compiler:2.11'
compile 'com.google.dagger:dagger:2.11'
provided 'javax.annotation:jsr250-api:1.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
....

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.

How to declare annotation processor witch is inside of gradle module

I have modules in my Android project. One of modules (moduleA for example) use another (moduleB) as a dependency:
dependencies {
api project(':moduleB')
}
And also I still have apt and old gradle plagin. Now, I delete apt from gradle files, upgrade plugin version and get error:
Annotation processors must be explicitly declared now. The following
dependencies on the compile classpath are found to contain annotation
processor. Please add them to the annotationProcessor configuration.
- moduleB.jar (project :moduleB)
If we use some external dependency we do like this (for example) to resolve this problem:
compile 'com.google.dagger:dagger:2.8'
annotationProcessor 'com.google.dagger:dagger-compiler:2.8'
But what I need to do with my case, when annotattion processor is inside of gradle module? Could you explain it, because I don't have deep understanding in this area and usually I simply take this lines
compile 'com.google.dagger:dagger:2.8'
annotationProcessor 'com.google.dagger:dagger-compiler:2.8'
from repository of library provider. I try to research this case, but don't find anything similar.
project(':moduleB') is just a way to specify an inter-project dependencies, like you do with "full" coordinates (com.google.dagger:dagger:2.8). So, to use another module as an annotation processor just use
dependencies {
…
annotationProcessor(project(":moduleB"))
…
}

Android Architecture Components: Gradle sync error for dependency version

I'm trying to add ViewModel and LiveData to a Kotlin app. I have the following dependencies added to my module's build.gradle:
implementation "android.arch.lifecycle:extensions:1.1.1"
kapt "android.arch.lifecycle:compiler:1.1.1"
testImplementation "android.arch.core:core-testing:1.1.1"
I'm given the following error:
Android dependency 'android.arch.lifecycle:runtime' has different version for the compile (1.0.0) and runtime (1.1.1) classpath. You should manually set the same version via DependencyResolution
Removing the first line (extensions) fixes the issue, indicating that the error is coming from there, but I can't figure out why.
As #RedBassett mentions Support libraries depends on this lightweight import (runtime library) as explained at android developers documentation.
This is, android.arch.lifecycle:runtime:1.0.0 is spreading up in the dependency tree as a result of an internal api (transitive) import so in my case I only had to include extensions library as "api" instead of "implementation" so that it will override its version to the highest (1.1.1).
In conclusion, change
implementation "android.arch.lifecycle:extensions:1.1.1"
to
api "android.arch.lifecycle:extensions:1.1.1"
In your main build.gradle file
allprojects {
...
configurations {
all {
resolutionStrategy {
force "android.arch.lifecycle:runtime:1.1.1"
}
}
}
}
This will enforce version 1.1.1
Apparently support-v4 was causing the conflict. In the case of this question, the Gradle dependency task wasn't working correctly, but for anyone else who runs into this issue:
./gradlew :app:dependencies will show the sub-dependencies used by your dependencies. Search the output of this command (changing app for your module name) for the dependency causing the conflict.
#RedBassett is right. However I was still having some problem excluding android.arch.lifecycle related sub dependencies.
In my case the conflict was caused in com.android.support:appcompat-v7:27.1.1.
This is how my gradle dependency looks like after excluding it.
implementation ('com.android.support:appcompat-v7:27.1.1') {
exclude group: 'android.arch.lifecycle'
}
api "android.arch.lifecycle:runtime:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
Also, you will have to add this exclude in every imported module.
I searched for all dependencies with ./gradlew :app:dependencies as #RedBassett mentioned. I noticed the incompatible version of android.arch.core:runtime that Gradle was complaining about was stemming from my version of com.android.support:appcompat-v7, so I just updated that version to the latest and everything worked.

Kotlin build cannot get classes created by SquidDatabase

In my project I use SquidDatabase almost everywhere to access the database.
I want to transform my project to Kotlin but when I build there are lots of "Unresolved reference: DepartmentInfo" errors.
That DepartmentInfo class was created by SquidDatabase while building so I think this problem is caused by Kotlin building before other libraries.
I failed to find any way to change Kotlin to the end of buildlist.
How can I solve this problem?
I posted this issue on Github and got reply from developers.
The solotion is here:
build.gradle of your app
//If using kotlin language,please add this
apply plugin: 'kotlin-kapt'
......
dependencies {
compile 'com.yahoo.squidb:squidb:3.2.3'
compile 'com.yahoo.squidb:squidb-annotations:3.2.3'
compile 'com.yahoo.squidb:squidb-android:3.2.3' // For Android projects only
//annotationProcessor 'com.yahoo.squidb:squidb-processor:3.2.3'
// If using the android-apt plugin, this becomes
// apt 'com.yahoo.squidb:squidb-processor:3.2.3'
// If using kotlin language, this becomes
kapt 'com.yahoo.squidb:squidb-processor:3.2.3'
}
It may cause errors if you use DataBinding for Andriod,and the solution is to add one more kapt in your dependencies:
kapt 'com.android.databinding:compiler:$gradle_version'

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