Jitpack repository retrieved wrong version of dependency - android

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.

Related

Gradle look into wrong maven repository

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

How to install my own Java library?

I created a Java class for helping me to use some common debuggin stuffs, for example to get the type of value, I have a method call typeOf()
Help help = new Help();
String s = "Something";
help.alert(help.typeOf(s));
above is a sample code in my class
Now every time I start a project, I need copy the class file to my project, so I want to know is there any way I can install this in my OS, then can import it to any of project whenever I needed.
In C you can move the header file to a location, then you can #include it whenever you want
If you using gradle, you can learn it from Building your own Android library but need to publish your library to jCenter or Maven Central.
Fortunately, you can make the library as a local artifact using maven plugin in the library. Read more at Deploying an Artifact to the Local Cache in Gradle
In your project, you need to add mavenLocal() to the repositories in your root build.gradle:
repositories {
mavenCentral()
mavenLocal()
}
And add the dependencies to your project build.gradle:
dependencies {
...
compile 'com.your.library:x.y.z'
...
}
But if your project is shared in your local network, you need to use Repository management like Artifactory by JFrog or Nexus Repository Manager by Sonatype.

Using personal library hosted on github as gradle dependency

I have an android library that is hosted on github and need to add it as a dependency to another project without manually cloning the repository and adding it as a module dependency. How do I go about creating my own gradle dependency with link from github? Thanks!
If you've pushed your code to GitHub then sharing your library is easy with JitPack.
Your users will just need to add the repository to their build.gradle:
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
and then your GitHub repository as dependency:
dependencies {
compile 'com.github.YourUsername:Repo:Release'
}
JitPack acts as a maven repository and can be used like Maven Central. The nice thing is that you don't have to upload your library. Behind the scenes JitPack will check out the code from GitHub and compile it. As you publish a new release on GitHub it becomes available for others to use.
There is also a guide on how to prepare an Android project.
You have to release your library to a repository which can be used by Gradle. When you want the library to be publicly available you can publish it to Maven Central. See http://central.sonatype.org/pages/gradle.html#releasing-the-deployment-to-the-central-repository for details about how to publish your library from gradle to Maven Central.
Once published in Maven Central use the normal gradle dependency declaration.
Github is not a maven repository.
if it's "free for all" license, you can clone project and post it for example in jCenter, then add it as gradle dependency.

Structure of build.gradle

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.

Android vs. Maven Internal 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.

Categories

Resources