上传Android library到JitPack

1. 前言

当然首先需要科学上网,可以简单的使用Edge的扩展插件SetupVPN
在这里插入图片描述
然后需要在github上创建一个项目,并且在这个仓库中添加一个release,如下图:
在这里插入图片描述
这里我上传到release的为我这个插件的aar包,至于怎么得到aar包,可以参考博客:Android文件下载——多线程断点下载mylibrary导出aar,并使用。然后需要进入jitpack网站,用github账号登录即可,比如:
在这里插入图片描述
进入后就可以看到怎么使用的教程了。比如这里还是以baiyazi/FileDownloader这个项目为例,首先选中这个你的项目,这里我选中FileDownloader

在这里插入图片描述
然后点击Get it来选择发布版本,就可以得到下面的两个配置:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

和:

dependencies {
	implementation 'com.github.baiyazi:FileDownloader:v1.0.0'
}

然后可以按照使用案例:https://github.com/baiyazi/FileDownloader操作测试即可。

但是很遗憾,失败了!可以清晰的看到Log这里变红了:
在这里插入图片描述

点击进去可以看到对应的日志信息:

ERROR:
No build file found. Looking for Gradle/Maven/Sbt/Lein build file in the root of the project
See the documentation at https://jitpack.io/docs/
Files: 
README.md
base
bean
config
controller
enums
listener
services
utils

所以这里将重新新建一个项目,而不是单独的上传源代码。当然这里需要查看下说明文档:Publishing on JitPack

值得注意的是,官方给了一个案例:android-example。幸运的是在这个项目的README文件中有一个Tutorial

1.1 创建模块

创建library模块filedownloader
在这里插入图片描述

1.2 添加android-maven插件

project/build.gradle文件中添加android-maven插件:

dependencies {
    classpath "com.android.tools.build:gradle:4.1.3"
    // JitPack 插件
    classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

1.3 设置自定义库的gradle

filedownloader/build.gradle 文件中添加maven插件和group,这里直接仿造这个android-example中的library/build.gradle 配置

plugins {
    id 'com.android.library'
    id 'maven-publish'
}

repositories {
    mavenCentral()
    google()
    jcenter()
    maven { url "https://jitpack.io" }
}

group = 'com.gitub.baiyazi'
version = '1.0'


android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                from components.release
                groupId = 'com.github.baiyazi'
                artifactId = 'FileDownloader'
                version = '1.0.0'
            }
        }
    }
}

1.4 上传项目到github

这里我直接是使用的是github desktop十分方便。

1.5 创建一个release版本

因为之前已经创建过了,这里不再创建。

1.6 继续打开JitPack

这里因为之前的有问题,所以重新新建了一个项目,然后对应的创建了一个插件,然后使用Build->Rebuild Project得到aar包。然后上传这个版本到新项目的release。之后打开JitPack,找到现在的这个项目baiyazi/Downloader,然后点击Get it,后等待Log更新完毕:
在这里插入图片描述
点击日志图标,看看是否报错:
在这里插入图片描述

此时对应的filedownlaod/build.gradle文件为:

plugins {
    id 'com.android.library'
    id 'maven-publish'
}

group = 'com.gitub.baiyazi'
version = '1.0'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                from components.release
                groupId = 'com.github.baiyazi'
                artifactId = 'Downloader'
                version = '1.0.0'
            }
        }
    }
}

1.7 使用:

配置:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

和:

dependencies {
	implementation 'com.github.baiyazi:Downloader:v1.0.0'
}

2. 快捷方式

直接找到我的项目baiyazi/Downloader,然后查看gradle的配置即可。


References

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦否

文章对你有用?不妨打赏一毛两毛

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值