It is possible to easily use third party libraries with gradle. For example, the following allows me to use Retrofit in my app.
dependencies {
compile 'com.squareup.retrofit:retrofit:1.9.0'
}
How does this work? Where does the library come from? In general terms, how would I go about publishing a library so that other people can import it like this?
Note: this is not a duplicate of Publish jar library to bintray using gradle/publish-jar-library-to-bintray-using-gradle. That question was asking a spefic question about one particular way to publish libraries.
Lots of this is answered in this tutorial.
How does this work?
Gradle imports the libraries from a Maven repository. The Maven repository can contain both regular .jar files and regular .aar files.
Where does the library come from?
By default, new versions of Android Studio import from JCenter. JCenter is a Maven Repository run by the company Bintray.
If you look at your Android Studio project's build.gradle
, you'll see the following lines
repositories {
jcenter()
}
This tells gradle where it should look when attempting to import com.squareup.retrofit:retrofit:1.9.0
.
In general terms, how would I go about publishing a library so that other people can import it like this?
You need to create a Bintray account in order to upload to JCenter since Bintray owns JCenter. Bintray's website is pretty easy to use compared to what Maven Central, the past default Maven Repository used by Android Studio.
After you've created a normal Library module inside Android Studio, you'll need to hand tweak your library module's build.gradle
file in order to configure it for Maven. Finally, you use a pre-baked script to upload everything to Bintray.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With