目录
一.生成Module二.生成开源库三.查看开源库四.使用开源库
一.生成Module
依次点击Flie->New->New Mudule创建Module文件
在Module文件里面实现功能代码
在Module文件下面的【build.gradle】文件里面进行设置
plugins {
id 'com.android.library'
id 'maven-publish' //新添加的
}
android {
compileSdk 32
defaultConfig {
minSdk 21
targetSdk 32
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
}
}
//新添加的
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.my.mySdkTest' // 这是需要打包的Module的包名,根据需要更换
artifactId = 'final' //多模块打包时用到,只打包一个Module用不到
version = '1.0.0'
}
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
创建一个新文件【jitpack.yml】跟Module同级
jirpack文件内容,指定jdk版本
将代码上传到Github
二.生成开源库
在Girhub找到提交的代码,点击Releases 进入 填写需要填写的数据 点击【Publish release】按钮,此时已经生成了开源库
三.查看开源库
进入【jitpack】 网址:【https://jitpack.io/】
在输入框中输入项目在【Girhub】的地址【https://github.com/用户名/项目名】
因为提交了5次,所以这里面有5个版本,其中前三个版本失败了,所以对应的【Log】下面的文档图片标志是红色的,后面两个版本成功了,对应的【Log】下面的标志是绿色的。 在未选中版本的时候,下面的提示是这样的
选中版本之后,这里选择的是【1.1.3】版本,点击对应的【Get it】按钮,下面的提示变成了这样
四.使用开源库
开源库使用其实已经在上边图片中显示出来了
在项目对应的build.gradle文件中【非APP对应的build文件】添加maven{ url ‘https://jitpack.io’}但是在Gradle高版本中有问题,在【Gradle】7.0+版本后,项目对应的build.gradle文件是这样的
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
找不到对应的地方
此时需要找这个文件【settings.gradle】,此文件和项目的【build.gradle】文件同级,打开此文件
maven { url "https://jitpack.io" }
然后就可以在APP目录下的【build.gradle】文件中引入生成的开源库使用了。
推荐链接
发表评论