In the following simple build.gradle file, i have some basic questions.
1.In repositories, when i specify mavenCentral(), is it the repository where all the libraries that i specify like compile 'com.android.support:support-v4:21.0.2'will be searched? Is there anything else for this apart from mavenCentral()? And what is the url required for after that(oss.sonatype..)?
2.What are the items that should be specified in classpath? Why cant the classpath items be specified like we specify the support libraries?
3.And for using a third party library, i had to specify an Amazon AWS URL at the bottom in allProjects() section. Why is this URL required?
buildscript {
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.jimdo.gradle:gradle-apt-plugin:0.2-SNAPSHOT'
}
}
allprojects {
repositories {
mavenCentral()
maven {
url "https://s3-ap-southeast-1.amazonaws.com/abc-release/abc/"
}
}
}
1.In repositories, when i specify mavenCentral(), is it the repository where all the libraries
No, you also have an implicit local Maven repository (usually at ~/.m2) which can also be searched for locally installed packages. The newer Android Studio builds use jcentral rather than Maven central, but the concept is the same: a central repository for packages.
2.What are the items that should be specified in classpath? Why cant the classpath items be specified like we specify the support libraries?
These are build tool (i.e. gradle) dependencies. Your app's dependencies are outlined in the app module's specific build.gradle file.
3.And for using a third party library, i had to specify an Amazon AWS URL at the bottom in allProjects() section. Why is this URL required?
Because their libraries are not in Maven central. So you're effectively pointing Maven to an outside repository you wish to use for your app and any of its libraries.
1.In repositories, when i specify mavenCentral(), is it the repository where all the libraries that i specify like compile 'com.android.support:support-v4:21.0.2'will be searched?
Yes, but only if it isn't in buildscript block.
Everything inside buildscript is used by build system – gradle and possibly some libraries for it.
allprojects.repositories is place where you declare repositories where libraries used by your application will be searched.
Related
I tried to use JitPack to add a forked library to my Android project.
allprojects {
repositories {
jcenter()
google()
maven { url 'https://jitpack.io'}
}
}
After sync, my library was added correctly, but the problem is, the newest version of dependencies like Picasso and okio was retrieved, other than the version specified in gradle file.
implementation 'com.squareup.okio:okio:1.9.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
Also tried to use includeGroup to only retrieve my own library via JitPack, but it still get the newest packages somehow.
maven { url 'https://jitpack.io'
content {
includeGroupByRegex "com\\.github\\.myusername.*"
}}
I assume it's a maven repository problem but don't really understand what's going on. Any suggestion is welcomed!
Maven dependencies are specified as
groupid:artifact:version
On most Maven repositories, they are taken from the pom.xml of the library.
JitPack works differently here:
the groupid identifies the git hosting service and user, e.g. com.gitlab.johndoe
artifact is the project name on that git hosting service, as found in the URL
version is a git ref, i.e. a tag or branch name, or a commit hash
There are ways to keep these in sync:
for the domain that corresponds to your groupid, configure a TXT record with a URL which points to the corresponding user or organization at the git hoster
choose the artifact name and project name to be the same
tag each release in git with the version number it has in pom.xml.
Otherwise, you will have to modify your dependencies so JitPack will find them.
when I sync my Android project, I keep seeing the following messages:
Gradle: Download https://s3.amazonaws.com/moat-sdk-builds/com/google/android/gms/play-services-ads-base/maven-metadata.xml
Gradle: Download https://s3.amazonaws.com/moat-sdk-builds/com/google/android/gms/play-services-measurement-base/maven-metadata.xml
Gradle: Download https://s3.amazonaws.com/moat-sdk-builds/com/google/firebase/firebase-iid/maven-metadata.xml
These libraries should be found in google() repo, which is the first one in my settings:
allprojects {
repositories {
google()
jcenter()
// ...
maven { url "https://s3.amazonaws.com/moat-sdk-builds" }
}
}
However, it looks into maven { url "https://s3.amazonaws.com/moat-sdk-builds" } and wastes a lot of time. What's going on here? And is there any way to debug it? Thanks.
You can try to customize dependency resolution behaviour or declare repository filters.
Declaring a repository filter is as easy as this:
allprojects {
repositories {
google()
jcenter()
// ...
maven {
url "https://s3.amazonaws.com/moat-sdk-builds"
content {
// Does only include this group
includeGroup "moat.sdk"
}
}
}
}
There is also the option to exclude groups and enhance for example the build performance.
Take care that "Matching repositories to dependencies is an incubating feature." The API documentation provide more information about filter options.
You can find more information on the specific behaviour you experience below. When it comes to dependency resolution Gradle does inspect repositories in order.
How dependency resolution works
[...]
Given a required dependency, Gradle attempts to resolve the dependency by searching for the module the dependency points at. Each repository is inspected in order. Depending on the type of repository, Gradle looks for metadata files describing the module (.module, .pom or ivy.xml file) or directly for artifact files.
[...]
But as i understand it gradle 'visits' each repository irrespective of whether it has already found the 'correct' artifacts or not.
Once each repository has been inspected for the module, Gradle will choose the 'best' one to use. This is done using the following criteria:
For a dynamic version, a 'higher' concrete version is preferred over a 'lower' version.
Modules declared by a module metadata file (.module, .pom or ivy.xml file) are preferred over modules that have an artifact file only.
Modules from earlier repositories are preferred over modules in later repositories.
When the dependency is declared by a concrete version and a module metadata file is found in a repository, there is no need to continue searching later repositories and the remainder of the process is short-circuited.
[...]
Introduction to Dependency Management - How dependency resolution works
In Gradle project, we can define multiple remote / local Maven repository.
buildscript {
repositories {
mavenLocal()
mavenCentral()
jCenter()
maven {
url 'https://example1.mavenrepo.com/public'
}
maven {
url "https://example2.mavenrepo.com/release"
}
}
dependencies {
classpath 'com.example.mydependencies:mylibrary:1.0.0'
}
}
If mylibrary exist in all of Maven repo. Which one will Gradle choose? Can I configure Gradle to only download mylibrary in certain Maven repo?
As you can find in the doc
A project can have multiple repositories. Gradle will look for a dependency in each repository in the order they are specified, stopping at the first repository that contains the requested module.
I don't think there is a way to point to the gradlew to use particular repository or order it. Gradlew by design uses a single repository for all dependencies. If any of them fail, the build fails.
[Refer]: https://github.com/gradle/gradle/issues/7965
I want to make android dependencies path of my project. For, example I made one library demo for AnimatedTextView now I want to use that demo in my other project as compile path like this.
dependencies {
compile 'com.zerocool.animatedtextdemo:AnimatedTextView:1.0'
}
I don't know how to make this path. If some one want to use my this library then I don't need to give demo library. Just give this compile path so they can use this. I don't have any idea for this.So suggest me what I have to do...?
To make this work, you need your library in a repository that is identified in the buildscript like this:
buildscript {
repositories {
mavenCentral()
jcenter()
}
}
This creates a reference to a repository where you can put external dependencies. See the documentation here: https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html
For example, if you want to host your own repository, you can create a maven server, add your library and then put this in the gradle.build:
buildscript {
repositories {
maven {
url "http://repo.mycompany.com/maven2"
}
mavenCentral()
jcenter()
}
}
If you then put your library in the repo at http://repo.mycompany.com/maven2, gradle will check that repo when it tries to resolve dependencies and it should find your library.
Note that you can reference multiple repository locations, and some are "well known" so you do not have to specify the URL. There are many ways to "host" your repo, but that is how you allow for the type of behavior that you asked about. See this reference here: https://docs.gradle.org/current/userguide/dependency_management.html#sec:repositories
Say I need to use some proprietary jars in my android library. I want my library to be conveniently available from Maven Central, but I can't just put dependencies there due to legal issues.
I figured it's possible to use Internal Repository to host dependencies so they would be resolved automatically.
I've used Github repo, just like this one, and declared it in library pom.xml
However Gradle doesn't seem to be resolving this dependencies. If I manually declare my repository in main build.gradle everything works fine. Am I doing something wrong here, or android gradle plugind just don't support internal repositories?
It's discouraged in the Maven community to have repository declarations in published POMs, and Gradle won't honor them. Instead, downstream builds will have to declare the internal repository in one way or another (which shouldn't be a big deal).
If you (only) publish POMs for proprietary dependencies to Maven Central (which is a common solution to this problem at least if you own the dependencies), downstream builds will need to declare the internal repository as follows:
repositories {
maven {
url "https://repo1.maven.org/maven2"
artifactUrls "https://some.internal.repo"
}
}
If you don't publish proprietary dependencies to Maven Central at all (not even POMs), downstream builds will have to declare the internal repository as another regular Maven repository:
repositories {
mavenCentral()
maven {
url "https://some.internal.repo"
}
}
Note however that last time I checked, one of the rules of publishing to Maven Central was that dependencies needed to be available from Maven Central as well.
PS: Whether you are publishing a Java or Android library shouldn't matter here.