My experience in publishing an AAR Artifact
A few months ago, I started to work on an SDK which basically an AAR(Android archive file). This SDK contained business logic in java and kotlin file as well as resources file such as manifest, layout, etc. This SDK will be used by another application who wanted to use our services. So we need to provide a handy way to share this AAR file.
At the initial state, we think about sharing the module itself, which is the source code of the SDK but this will increase the compile time of the project who implemented it and of course, we don’t want to expose our code even though the team who will use the code is in our company. So we move to other options which is provide the aar file, this option obviously better than the first option but it still has flaws, by this it will make a lot of work to manually import the aar file to the project and don’t mention if there is a new version of the SDK, you will need to manually download and delete the previous version and import the new version which is cumbersome for me at least.
I can use jitpack or any other artifact hosted platform which is very easy to implement if the SDK has a public code yet since my project is private then I need to pay for hosting it in jitpack which I don’t want to.
We do have self-hosted jfrog artifactory, which I can use to host the artifact but unfortunately the artifactory only for one dedicated service so still, I can’t host it on there.
Apparently, gitlab has a maven repository package support(if you use the premium version). So without further consideration, I found the right place to host my artifact.
In the beginning, I tried to follow the official documentation but I found no luck on there, the instructions are too general and (for me) no helpful at all. After trial and error, I gave up on following the official docs (official doc can be useless too xD). so I go to google and find that we can use maven-publish gradle plugin to publish an artifact directly from gradle file or simply create a task to do that.
Using maven-publish gradle plugins is really life-saver for me. it really straight forward and very easy to implement.
To publish your artifact you can simply follow these steps:
- Apply maven-publish plugin in your build.gradle file
apply plugin: 'maven-publish'
2. Give your artifact an information
You can simply only provide the necessary information as I will do here, to learn more about the meta-data or information you can go to this link
def libraryGroupId = 'io.rahmatt.artifact'
def libraryArtifactId = 'coollib'
def libraryVersion = '0.0.1'
3. Setting up for the publishing
in the above gist, note that I specify that only release build that will be published to maven. And here also the tricky part, at first I don’t have pom.withXml configuration, the artifact still be published but it gave me an error when I tried to build the project that implements my SDK, it said that some dependencies are missing, it happened because my SDK also has a dependencies to a third-party library which not included in the project. so I need to specify what dependencies needed in order to use my SDK and once the project that implemented the SDK now it will automatically download all the required dependency and here pom.withXml configuration comes to the rescue, basically, it creates a pom.xml file (Project object model) to give a solution that we need which gives information for required dependency in order to use our SDK.
Last, we set up the repositories configuration where we specify the URL and the credential we need in order to publish the artifact/SDK (above is dummy URL). private token here is gitlab access token.
Now we can publish the artifact by simply call the task to publish it like this:
./gradlew assembleRelease publish
but before that I suggest you clean the project first, in case there is some generated file or just simply we want to clean project before publishing it.
by that, I can download my artifact using this:
//build.gradle project level
...
repositories {
....
maven { url "https://rahmatt.io/coollib" }
...
}//build.gradle module level
implementation "io.rahmat.artifact:coollib:0.0.1"
And also one use case if your project has internal or private visibility to download your artifact, you also need to put credential for authentication process like this
//build.gradle project level
...
repositories {
....
maven { url "https://rahmatt.io/coollib"
credentials(HttpHeaderCredentials) {
name = "Private-Token"
value = "${private_token}"
}
authentication {
header(HttpHeaderAuthentication)
}
}
...
}
That is that, the artifact is now live and you can share it!
Thank you for reading this article, I hope my experience and findings can help you to not struggle as I did before. If you find this post is helpful, please give a clap and share it to your circle. Have a nice day!