Using GitHub Packages for Android projects

栏目: IT技术 · 发布时间: 5年前

内容简介:GitHub has released GitHub Packages last year. You can now easily host your private libraries without subscribing to another service.In this post, we’re going to setup GitHub Packages to publish an example Android library.We’ll be following these steps:

GitHub has released GitHub Packages last year. You can now easily host your private libraries without subscribing to another service.

In this post, we’re going to setup GitHub Packages to publish an example Android library.

We’ll be following these steps:

  1. Creating credentials for accessing GitHub’s maven repository
  2. Publishing an Android library to GitHub’s maven repository using a Gradle plugin
  3. Installing an Android library in another project

Setup credentials

The maven repository for GitHub Packages has the following pattern:

https://maven.pkg.github.com/{repository owner}/{repository}

You need to provide valid credentials to publish or install packages from this repository.

repositories {
   maven {
       url = "https://maven.pkg.github.com/githubuser/Library"
       credentials {
           username = GITHUB_USER
           password = GITHUB_TOKEN
       }
   }
}

To get a token, go to your Settings page on your GitHub account. From there, go to Developer Settings -> Personal access tokens and generate a new one with the following permissions:

read:packages
write:packages
repo

Check the official instructions here

Note:These credentials shouldn’t be added to your git repository. Instead, you should define GITHUB_USER and GITHUB_TOKEN in a properties file or pass them directly using the gradlew -P parameter for the publish task.

Publishing packages

With the recent release of Android Studio 3.6, publishing to maven repositories got simpler. You can read more about the maven-publish plugin here . The Android gradle plugin will now generate a proper POM file based on the project’s dependencies.

For publishing maven packages, I use an adapted version of Chris Banes’ goodie gradle-mvn-push which you can find here: publish_github.gradle

This plugin does the following:

  1. Specifies the maven repository and its credentials
  2. Generates the javadocs from java/kotlin files (using dokka if it’s available and properly setup in the project)
  3. Bundles the aar, javadocs and sources to be published

Feel free to modify the plugin for your own needs. The instructions are almost the same as the ones over gradle-mvn-push . For each library of your project, add this to your gradle.properties :

LIBRARY_VERSION=1.0.0
LIBRARY_GROUP=com.library.package
LIBRARY_ARTIFACT=library
// Gradle path for installation will be: com.library.package:library:1.0.0

If you have multiple modules and they all share the same properties, you can place this in the root gradle.properties :

LIBRARY_PUBLISH_SOURCES=true // default is true
LIBRARY_PUBLISH_DOCS=true // default is true

// The user/organization that owns the repository
GITHUB_OWNER=githubuser
// The name of the repository that contains your module
GITHUB_REPOSITORY=Library
// Maven repository url will be: https://maven.pkg.github.com/githubuser/Library

POM_PACKAGING=aar
POM_NAME=Library
POM_DESCRIPTION=A sample library that will be published to github packages
POM_URL=https://github.com/githubuser/Library
POM_SCM_URL=https://github.com/githubuser/Library
POM_SCM_CONNECTION=scm:git@github.com:githubuser/Library.git
POM_SCM_DEV_CONNECTION=scm:git@github.com:githubuser/Library.git
POM_DEVELOPER_ID=githubuser
POM_DEVELOPER_NAME=GitHub User
// Optional license fields. If a license name isn't available,
// the artifact won't contain any license
POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo

If it’s a single module project, everything can be defined in the same file.

Finally, apply the plugin at the bottom of your module’s build.gradle:

// Use this if the script is stored locally in the /gradle folder
apply from: "$rootProject.projectDir/gradle/publish_github.gradle"

// Or use this if the script is stored remotely somewhere:
apply from: "https://gist.github.com/user/publish.gradle"

To publish the library you can now simply run the publish task:

./gradlew publish

If you’re running this task using a CI service, like GitHub Actions, you can pass the properties needed as parameters:

./gradlew publish -PGITHUB_USER=${ {secrets.GITHUB_USER} } -PGITHUB_TOKEN=${ {secrets.GITHUB_TOKEN} }

After the task has finished running, packages can be found at: https://github.com/githubuser/Library/packages

Note: GITHUB_USER is the user that has permissions to read/write to the repository while GITHUB_OWNER is the user/organization that owns the repository.

Installing packages

Now that we’ve published our module to GitHub Packages, it’s time to install it from another project. Update your root build.gradle file with the new repository:

repositories {
   maven {
       url = "https://maven.pkg.github.com/githubuser/Library"
       credentials {
           username = GITHUB_USER
           password = GITHUB_TOKEN
       }
   }
}

And as usual, include the library in the module’s build.gradle:

implementation com.library.package:library:1.0.0

Unfortunately, this must be done even for open-source projects. We can only hope GitHub releases support for a central repository so that the setup becomes something like:

repositories {
   maven {
       url = "https://maven.pkg.github.com/"
   }
}

However, for open-source projects you can still use jcenter or jitpack (they’re both free for open-source).

Installing multiple packages

If you have a group of libraries, adding them becomes cumbersome because there’s no support for a “central” repository like jcenter or mavenCentral. This will eventually be the end result:

repositories {
   maven {
       url = "https://maven.pkg.github.com/user/AndroidLibrary1"
       ...
   }
   maven {
       url = "https://maven.pkg.github.com/user/AndroidLibrary2"
       ...
   }
   maven {
       url = "https://maven.pkg.github.com/user/AndroidLibrary3"
       ...
   }
}

As you can see, this doesn’t look so good. For the moment, you can workaround this by publishing your libraries to another repository in GitHub. This repository’s purpose will be hosting the packages from the other repositories:

repositories {
   maven {
       url = "https://maven.pkg.github.com/user/AndroidLibraries"
       ...
   }
}

Note:At the moment, GitHub doesn’t allow duplicate package names for the same owner. If AndroidLibrary1 is published to /user/AndroidLibrary1 , it can’t be published to /user/AndroidLibraries and vice-versa.

Conclusion

GitHub Packages are really helpful and play together nicely with GitHub’s ecosystem. You can easily create private android modules and make them available privately or publicly.

However, I found a few drawbacks:

  1. Boilerplate setup for open-source packages. Importing a library requires adding its maven repository, which means N libraries will require N entries in the build.gradle file. For open-source projects I still recommend jcenter or jitpack.
  2. Limited support for publishing to different repositories. A library that’s present in a repository can’t be added to another one if it has the same artifact id.

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

这才是马云

这才是马云

陈伟 / 浙江人民出版社 / 2011-5 / 30.00元

“幽默马云”、“开心马云”、“顽皮马云”、“狂妄马云”等。《这才是马云》从各个角度揭开了千面马云的真面目,告诉你一个与想象中大不一样的马云。这不只是一本书,更像一部喜剧电影,让你通过声音、色彩、表情等诸多要素走近马云,感受阿里巴巴。没有冗长的说教,只有让人忍俊不禁的细节;没有高深的理论,只有通俗、诚恳的陈述。作者借幽默平常的琐事,记录下马云“可爱”的一面,看完后让人恍然大悟:原来,马云是这样一个人......一起来看看 《这才是马云》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具