GitHub released GitHub Packages in November 2019. Now you can publish different kinds of packages or libraries i.e. maven, npm, ruby gems, docker, etc without subscribing to third party services.
In this article, we will see how to publish the Android library or any maven package i.e. JAR, AAR, etc. to GitHub Packages.
You can see the example project here.
You will learn everything about it in these simple steps:
- Setup credentials
- Publish Android library
- Use the library in another project
Let's jump into the details:
Setup credentials
To publish any package to GitHub, you need GitHub username and GitHub token. Username is not a problem which you already have. Now you need to create GitHub token.
To create a new token, open menu by clicking on your profile picture at the top right of the GitHub account. Then go to,
Developer settings ➡ Personal access tokens ➡ Generate new token
.
Here, make sure you checked the following permissions:
read:packages
write:packages
repo
This token cannot be seen again in GitHub. So copy the token and save it somewhere in your system or a secure place. If you didn't save copy it, you can generate a new one.
Publish Android library
When credentials are ready, it's time to add some code on your module-level gradle file.
Open module or library's build.gradle
file and apply following plugin:
apply plugin: 'maven-publish'
Now it's time to define some methods which will return the information related to the library. Here are those methods:
// Place the version of your library here
def getVersionName = { ->
return "0.0.1"
}
// Add the name of your library here
def getArtifactId = { ->
return "analytics-sdk"
}
// Add the group ID of your library here
def getGroupId = { ->
return "com.otb"
}
// Prepare URL of maven package.
// Replace 'mohitrajput987' with your github repo's username or organization name.
// Replace 'analytics-sdk-android' with the name of github repo
def getGitHubUrl = { ->
return "https://maven.pkg.github.com/mohitrajput987/analytics-sdk-android"
}
Complete build.gradle
file of library module will look like this:
apply plugin: 'java-library'
apply plugin: 'kotlin'
apply plugin: 'maven-publish'
def getVersionName = { ->
return "0.0.1"
}
def getArtifactId = { ->
return "analytics-sdk"
}
def getGroupId = { ->
return "com.otb"
}
def getGitHubUrl = { ->
return "https://maven.pkg.github.com/mohitrajput987/analytics-sdk-android"
}
publishing {
publications {
bar(MavenPublication) {
groupId getGroupId()
artifactId getArtifactId()
version getVersionName()
// Place the path of your artifact here
artifact("$buildDir/libs/${getArtifactId()}.jar")
}
}
repositories {
maven {
name = "GitHubPackages"
url = uri(getGitHubUrl())
credentials {
username = System.getenv("GITHUB_USER_NAME")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
I have stored GitHub's username and password in an environment variable and getting those as follows:
username = System.getenv("GITHUB_USER_NAME")
password = System.getenv("GITHUB_TOKEN")
You can access them from local.properties
file also.
Now sync gradle and you will see new gradle task publish
.
It's time to create a new artifact and upload it to the GitHub package.
To create a new build, run following command:
./gradlew analytics-sdk:assemble
It will create the JAR file. To publish this to GitHub packages, run following command:
./gradlew analytics-sdk:publish
Replace analytics-sdk
with your module project name.
After running these two commands, our package is published successfully. You can see that package in GitHub Packages.
You can also see the complete code of this project in this public repository:
Analytics GitHub Repository
Use the library in another project
We have published our library successfully to GitHub packages. To use this library in another project, add the following gradle dependency to app-level gradle file:
implementation "com.otb:analytics-sdk:0.0.1"
Whether it's a public or private repository, the user has to add package url and credentials in root level gradle file:
repositories {
maven {
url = "https://maven.pkg.github.com/mohitrajput987/analytics-sdk"
credentials {
username = GITHUB_USER
password = GITHUB_TOKEN
}
}
}
The user's github token should have repo
and read package
permissions.
I know adding these credentials is painful for the open-source projects but it's mandatory for now. We can only hope that GitHub host these libraries in a central repository.
Yes, that's it!!!
Conclusion
Our library is published in GitHub packages and you can use it in your project. Do check this library at GitHub which will make your life easy if you are integrating analytics i.e. Mixpanel, Google Analytics to your project.
GitHub packages are really useful if you are already using GitHub ecosystem. You can easily publish Android and Java libraries here with few lines of setup.
Top comments (3)
aused by: groovy.lang.MissingMethodException: No signature of method: build_2lizke14oq34rifyuvp3onsgw.publishing() is applicable for argument types: (build_2lizke14oq34rifyuvp3onsgw$_run_closure6) values: [build_2lizke14oq34rifyuvp3onsgw$_run_closure6@1c67714c]
at build_2lizke14oq34rifyuvp3onsgw.run(/home/gunjan/git_workspace/otasdk-android-plugin/OtasdkPlugin/build.gradle:25)
i am getting this exception while using above code to publish my package on github
how to remove that exception
how to publish custom gradle plugin to github
For any query, you can use this discussion section.