0%

gradle发布到github packages

最近弄了个Android的模拟自动注入的库,在给别人用的时候,需要发送 jar 或者 aar 给对方,这就导致我可能需要保留一份,否则每次都需要重新编译生成。为了 偷懒
增加效率,在查询资料之后,将其发布到了github packages

准备工作:需要github账号(废话),生成GitHub token需要有写权限(如果只是使用不需要,如果需要推送则需要),将账号(GITHUB_USER)
token(GITHUB_PERSONAL_ACCESS_TOKEN)配置到用户目录下~/.gradle/gradle.properties

注意事项:

如果发布返回4**系列错误,原因可能是这几个:

  1. GroupPageId或者ArtificatId使用了大写,需要改为小写。
  2. 当前GroupPageId或者ArtificatId已经在使用了!!!,需要修改,或者把在使用的删掉。。。。
  3. 账号密码(Token)错误。

新建或者使用已有仓库,新建 Android library 项目(id 'com.android.library')。

修改library项目的build.gradle配置(不要改错了):

1.

1
2
3
plugins {
id 'com.android.library'
}

改为

1
2
3
4
plugins {
id 'com.android.library'
id 'maven-publish'
}
  1. 增加函数定义
1
2
3
4
5
6
7
8
9
10
11
//def githubProperties = new Properties()
//githubProperties.load(new FileInputStream(rootProject.file("github.properties"))) //Set env variable GPR_USER & GPR_API_KEY if not adding a properties file
def getGroupPageId = { ->
return "com.kekxv" // Replace with version Name
}
def getVersionName = { ->
return "0.2.4" // Replace with version Name
}
def getArtificatId = { ->
return "autowired" // Replace with library name ID
}
  1. 增加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "sources"
}
publishing {
publications {
// Creates a Maven publication called "release".
release(MavenPublication) {
groupId getGroupPageId()
artifactId getArtificatId()
version getVersionName()
artifact("$buildDir/outputs/aar/${getArtificatId()}-release.aar")

artifact(sourceJar)
pom.withXml {
final dependenciesNode = asNode().appendNode('dependencies')
ext.addDependency = { Dependency dep, String scope ->
if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
return
final dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dep.group)
dependencyNode.appendNode('artifactId', dep.name)
dependencyNode.appendNode('version', dep.version)
dependencyNode.appendNode('scope', scope)
if (!dep.transitive) {
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
exclusionNode.appendNode('groupId', '*')
exclusionNode.appendNode('artifactId', '*')
} else if (!dep.properties.excludeRules.empty) {
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
dep.properties.excludeRules.each { ExcludeRule rule ->
exclusionNode.appendNode('groupId', rule.group ?: '*')
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}
configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
}
}
}

repositories {
// maven {
// url "$buildDir/repo"
// }
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/用户名所有者/仓库名/")
credentials {
username = System.getenv('GITHUB_USER') ?: project.properties['GITHUB_USER']
password = System.getenv('GITHUB_PERSONAL_ACCESS_TOKEN') ?: project.properties['GITHUB_PERSONAL_ACCESS_TOKEN']
}
}
}
}

publish.dependsOn(build)

配置完成之后,在AndroidStudio右侧标签点击 Gradle ,选择library项目,点开publishing,执行publish,如果没有意外并且账号(GITHUB_USER)
token(GITHUB_PERSONAL_ACCESS_TOKEN)配置正确,你将可以在你的仓库packages
可以看到项目,例如:https://github.com/kekxv/JavaRepo/packages

使用方式

引入方式

以下配置均在项目build.gradle

方式一

  1. 生成 GitHub Token,教程点击本链接
  2. 配置 gradle ,增加仓库https://maven.pkg.github.com/kekxv/JavaRepo/ ,github 的仓库需要授权(公开的也要),自己配置一下 GITHUB_USERGITHUB_PERSONAL_ACCESS_TOKEN,建议在家目录的gradle.properties进行配置。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    //配置gradle
    android {
    repositories {
    maven {
    name = "GitHubPackages"
    url = uri("https://maven.pkg.github.com/kekxv/JavaRepo/")
    credentials {
    username = System.getenv('GITHUB_USER') ?: project.properties['GITHUB_USER']
    password = System.getenv('GITHUB_PERSONAL_ACCESS_TOKEN') ?: project.properties['GITHUB_PERSONAL_ACCESS_TOKEN']
    }
    }
    }
    }
  3. 增加 dependencies implementation 'com.kekxv:autowired:0.2.3' (0.2.3为版本号,可更改为最新版本)

方式二

直接下载前往 仓库 下载autowired-0.2.3.aar导入到项目。
根据情况,可能需要将autowired-0.2.3.aar 拷贝到libs 目录并 implementation fileTree(dir: "libs", include: ["*.jar"]) 更改为implementation fileTree(dir: "libs", include: ["*.jar","*.aar"])

参考文档

  • 配置 Gradle 用于 GitHub 包
  • Android Libraries on GitHub Packages