What is default dependency scope in Gradle on Android? - android

In Gradle in Android Studio I noticed providing a dependency scope is optional. For example:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
'org.hamcrest:hamcrest-core:1.3'
'org.hamcrest:hamcrest-library:1.3'
}
Notice the very last two libraries don't have a compile scope attached. I left it blank and I was still able to sync gradle. What is the default scope if nothing is specified here ?

The scope is actually a label for a given dependency configuration. It depends very much on the gradle plugins you are using (i.e.: java plugin or android plugin).
If you don't add any configuration label, it will be saved as an unlabeled dependency.
Most of the time if you need a implementation dependency and do not add the label, your build will break. If it doesn't break it could be because:
You were not actually needing the dependency
You are using a gradle plugin that handles nicely unlabeled dependencies
Or (more probably), the dependency is already on your build cache or partial build and therefore the compiler is still able to find the classes, but will break if you clean the project.
Related documentation on dependency configuration for gradle

There is a small update to Logain's answer. compile is deprecated now. implementation can be used to replace it.
https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management

Related

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"))
…
}

What is "implementation" in Kotlin Gradle dependencies?

I'm using Android Studio 3.0 Preview to start new Kotlin project. As I try to add dependencies in build.gradle I saw implementation scope instead of usual compile.
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:25.3.1'
testImplementation 'junit:junit:4.12'
There's also androidTestImplementation and testImplementation scope.
In the end, I add compile to add third party dependencies and it works.
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
So my questions are..
What is implementation, androidTestImplementation, and testImplementation scope?
Is it any different than compile, testCompile, and androidTestCompile?
Which one I should use for my Kotlin project?
Edit:
My bad, this question is not Kotlin specific. It's the new Android Gradle Plugin configuration.
This is not specific to Kotlin, but has to do with the new Gradle plugin for Android.
compile, provided and apk are now deprecated.
Use implementation or api instead of compile, compileOnly instead of provided, and runtimeOnly instead of apk.
The reason for this is to speed up multi-module builds. Given module A which depends on module B which in turn depends on module C, a change in module C would trigger a recompile of module A as well. If A does not use C directly, there is no need for A to recompile when C changes.
The implementation configuration ensures exactly this: if you specify implementation project(':C') in B, you cannot access C from A and you avoid building unnecessary modules. In a large multi-module project this can save a lot of time.
See Migrate to the new Gradle plugin for more information.
Earlier version of gradle v3.0.0-alpha1 used to use compile but it has been deprecated now on.
Why?
Dependencies appearing in the compile 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.
Let's take an example to understand this. Let's say, I created a Library_Image_Upload that supports Image uploading to the server. I used Library_Network lib in Library_Image_Upload that supports all the network operations. My library only makes use of image uploads and provide a convenient way of uploading images. Now as i used Library_Network lib in my Library_Image_Upload project, everyone using this lib will have functionality of Image Uploading along with all network operations that someone may also use(Important). Later on i thought there is a better alternative to Library_Network as Library_Magic_Image and used it. So all the API functions exposed by Library_Network are gone and whoever is using those functions has broken build.
implementation comes with several benefits:
Dependencies do not leak into the compile classpath of consumers anymore, so you will never accidently 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).
To learn more read The Java Library Plugin
So i think you have the answer of all three questions.
I hope it helps.

Getting Latest Library Dependency - Android

I'm new to Gradle and I need to make sure that my Android project has the latest dependencies with regards to RxAndroid and RxJava. Forgive me for my ignorance but could someone explain how I go about to make sure that the libraries I include in my project are the latest ones? Where does Gradle downloads these libraries from? Is there a central repository? Is GitHub the source? It's not clear from the code shown below:
build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'io.reactivex:rxjava:1.0.14'
}
I would really appreciate if someone could shed some light with regards to this topic.
Gradle downloads them from Maven or other repos. I don't believe GitHub hosts the libraries themselves, but the link to download the libraries could be found on GitHub.
To ensure they are the latest version, you will have to go the library's web page(Git hub page usually) and check if a latest version has been released.
If I am not wrong, you could add a + sign at the end and when you sync Gradle with work in offline mode turned off, it should fetch the latest version of the library.
Eg:
compile 'com.android.support:appcompat-v7:23.3.0+'
compile 'io.reactivex:rxandroid:1.0.1+'
The above line will ensure that 1.0.1 is the min version that you will download and if there are any higher version, it will download that instead.
WARNING : this is not a safe thing to do, because the latest version of library will be automatically downloaded and the changes in the newer version of the library might have breaking changes, and break your app. For instance the methods you use from a library might be removed in the new library or even worse things could happen and it has to me. I suggest you set some time apart once every few weeks and see if there are new updates and consider if it is safe to update.
Deciding to add a library to your project must be carefully thought through, as you are making the project dependent on it and if that library dies out or is buggy, your project too will be screwed. Incase you didn't, I suggest you read this on how being careless with adding dependencies literally broke the internet.
You can just add a plus to the end of each, although it's not recommended as sometimes new libs will break your code.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0+'
compile 'io.reactivex:rxandroid:1.0.1+'
compile 'io.reactivex:rxjava:1.0.14+'
}
Here are the latest:
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'io.reactivex:rxandroid:1.2.0' // Upgraded
compile 'io.reactivex:rxjava:1.1.5' // Upgraded
testCompile 'junit:junit:4.12'
}
check here

