Publishing to GitHub Package Registry

1. Generate your GitHub personal access token

Generate your personal access token by following the Creating a personal access token GitHub documentation.

Make sure the following scopes (" write:packages", " read:packages") are selected.

2. Create a github.properties file 

Add this file within your root Android project and add the generated username and key.

gpr.usr=github-user-name gpr.key=generatedkeygoeshere

Add this file to your .gitignore file to keep the access token private.

 

Alternatively, you can also add the GPR_USER and GPR_API_KEY values to your environment variables on your local machine or build server to avoid creating a GitHub properties file.

3. Update the build.gradle in the library module

 

Add the maven-publish plugin

apply plugin: 'maven-publish'

Add the Maven publication and repositories configs in a publishing {}block.

def githubProperties = new Properties() githubProperties.load(new FileInputStream(rootProject.file("github.properties"))) publishing { publications { SamplePublication(MavenPublication) { artifact("$buildDir/outputs/aar/opensrp-library.aar") // Replace the correct artifact aar path artifact(sourceJar) groupId this.group artifactId 'opensrp-client-lib' // Replace the library id version this.version //The publication doesn't know about our dependencies, so we have to manually add them to the pom pom.withXml { def dependenciesNode = asNode().appendNode('dependencies') //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each configurations.implementation.allDependencies.each { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) dependencyNode.appendNode('version', it.version) } } } } repositories { maven { name = "GitHubPackages" /** Configure path of your package repository on Github * Replace GITHUB_USERID with your or organisation Github userID and REPOSITORY with the repository name on GitHub * e.g. "https://maven.pkg.github.com/opensrp/opensrp-client-reporting" */ url = uri("https://maven.pkg.github.com/GITHUB_USERID/REPOSITORY") credentials { /** Create github.properties in root project folder file with * gpr.usr=GITHUB_USER_ID & gpr.key=PERSONAL_ACCESS_TOKEN * Or set env variable GPR_USER & GPR_API_KEY if not adding a properties file */ username = githubProperties['gpr.usr'] ?: System.getenv("GPR_USER") password = githubProperties['gpr.key'] ?: System.getenv("GPR_API_KEY") } } } }

This can be defined in a separate file e.g. 'githubpackages.gradle' then included as follows

4. Publish the library to the GitHub Package Registry

 


Using a published library from GitHub packages 

1. Generate your GitHub personal access token

You’ll need the GitHub personal access token. If you don’t have one, generate as mentioned above.

If you’ve already generated one you can skip this step. A new one won’t be required.

2. Update the build.gradle inside the application module

Include the following code in the build.gradle file for the module that will be making use of the published library

 

3. Add the dependency

Add the dependency for the specific library, for example