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.
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.
The problem is, I previously used a github repository (https://github.com/kenglxn/QRGen) as my dependency:
compile 'com.github.kenglxn.QRGen:android:2.4.0'
It worked fine but now i have forked the repository, made some changes and I'm trying to use my repository as dependency, my repository: https://github.com/prasenjeetpaul/QRGen, I'm trying as:
compile 'com.github.prasenjeetpaul.QRGen:android:2.4.0'
I have sync the project many times but couldn't use my repository code, even after removing the old dependency it is still using the old code but not the new code from my github repository.
Is there a way to remove the old dependency code and use my new repository code??
Please help!
Uploading your files to GitHub will not make your code to be available to be included as a dependency. First, make sure that your project is a library, and follow these steps to upload your library to Bintray's jcenter() repository so that your library can be included as a dependency.
I prefer to use bintray to upload my libs to jcenter repository but the process a little bit complicated so you may use jitpack to publish new dependency immediately
Just add jitpack repo link to root build.gradle and it will work
allprojects {
repositories {
...
maven { url 'https://jitpack.io' } // add this line
}
}
When you want to use a library from GitHub that the owner tells you to copy something like this to your dependencies and then you have access to it:
compile 'com.1gravity:android-rteditor:1.6.2'
How can I make this kind of URL for my library?
You have to publish to some public library repositories like jcenter, Maven Central or jitpack.
I want to create the library and have access to it through the Internet.
In Android Studio (via Gradle) dependency may be added in this way:
In build.gradle (Module app):
dependencies {
...
compile 'com.android.support:design:23.1.0'
compile 'com.squareup:otto:1.3.8'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.j256.ormlite:ormlite-android:4.48'
...
}
How can I add my own library in this way from github?
To achieve it you have some ways:
publish your library (artifact) in central maven or jcenter.
use a github repo and the jitpack plugin
use a private maven
The point 2. is very simple.
Just push your codein github and modify the gradle script in the project where you want to use it.
Just add this repo tp your build.gradle
repositories {
// ...
maven { url "https://jitpack.io" }
}
and the dependency:
dependencies {
compile 'com.github.User:Repo:Tag'
}
To publish a library in Central Maven or JCenter, it is very long to explain in an answer. Hovewer you can read these posts:
Publish on JCenter
Publish on Central Maven. Another blog for Central Maven
Refer Jitpack is best to import your project or libs from Github to gradle
For more information refer Gabriele Mariotti answer
For a quick solution, as the others have said JitPack is probably the way to go. However, if you want to make your library available to a wider audience you should probably add it to jcenter since this is set up by default in Android Studio now. (Previously it was Maven Central.)
This post gives a detailed walkthrough of how to do it. The following is a summary:
Create the Android library
Test to make sure the library is usable locally
Publish the library on Bintray
Add the library to Jcenter
Then all people will have to do to use your library is add a one liner to their build.gradle dependencies.
I have a repository on Github, I would like to use my repository in Android Studio using: dependencies {compile 'com.google.code.gson: Gson: 2+'} for gradle. Does someone know how to do this?
You could use JitPack.io to expose your repository as a Maven dependency. Then including it in Android Studio is easy with gradle:
compile 'com.github.YourUsername:RepoName:ReleaseVersion'
There are more instructions on the website.
I don't know if I really got the question...I understood that you want to compile some jar files that are not local to your project and are hosted in your repository. If it is the matter, I guess you should use a custom maven repository, not a Github one. If this is the problem I can give you more details on how to create a custom maven repo.
It's like #dometheshooter said.
See:
How to publish aar file to Apache Archiva with Gradle
Guide to publish an aar to maven using gradle
How to deploy JAR to Maven remote repository
Guide to uploading artifacts to the Central Repository
Guide to deploying 3rd party JARs to remote repository