Migrate from Eclipse to Android Studio import libraries

I'm trying to migrate an Android project developed with Eclipse ADT to Android Studio. I've already read the instructions mentioned here http://developer.android.com/sdk/installing/migrate.html and it works fine!
Gradle builds a new android project but I need to "hardcode"(modify manually) the gradle.build file in order to make the libraries work properly. All the other stuffs work fine.
This is the ADT project
MyApplication/
-->assets/
-->libs/
---->android-support-v7-appcompat.jar
---->android-support-design.jar
-->res/
-->src/
-->AndroidManifest.xml
-->project.properties
The first library is automatically recognized by Gradle, and it is substituted with
compile 'com.android.support:appcompat-v7:23.2.1'
in the grandle.build file.
Instead the "android-support-design.jar" is not recognized and it is added to the gradle.build as
compile file('libs/android-support-design.jar')
but it is not working at all.
At the moment, I need to manually substitute the
compile file('libs/android-support-design.jar')
with
compile 'com.android.support:design:23.2.1'
In order to make the build work effectively.
Is there any way to force Gradle to recognize that library and automatically import it? Can I download a version of that library that is recognized, anywhere? At the moment I'm taking both libraries from
<sdk>/extras/android/support/
Thank you all.
I need to "hardcode" the gradle.build file in order to make the libraries work properly
I'm not sure what you mean by "recognize" and "hardcode", but if you just have jar files that you can't use the compile line like for the support libraries, then you can use this line, which will take any jar file in the libs/ folder and compile it. You don't need to hard-code any of those.
compile fileTree(dir: 'libs', include: ['*.jar'])
Otherwise, you should already have these.
compile "com.android.support:appcompat-v7:23.2.1"
compile "com.android.support:design:23.2.1"
But, if you want to get fancy with Gradle, you can do something like this to keep all the support libraries the same version.
ext {
supportLibVersion = '23.2.1' // variable that can be referenced to keep support libs consistent
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:appcompat-v7:${supportLibVersion}"
compile "com.android.support:design:${supportLibVersion}"

How can I add BaseGameUtils to Chrome Cast project?

I'm trying to add a Leader Board to a Chrome cast project and am getting errors. Android project in Android Studio. In my gradle build - Error: more than one library with package name 'com.google.android.gms'
I understand why you don't want to use two different libraries with the same name, but not sure how to use the same library throughout the project. Here are the two uses of gms:
1) Main activity has dependency on 'CastCompanionLibrary-android-master' which then uses google-play-services_lib. I'm not sure which version of gms this uses, but the version number is referenced in the manifest. Is this just grabbing the version # of play services that they have installed on their phone?
2) BaseGameUtils - has dependency on com.google.android.gms:play-services:+ (I think this is grabbing the most recent version of play-services, but doesn't match the other one.
MainActivity gradle file
dependencies {
compile project(':BaseGameUtils')
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':CastCompanionLibrary-android-master')
}
CastCompanionLibrary-android-master dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':android-support-v7-appcompat')
compile project(':android-support-v7-mediarouter')
compile project(':google-play-services_lib')
}
BaseGameUtils dependencies {
compile 'com.android.support:appcompat-v7:20.0.+'
compile 'com.android.support:support-v4:20.0.+'
compile 'com.google.android.gms:play-services:+'
}
So, the problem (I think) is these two versions of com.google.android.gms, but how do I rectify it so that they all use the same version. I've had almost 2 years of working with Android, but this is my first question on stack overflow. Help is appreciated - Is there a guru out there that has the answer to this?
Seems like you have modified the gradle file for CCL since what you have there does not match with what CCL has in GitHub. The best approach is to only use the piece of play services that you need; for example CCL only needs the play-services-cast (besides the base, which will be pulled in automatically) so if you follow that pattern, things would look smaller (less possibility of running into the 64K dex limit) and less collisions.CCL, in Github, lists its dependency as:
compile 'com.google.android.gms:play-services-cast:6.5+' so you might want to start using versions and also follow the recomendation I just made (same applies for any other code that you have and uses play services; just pull in what you really need)

Categories

Resources