How to create a library on Github and use it through gradle dependencies in Android Studio
Asked Answered
S

3

29

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?

Starbuck answered 17/12, 2015 at 7:9 Comment(1)
Does this answer your question? Add github library as dependency to Android-Studio projectSliest
R
14

Refer Jitpack is best to import your project or libs from Github to gradle

For more information refer Gabriele Mariotti answer

Rai answered 17/12, 2015 at 7:11 Comment(4)
Thanks for the quick response!Include
While this may answer the question, it is better to provide the actual information here and not just a link. Link-only answers are not considered good answers and will probably be deleted.Jumbuck
How can I do it if I want the library to take only a folder inside the library repository? ... Like github.com/username/library/folder .... Take what's inside 'folder' only? .. Is it possible? Thanks in advanceStanzel
JitPack is very useful for public repositories. You can read some examples in this post: solidgeargroup.com/jitpack-public-repositories-androidNerve
P
22

To achieve it you have some ways:

  1. publish your library (artifact) in central maven or jcenter.
  2. use a github repo and the jitpack plugin
  3. 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:

Puttee answered 17/12, 2015 at 7:42 Comment(4)
once you mark it as a compile time dependency, as above, and add it to the classpath, assuming it's also a run-time dependency, where is the JAR? It should show in build/libs/?Retain
It is not a jar. You should find the library in build/intermediates/exploded-aarPuttee
I would love it if you could take a look at my question here. Following the simple JitPack instructions did not work there: https://mcmap.net/q/145968/-jitpack-won-39-t-use-a-github-repo-included-sample-repo-that-demonstrates-the-issueDenisdenise
JCenter is not an option anymore, it seems.Aedile
R
14

Refer Jitpack is best to import your project or libs from Github to gradle

For more information refer Gabriele Mariotti answer

Rai answered 17/12, 2015 at 7:11 Comment(4)
Thanks for the quick response!Include
While this may answer the question, it is better to provide the actual information here and not just a link. Link-only answers are not considered good answers and will probably be deleted.Jumbuck
How can I do it if I want the library to take only a folder inside the library repository? ... Like github.com/username/library/folder .... Take what's inside 'folder' only? .. Is it possible? Thanks in advanceStanzel
JitPack is very useful for public repositories. You can read some examples in this post: solidgeargroup.com/jitpack-public-repositories-androidNerve
T
2

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:

  1. Create the Android library
  2. Test to make sure the library is usable locally
  3. Publish the library on Bintray
  4. 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.

Tanny answered 25/1, 2017 at 9:23 Comment(1)
JCenter is not an option anymore, it seems.Aedile

© 2022 - 2024 — McMap. All rights reserved.