Gradle auto resolve dependencies - android

Does gradle automatically imports the dependencies of the dependencies?
For example in an Android application I have those:
dependencies {
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
#######compile 'com.google.code.gson:gson:2.3'
}
okhttp and retrofit use Gson.
Do I have to include Gson manually or it is resolved by gradle automatically?
If I include Gson manually, does gradle imports the library twice and thus increasing my application size?

So far Retrofit itself used GSON and it should import the GSON dependencies automatically once you compile 'com.squareup.retrofit:retrofit:1.9.0'.
When using Retrofit together with OkHttp, OkHttp (version 1.6.0 or newer) and OkHttp-UrlConnection is required.
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.squareup.okhttp:okhttp:2.0.0'

Related

Dagger2 annotation processor is not working

I try to use Dagger2 in my Android project;
When I use apt, every things is right.But apt is not supported in AndroidStudio 3.0, so I use annotation processor.But no Dagger2 code created after I click "Make Project";
And I'm sure the annotetion processing is enable in AndroidStudio,because the Butterknife annotation processor is all right.
the follow is the build.gradle:
dependencies {
annotationProcessor 'com.google.dagger:dagger-android-processor:2.13'
compile 'com.google.dagger:dagger-android:2.13'
compile 'com.google.dagger:dagger-android-support:2.13'
compile 'com.jakewharton:butterknife:8.6.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
}
use this in android studio 3.0 version
implementation 'com.google.dagger:dagger:2.9'
annotationProcessor 'com.google.dagger:dagger-compiler:2.9'
these are the dependences for dagger2 and butter in andriod studio 3.0
//ButterKniffe
compile "com.jakewharton:butterknife:8.8.1"
kapt "com.jakewharton:butterknife-compiler:8.8.1"
//dagger
compile "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
It is one of the breaking changes coming with gradle:3.0 that Google announced at IO17 gradle:3.0
the compile configuration is now deprecated and should be replaced by implementation or api
From the gradle docs :
Dependencies appearing in the api configurations will be transitively exposed to consumers of the library, and as such will appear on the compile classpath of consumers.
Dependencies found in the implementation configuration will, on the other hand, not be exposed to consumers, and therefore not leak into the consumers' compile classpath. This comes with several benefits:
List item dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive dependency
faster compilation thanks to reduced classpath size
less recompilations when implementation dependencies change: consumers would not need to be recompiled
cleaner publishing: when used in conjunction with the new maven-publish plugin, Java libraries produce POM files that distinguish exactly between what is required to compile against the library and what is required to use the library at runtime (in other words, don't mix what is needed to compile the library itself and what is needed to compile against the library).
The compile configuration still exists but should not be used as it will not offer the guarantees that the api and implementation configurations provide.

Can't compile okhttp with jack enabled

My project was working great but then i decided to enable jack, after this i got the following error:
The type com.squareup.okhttp.OkHttpClient cannot be found in source files, imported jack libs or the classpath.
I'm using build tools 25.0.1 and my gradle dependencies are:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
Please note that if i disable again jack the project works.

Android Studio Can't Fetch Library

I have recently started getting this issue with Android Studio and it has been driving me up the wall. I keep getting the error "Failed to resolve: com.squareup.okio:okio:1.8.0". These are the dependencies I have:
dependencies {
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile files('libs/okio-1.8.0.jar')
compile files('libs/retrofit-2.1.0 (1).jar')
}
As you can see, I have the retrofit and gson libraries simply compile (I'm assuming this downloads them from the internet), I tried doing the same with Okio however it didn't resolve, so I have speicifcally downloaded the 1.8.0 Okio JAR however it still fails to resolve. Why could this be? Any answer would be greatly appreciated. Thank you
Don't put dependencies as a Gradle dependency:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
And as a .jar file in src/libs:
compile files('libs/retrofit-2.1.0 (1).jar')
at one time. Choose only one method. I advise you to choose Gradle dependencies, because they are automatically fetched.
In you case, the dependencies section should look like:
dependencies {
compile `com.google.code.gson:gson:2.7`
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
}

Cannot resolve symbol 'RequestQueue'

I am new to android studio and volley library so please bear with me.
I've added volley library by GitHub and then and added this line to build gradle file:
compile 'com.android.support:appcompat-v7:23.0.1'
And now I am trying some tutorials about request and response from api, but for some reason I cannot make an instance of RequestQueue why?
I am using Android 4.0.3 IceCreamSandwish
Thank you
add following to your build.gradle file
compile 'com.mcxiaoke.volley:library:1.0.19'
Google have added it to their own documentation pages and GitHub https://developer.android.com/training/volley/index.html
The dependency can be added using:
dependencies {
...
implementation 'com.android.volley:volley:1.1.0'
}
Add this volley repo clone to your gradle dependencies:
compile 'com.mcxiaoke.volley:library:1.0.19'
https://github.com/mcxiaoke/android-volley
Add the Volley library as a dependency in the module build.gradle file.
dependencies {
compile 'com.he5ed.lib:volley:android-cts-5.1_r4'
}
This is another mirror of the Google Volley library https://github.com/he5ed/volley
I would add it in the build.gradle:
compile 'com.mcxiaoke.volley:library:1.0.19'
you were adding
compile 'com.android.support:appcompat-v7:23.0.1'
which is a completely different thing (a backwards COMPATibility library for older API levels).

How to use different versions of the same gradle dependency in different Android Studio modules?

In my Android Studio project, I have two modules app and library.
Therefore I also have two build.gradle files.
build.gradle(app):
dependencies {
compile 'com.squareup.okhttp:okhttp:2.4.0'
...
}
build.gradle(library):
dependencies {
compile 'com.squareup.okhttp:okhttp:2.3.0'
...
}
When I call some retrofit methods in my library module, it uses okhttp 2.4.0 instead of 2.3.0. Why is this the case and how can I change this?
Retrofit most likely has a transitive dependency on okhttp and uses 2.4.0.
You might try
compile ('com.squareup.retrofit:retrofit:2.0.0-beta1'){
exclude module: 'okhttp'
}
compile 'com.squareup.okhttp:okhttp:2.3.0'}
to force retrofit to use the okhttp dependency that you declared yourself.
I don't know though if Retrofit relies on a certain version / implementation of OkHttp and will work properly if it is changed.

Categories

Resources