在使用较新版本gradle(笔者版本为7.0.2)的android library项目中,想要成功通过jitpack编译发布会遇到一系列问题,部分问题在这篇文章
中已有了正确的处理方法,但还是遇到其中未记载到的问题。其中比较令人困惑的问题是这个
https://jitpack.io/com/github/EnthuDai/SimpleChart-Kotlin/1.0.5/build.log
可以看到里面
BUILD SUCCESSFUL in 2m 30s
134 actionable tasks: 131 executed, 3 up-to-date
No issues found.
Build tool exit code: 0
Looking for artifacts...
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -Dhttps.protocols=TLSv1.2
Looking for pom.xml in build directory and ~/.m2
2022-01-12T08:49:49.48117059Z
Exit code: 0
ERROR: No build artifacts found
成功build但是
ERROR: No build artifacts found
对于这个问题,目前网上较多(但未必正确)的解决方案是在library的build.gradle中添加
publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'org.gradle.sample'
artifactId = 'library'
version = '1.1'
from components.java
}
}
}
或是类似的代码段
该方法的出处在
https://sami.eljabali.org/how-to-publish-a-kotlin-library-to-jitpack/
但该方法在我的项目gradle的sysc阶段,就会报错,无法正常编译
经过多番研究(科学上网搜索),最终找到真正的解决方法:
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'maven-publish'
}
...
android {
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
}
}
}
}
...
该方法出自jitpack项目的issues中
https://github.com/jitpack/jitpack.io/issues/3814
至此终于成功在jitpack上成功编译并发布!
但当引用此编译的项目源时,仍然遇到问题:在jitpack官网提供的依赖引用方式:
To get a Git project into your build:
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
Step 2. Add the dependency
dependencies { implementation 'com.github.EnthuDai:SimpleChart-Kotlin:Tag' }
发现在新版的gradle中,需要将
maven { url 'https://jitpack.io' }
写在 settings.gradle 中才能正确引用,即:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
maven { url 'https://jitpack.io' }
}
}
至此,终于成功完成一个 kotlin android library的发布和使用。
完整的示例大家可参考我的开源项目:
https://github.com/EnthuDai/SimpleChart-Kotlin.git