美文网首页
发布自己的lib库到jitpack记录

发布自己的lib库到jitpack记录

作者: 奈蜇 | 来源:发表于2023-03-05 16:44 被阅读0次

1.Android studio 同步项目到GitHub(要会使用自带的git版本控制)

2.配置好lib的build.gradle文件

plugins {
    id 'com.android.library'
    id 'maven-publish'//添加publish插件
}

//设置生产jar Task
task generateSourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier 'sources'
}

//设置全局lib名称    (如implementation 'androidx.appcompat:abc:1.5.1'  中的 abc)
def libName = "TestUtils"

android {
    compileSdk 32

    defaultConfig {
        minSdk 19
        targetSdk 32
        versionCode 200
        versionName "2.0.0"

        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
    }

    //配置aar输出
    android.libraryVariants.all { variant ->
        // aar 输出文件名配置
        variant.outputs.all { output ->
            outputFileName = "${libName}-${android.defaultConfig.versionName}.aar"
        }
    }

    buildFeatures {
        viewBinding true
    }

    publishing {
        singleVariant("release")
    }
}

//配置
afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release         // 表示发布 release
                groupId = 'com.example.test' // 这个是依赖库的组 id
                artifactId = libName       // 依赖库的名称
                version = android.defaultConfig.versionName//当前版本依赖库版本号,这个jitpack不会使用到,只是我们开发者自己查看 
            }
        }
    }
}

dependencies {

    //第三方依赖
    implementation 'androidx.appcompat:appcompat:1.5.1'

}

参考上面的配置即可

3.生成的依赖版本号 == 你在github上创建release时设置的tag

4.lib库中依赖的第三方库请使用api来依赖 而不使用 implementation
使用 implementation 会导致生成的pom文件中的依赖为

<dependency>
<groupId>androidx.appcompat</groupId>
<artifactId>appcompat</artifactId>
<version>1.5.1</version>
<scope>runtime</scope>
</dependency>

而使用api

<dependency>
<groupId>androidx.appcompat</groupId>
<artifactId>appcompat</artifactId>
<version>1.5.1</version>
<scope>compile</scope>
</dependency>

一个是runtime,另一个是compile

相关文章

网友评论

      本文标题:发布自己的lib库到jitpack记录

      本文链接:https://www.haomeiwen.com/subject/kcxpldtx.html