使用Gradle7.0发布SDK到JitPack遇到的问题

488 阅读2分钟

前言

最近在学习《Android第一行代码》练习到最后一个项目的时候,需要发布SDK到Jcenter。但是众所周知其在今年5月份的时候就已经停止服务了。本着简单的原则我将其项目发布到了JitPack上,本以为很简单但是其中的坑也不少,特此记录下来。

发布过程

第一步

首先在Github上创建一个仓库

image.png

第二步

image.png 赋值仓库链接在你创建项目的跟目录打开git bash

image.png 然后将初始化好的文件夹中的所有文件剪切出来到你的项目中如.git文件夹等

第三步

这里假如你的项目已经开发好了,其实就是《Android第一行代码》中最后一个项目。将项目推送到github上 在项目的跟目录打开git bash,输入如下命令:

  1. git add . (将文件全部添加git)
  2. git commit -m "项目完成提交" (本地提交)
  3. git push origin main (推送到github上的main分支上)
  4. git tag v1.0.0 (添加tag版本,发布jitpack要用)
  5. git push origin main v1.0.0(tag版本推送到github上)

第四步

点击tag

image.png

image.png

image.png

image.png

第五步

赋值github你项目的链接,打开jitpack网站,贴入你的链接

image.png 这里就会看到你tag的所有版本,点击get it就会加载项目。

image.png 如果显示绿色说明发布成功就可以在项目中使用了

image.png

问题

问题一

FAILURE: Build failed with an exception.

* Where:
Build file '/home/jitpack/build/app/build.gradle' line: 2

* What went wrong:
An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
     You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

解决办法:在项目的跟目录下创建文件jitpack.yml,里面加入如下代码可以解决。

jdk:
  - openjdk11

问题二

FAILURE: Build failed with an exception.

* Where:
Script '/script/maven-plugin.gradle' line: 2

* What went wrong:
A problem occurred evaluating script.
> Failed to apply plugin 'com.github.dcendents.android-maven'.
   > Could not create plugin of type 'AndroidMavenPlugin'.
      > Could not generate a decorated class for type AndroidMavenPlugin.
         > org/gradle/api/publication/maven/internal/MavenPomMetaInfoProvider

解决办法:在你的library的build.gradle中加入如下代码 image.png

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'maven-publish'  //固定写法
}
afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release

                // You can then customize attributes of the publication as shown below.
                groupId = 'com.qmc.library'  // 你库的包名
                artifactId = 'library'       // 你的库名
                version = '1.0.6'            //版本号
            }
        }
    }
}

修复完以后继续执行发布新的tag,发布到jitpack上面

结语

此次发布到jitpack主要遇到上述两个问题,打完收工