Image by Free-Photos from Pixabay

Importing Android libraries from the GitHub Package Registry

Prasad Pulikal

--

Click here to view the full article on publishing and using an Android library from the GitHub Package Registry

GitHub has recently introduced the GitHub Package Registry, a package management service where developers and organisations can publish packages either publicly for the open source community or privately for use within an organisation.

Until recently the service was available as a limited public beta. Currently it is available free for public repositories and with a pay-as-you-go pricing model for private repositories. More information can be found at the below link:

Using an Android library from GitHub Package Registry within an Android Application

Currently the GitHub Package Registry requires us to Authenticate to download an Android Library (Public or Private) hosted on the GitHub Package Registry. This might change for future releases.

Steps 1 and 2 can be skipped if already done while publishing a library

Step 1 : Generate a Personal Access Token for GitHub

  • Inside you GitHub account:
  • Settings -> Developer Settings -> Personal Access Tokens -> Generate new token
  • Make sure you select the following scopes (“ write:packages”, “ read:packages”) and Generate a token
  • After Generating make sure to copy your new personal access token. You cannot see it again! The only option is to generate a new key.

Step 2: Store your GitHub — Personal Access Token details

  • Create a github.properties file within your root Android project
  • In case of a public repository make sure you add this file to .gitignore for keep the token private
  • Add properties gpr.usr=GITHUB_USERID and gpr.key=PERSONAL_ACCESS_TOKEN
  • Replace GITHUB_USERID with personal / organisation Github User ID and PERSONAL_ACCESS_TOKEN with the token generated in #Step 1

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

Step 3 : Update build.gradle inside the application module

  • Add the following code to build.gradle inside the app module that will be using the library published on GitHub Package Registry
def githubProperties = new Properties() githubProperties.load(new FileInputStream(rootProject.file(“github.properties”)))repositories {
maven {
name = "GitHubPackages"
/* Configure path to the library hosted on GitHub Packages Registry
* Replace UserID with package owner userID and REPOSITORY with the repository name
* e.g. "
https://maven.pkg.github.com/enefce/AndroidLibraryForGitHubPackagesDemo"*/
url = uri("https://maven.pkg.github.com/UserID/REPOSITORY")
credentials {
username = githubProperties['gpr.usr'] ?: System.getenv("GPR_USER")
password = githubProperties['gpr.key'] ?: System.getenv("GPR_API_KEY")
}
}
}
  • inside dependencies of the build.gradle of app module, use the following code
dependencies {
//consume library
implementation ‘com.example:package’ // Replace with the package id
....
}
  • Thats it! Sync your Android gradle project and the library from Github Package Registry should be synced with your Android Project

--

--