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?

  • Does this answer your question? [Add github library as dependency to Android-Studio project](https://stackoverflow.com/questions/21798694/add-github-library-as-dependency-to-android-studio-project) – Mahozad Jun 07 '22 at 15:29

3 Answers3

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:

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 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/`? – Thufir Mar 10 '16 at 10:42
  • 1
    It is not a jar. You should find the library in build/intermediates/exploded-aar – Gabriele Mariotti Mar 10 '16 at 11:37
  • I would love it if you could take a look at my question here. Following the simple JitPack instructions did not work there: https://stackoverflow.com/questions/44841415/specific-jitpack-issue – Gazihan Alankus Jun 30 '17 at 07:54
  • JCenter is not an option anymore, it seems. – Nicolas Raoul Jan 02 '22 at 04:29
14

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

For more information refer Gabriele Mariotti answer

M D
  • 47,665
  • 9
  • 93
  • 114
  • Thanks for the quick response! – Владимир Широков Dec 17 '15 at 07:21
  • 6
    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](http://stackoverflow.com/help/deleted-answers). – elixenide Dec 23 '15 at 16:10
  • How can I do it if I want the library to take only a folder inside the library repository? ... Like https://github.com/username/library/folder .... Take what's inside 'folder' only? .. Is it possible? Thanks in advance – Jahir Fiquitiva Feb 08 '16 at 20:47
  • 3
    JitPack is very useful for public repositories. You can read some examples in this post: https://solidgeargroup.com/jitpack-public-repositories-android – José Carlos Jan 31 '17 at 08:00
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.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393