How to publish android library to subversion and use it on projects - android

There are many posts about publishing android library in github. Is there any way to publish android library to the svn?
EDIT:
I have created a libray and build the aar file. I have imported the aar file in to a tag of the svn. (Ex: svnpath/project/tags/library0.0.1.aar)
I want to use this library in a separate project. I want to use this library as
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
like this. how can i achieve this?

As discussed in the comments, Subversion (SVN) is not an appropriate tool for what you are trying to achieve. Subversion is a version control system which manages the versioning of source code files and associated resources, much in the same manner as Git does.
What you are looking for is a dependency management system using Maven, for example Artifactory. This will allow you to publish your .aar files to either a public-facing or private repository and import those dependencies into your build.gradle file. Unfortunately the process for setting up such a service is too broad for the scope of this question, but once you have it up and running you can add it to your build.gradle file under the repositories section:
repositories {
maven {
url "<url of Maven server>"
credentials {
// If you choose to use authentication
username = <your artifactory username>
password = <your artifactory password>
}
}
}

Related

How to use a Gradle dependency with local modifications?

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.

Managing Android shared resources in repository

I have multiple Android projects that I need to manage in various Git repositories, all of which are being managed by repo. I have found a .gitignore file for Android managed by Git, but I don't see things like image files being ignored:
https://github.com/github/gitignore/blob/master/Android.gitignore
I have also read that creating an Android library which contains my resources would allow for a module that is not an .apk, rather a resource for apps to use.
https://developer.android.com/studio/projects/android-library.html
Is there a best practice for storing shared resources?
Creating an Android library project is trivial; distributing it to your other downstream apps is really the interesting part.
Ideally, it would be nice to do this in a way that mimcs how you use other dependencies on Android, namely adding an artifact to the dependencies block in your build.gradle file and having Gradle automatically fetch it. The trouble there is it's a private library that you wouldn't want to host on JCenter, Maven Central, or other public artifact hosting. Some companies have their own internal instances of artifact hosting servers specifically for this.
Fortunately, you can use the maven-publish Gradle plugin to use Maven locally (on your machine) and achieve the same effect. First, in the library project's build.gradle file, you need
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
groupId 'your.package.namespace'
artifactId 'library.name'
version '0.1' // for example
artifacts = configurations.archives.artifacts
artifact sourceJar
}
}
}
Next, you need to publish this library to your local Maven cache by running
./gradlew publishToMavenLocal
Next in your downstream project, add mavenLocal as a repository and add your dependency using the group id, artifact id, and version you used earlier:
repositories {
mavenLocal()
/* more repositories here */
}
dependencies {
compile 'your.package.namespace:library.name:version'
/* more dependencies here */
}
This should achieve what you want.
The downside is every developer will have to pull down the library project at least once and run the command to publish to their Maven local, and if the library project changes at all, the version must be updated, and all devs need to pull down the changes and publish again locally. This can get a little unwieldy.
Many companies run their own artifact hosting server instances internally to avoid this, so you publish once to the internal hosting and downstream projects just update the version in their build.gradle and let it sync automatically. This requires some additional configuration for the publishing block (of the library) and the repositories block (of the downstream projects); I leave it to you to research that if you intend to go that direction.

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.

what is the purpose of the init.gradle file

Can someone explain the meaning of the purpose of this init.gradle file as it seems like just duplicating code to me:
buildscript {
repositories {
maven {
url "https://someEnterpriseURL"
}
}
}
allprojects {
repositories {
maven {
url "https://someEnterpriseURL"
}
}
}
the reason for the confusion is that in the projects build.gradle file its defined like this:
buildscript {
repositories {
maven {
url "https://someEnterpriseURL"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.1'
}
}
allprojects {
repositories {
maven {
url "https://someEnterpriseURL"
}
}
}
so why even have this defined in the init.gradle file ? how does it help developers to have a init.gradle file when im defining the same thing in the build.gradle file?
Your build.gradle files are per-project and per-module. Your init.gradle files are set up on a per-${GRADLE_USER_HOME} basis (default is in ~/.gradle or in Gradle's home directory for local Gradle installations). I would expect to see init.gradle files used more commonly in large organizations, trying to standardize some Gradle build policies across multiple projects and development teams.
Quoting the Gradle documentation:
Here are several possible uses:
Set up enterprise-wide configuration, such as where to find custom plugins.
Set up properties based on the current environment, such as a developer's machine vs. a continuous integration server.
Supply personal information about the user that is required by the build, such as repository or database authentication credentials.
Define machine specific details, such as where JDKs are installed.
Register build listeners. External tools that wish to listen to Gradle events might find this useful.
Register build loggers. You might wish to customize how Gradle logs the events that it generates.
As you know gradle is combination of Ant tool and Maven tool. You might have seen how ant tool works(adding jars or libs locally) and maven tool(adding jars and libs file from cloud and writing script to download it from cloud during building). So basically this init.gradle file is generally used for some jars available globally(generally used for big organisations placed in cloud) which will contain the url or address of server so as to download the jar and cache it in gradle wrapper(you can see those jar files over cloud once downloaded can be seen in .gradle->caches).
Visualize in a prospect like suppose in an organisation which is have one support jar file which keeps updating every now and then and there is another team in that organisation which needs that jar for their application. So here our guy 'init.gradle' help us to solve this problem. Automatically download the latest jar from the server whenever application is build by the later team.

How to add a github library to gradle

I made a android library in github that i want to add to Gradle,
that other people can add
compile "my project ..."
to there android build.
how can i upload it to Gradle?
(I didn't find an answer for this anywhere)
EDIT:
To clear my question, I want to release a library for developers.
Thanks
You don't "upload a library to gradle" ;-), you make it accessibla via gradle, so you first have to compile your library and publish the generated artefacts (aar or jar files) to a central repository, like Maven Central.
There are two common Maven repositories at time of this writing:
Maven Central http://search.maven.org/
JCenter https://bintray.com/bintray/jcenter
See this guide as a direction for mavenCentral:
http://www.vandalsoftware.com/post/52468430435/publishing-an-android-library-aar-to-a-maven
Since recent Android Studio versions, JCenter is the preferred default repository, so you may register there and upload your archives to it, like explained here:
https://www.virag.si/2015/01/publishing-gradle-android-library-to-jcenter/
Hope that helps.
The easiest way to expose your library to other developers is by using the JitPack service.
The requirement is that you create a GitHub release and that you have a build file in your repository. It doesn't require that you upload your library.

Categories

Resources