How to use a Gradle dependency with local modifications? - android

I have an Android application using an Android library. The library is a pretty big open-source project on GitHub, and its authors publish the artifacts to Bintray. I can specify the dependency with the usual syntax dependencies { implementation 'group:artifact:version' } in the app's build.gradle.
Now I want to change some code in the library. I git clone it on my machine, I make my changes, then I build the library. But how can I tell my app to use the library I built locally, instead of the one in Bintray?
I don't want to follow the approach in Gradle Local Project Dependency, because that means that the library code is now part of the application project, but I really want to keep things separated.
I think the solution involves publishing to a local Maven repository. I followed the guide at https://proandroiddev.com/tip-work-with-third-party-projects-locally-with-gradle-961d6c9efb02 but the app's Gradle is still picking the original library from Bintray.

Bintray-based projects have the install task. That's the one to be used instead of publishToMavenLocal.
When using install, the artifact version is automatically set to X.X.X before publishing to the local repository. Therefore, in order for the app to pick up the local library, you have to edit the implementation row to group:artifact:X.X.X.
As the guide https://proandroiddev.com/tip-work-with-third-party-projects-locally-with-gradle-961d6c9efb02 suggests, you also need to add mavenLocal() as the first entry in the repositories section in the top-level build.gradle of the application.

Related

How does publishing android libraries that can be imported easily by other people work?

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.

Gradle - How my own library can be used in the dependencies section

I'm developing my own library for android. I want to allow other developers to use my library via the dependencies section in the gradle file.
What should I do in order to enable this?
Thanks.
You need to deploy your library to Bintray and link your package to JCenter (that's the default repository configured when you create a new project with Android Studio).
Here are some instructions on how to get started.
Please note that Bintray does not support SNAPSHOTs, but you can get a free account in oss.jfrog.org if you have a Bintray package linked to JCenter.
Here a project example that has all the pieces in place (look for gradle scripts in /gradle directory)

Android Gradle dependency on a forked GitHub project

I've pulled in a third party custom ListView library into my Android Gradle project. I initially added the project as a gradle library dependency from the jcenter repo. But now I forked the GitHub project and I'm making changes to it.
The original project is no longer maintained, so submitting a pull request is not going to work, I really need my own fork.
What would be a nice way to set this dependency up using Gradle?
I thought of putting the ListView library under the same GitHub repo as my project, but that seems messy, I do want to keep my fork as a separate library.
Another thing I thought about was checking them both out at the same level, and using ".." in my Gradle config to get to the library from my app. This means that if I have a collaborator (and I may soon) they either need to tweak the config to suit them or check things out in the same way I did.
Or I could publish to a repo like mavenCentral or jcenter, but I'm still working on it, so that doesn't sound good either.
Is there a cleaner option that I'm missing?
A simple solution would be to publish your library with JitPack. You just would need to create a GitHub release and have a build file in your repository.
JitPack is a maven repository that pulls in packages from GitHub repositories. It checks out your code and builds it.
Another option is Gradle Source Dependencies.
Basically you declare a dependency on a Git repository:
sourceControl {
gitRepository("https://github.com/gradle/native-samples-cpp-library.git") {
producesModule("org.gradle.cpp-samples:utilities")
}
}
and Gradle will clone and build that project.
Then you can add it as a dependency:
dependencies {
implementation('org.gradle.cpp-samples:utilities') {
version {
branch = 'release'
}
}
}

Android studio - Gradle - GitHub Repo

Does anyone have an idea if it's possible to use a github repo as a dependency, without it being published to maven central.
Let's say I'm developing an android library that has it's own github repo. I'd like to be able to "compile" this library has gradle dependency, in my android studio project, without having to publish to maven central (at least for the moment).
In other words : I want to use a dependency that is not on maven central. It's a straight github repo (an android library that also uses gradle).
I'd like my build.gradle to do something like this :
dependencies {
// Google Play Services (normal dependency)
compile "com.google.android.gms:play-services:5.2.08"
// The library I want to pull from github
compile "path_to_my_github_repo"
}
Thanks!
You can do that with Jitpack with Maven, Gradle and sbt.
However, I would strongly suggest to use binary artifact instead so that you are guaranteed that it is the same upon each build of your application, you control the artifact storage and you are using the official release of a project and not some downstream build. It will also make your builds much faster and more stable.
Publishing to the Central Repository is free, easy and well documented at e.g. http://central.sonatype.org/pages/producers.html and specifically for Gradle at http://central.sonatype.org/pages/gradle.html. You can also find lots of real world examples on the Nexus community site.
If you do not control the project you want to consume, I would suggest to send these pointers to the project and maybe even help them with a pull request ;-)
This should be possible and there is an unofficial gradle plugin called Gradle Git Repo plugin that claims to do what you're looking for. Note however, that I did not play with it myself to verify that it works.
What you need to use is a binary repository. It will contain your dependencies during development.
If you plan to publish your package to jcenter (and maven central) eventually, you can get a free Artifactory account in oss.jfrog.org.
Once doing that, your CI server can deploy your dependency to it and Gradle will resolve it from there.

add library from Git to Android Studio

I know this must be a pretty basic question, but I'm new to Android Studio and gradle, and I can't find any up-to-date info on this.
I'm trying to add this library to my project: android-segmented-control.
It doesn't look like I can add it to my build.gradle file (correct?). I'd like to do it that way, of course, and not download the project if possible.
If I do need to download the project, how do I link it up with my existing project? Again, I haven't been able to find anything that is current that describes this process for Android Studio 0.5.3
The library you mentioned does not seems to be pushed on maven central or any other maven repository. As this library contains resources files, you cannot add it as a jar.
The only way to use it is clone the git repository and add it as a module to your android app project.
Meanwhile, you can ask the author to make it available on a Maven repository like OSS sonatype
Thanks #Thomas Bouron for the hint !
I have pushed my library to maven center, so you just need to add the following dependency to your build.gradle.
dependencies {
compile 'info.hoang8f:android-segmented:1.0.0'
}
(A little late for #workInAFishBowl but it may be helpful for others.).

Categories

Resources