AndroidStudio之模块库上传与引用

来源:互联网 发布:卖家网是淘宝的吗 编辑:程序博客网 时间:2024/06/06 03:59

主要内容如下

  • 上传到maven仓库
  • 引用maven仓库的
  • 自己搭建maven仓库,及其它几种包管理形式
  • 上传到jcenter
  • 附录

闲话

仓库管理,需要了解一些基本的概念,远程仓库 Gradle项目管理 Gradle和Maven的比较 等等

1,上传到maven仓库

上传到maven仓库,我们以Android的库模块最终以aar形式上传到maven仓库为例,贴代码:
在需要uoload的模块build.gradle做如下更改:

apply plugin: 'maven'uploadArchives {    repositories {        mavenDeployer {            repository(url: "file://localhost/tmp/myRepo/")        }    }}

首行的功能是为了使用maven插件,有点类似于java编程中导入包的意味,只有使用了maven插件,后续才可以使用maven插件提供的方法。

下面

 repository(url: "file://localhost/tmp/myRepo/")    

是定义我们仓库的地址,这里可以是自己搭建的仓库,本地文件系统,公共的maven仓库等等。有些仓库需要认证,需要我们提供密码,类似于下面:

uploadArchives {    repositories {        mavenDeployer {            repository(url:"http://localhost:8081/repository/fish/") {                authentication(userName:'admin',password:'admin123')            }            pom.groupId = 'com.acmes'            pom.version = '2.1'            pom.project {                licenses {                    license {                        name 'The Apache Software License, Version 2.0'                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'                        distribution 'repo'                    }                }            }        }    }}

上面的代码片段可以看到,我们还可以更改自己的groupId等信息,具体参考Gradle的UserGuide
等我们配置完毕之后,同步一下gradle,如果没有错误,在相应模块的AndroidStudio右侧的gradle的task栏目,就会出现一个upload的文件夹,路径类似于

:yourmodulename:upload

这里写图片描述

双击即可

上传之后,在下面目录文件可以看到自己maven的配置信息,在引用的时候要保证一一对应:
这里写图片描述

2,引用maven仓库

引用maven仓库比较简单,需要更改工程根目录的build.gradle和引用模块的build.gradle,见下面截图的代码:
工程根目录build.gradle加入自己的仓库:
这里写图片描述
引用模块的build.gradle:
这里写图片描述

格式如下:

groupId:artifactId:version

3,自己搭建maven仓库

为了更方便观察整个过程,我们可以自己搭建一个maven仓库,开源的仓库很多,我们以
Nexus Repository Manager为例,可以自己去尝试下。

除了在线maven仓库,我们甚至可以以文件系统来管理我们的仓库,我们假设根目录有一个localmaven的本地仓库:
这里写图片描述

上传的时候,如下配置:

uploadArchives {    repositories {        mavenDeployer {            repository(url:uri('../localmaven')) //custom as you wish            pom.groupId = 'com.acmes'            pom.version = '3.0'    }}

这里写图片描述

使用的时候,和线上一致,除了maven仓库地址格式不同:

工程根目录build.gradle:
这里写图片描述

引用模块的build.gradle:
这里写图片描述

4,上传到jcenter

jcenter是AndroidStudido默认的公共maven仓库,这里记录下如何将库以aar的形式上传到jcenter。更具体的描述和介绍,请参考官网 官方说明
具体步骤如下:

1,注册Bintray账户,根据提示说明,正确建立仓库和package2,在AndroidStudio的gradle文件中,引入maven-publish插件,并填写正确配置信息3,在AndroidStudio的gradle文件中,引入gradle-bintray-plugin插件,并填写正确配置信息4,信息正确配置完毕之后,找到uploadBintray任务,执行 ./gradlew uploadBintray ,即可

1,注册Bintray,以及创建 Repository(仓库,有时简写为repo)这里从简介绍。官网注册之后,根据提示创建maven仓库,根据提示一步一步操作即可。

2,maven-publish插件使用见下图:
这里写图片描述

3,Bintray提供给我们的插件使用如下,需要我们手动导入:

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'

待上传的模块,见如下截图:
这里写图片描述

注意publicatitons后面的MyPublication,这里即是利用上面maven-publish插件定义的maven信息来上传我们实际的文件。

4,上面信息配置完毕,同步没有错误的话,会有以下截图信息:
这里写图片描述

打开终端,工程根目录执行 ./gradlew bintrayUpload 即可

如果一切正确,在bintray页面会看到自己的上传文件:
这里写图片描述

这里写图片描述

注意点有二:

1,./gradlew bintrayUpload执行成功之后,在bintray主页会有一个 Discard和Publish确认框,只有Publish之后,文件才会被保存。2,上传到Bintray之后,默认没有和Jcenter同步,需要我们点击如上截图按钮,在相应的页面,填写正确的信息之后,N个工作日后,即可同步到jcenter

点击同步到jcenter如下图:
这里写图片描述

GOOD LUCK, BIG BOY !

5,附录

Maven与Gardle对比
搭建自己的maven仓库
Gradle教程
Bintray官网
Bintray官方说明
简介博文Jcenter上传

apply plugin: 'com.android.library'apply plugin: 'maven'android {    compileSdkVersion 21    buildToolsVersion "26.0.0"    defaultConfig {        minSdkVersion 18        targetSdkVersion 21        versionCode 1        versionName "1.0"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])}// using maven plugin for publish artifactsuploadArchives {    repositories {        mavenDeployer {//            repository(url:uri('../localmaven')) //custom you wish            repository(url: "https://dl.bintray.com/yuxiaolongok/AcmeSAndroid")            pom.groupId = 'com.acmes'            pom.version = '1.0'        }    }}apply plugin: 'maven-publish'//using bintray for publish artifactstask sourceJar(type: Jar) {    from android.sourceSets.main.java.srcDirs    classifier "source"}publishing {    publications {        MyPublication(MavenPublication) {            groupId 'com.acmes'            artifactId 'screenercrdlib'            version '1.3'            artifact(sourceJar)            artifact("$buildDir/outputs/aar/screenrecordlib-release.aar")            // 解决pom文件中,依赖规则无法生成的issue            pom.withXml {                def dependenciesNode = asNode().appendNode('dependencies')                //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each                configurations.compile.allDependencies.each {                    if (it.group != null && it.name != null && it.version != null) {                        def dependencyNode = dependenciesNode.appendNode('dependency')                        dependencyNode.appendNode('groupId', it.group)                        dependencyNode.appendNode('artifactId', it.name)                        dependencyNode.appendNode('version', it.version)                    }                }            }        }    }}apply plugin: 'com.jfrog.bintray'//Bintray插件所需要用到参数配置bintray {    user = 'yxxxxxk'  //用户名    key = '0xxxxxxxxxxxxxxxxxxxxe'    // API key ,在bintray个人资料卡中可以找到    pkg {        repo = 'AcmeSAndroid' //仓库地址        name = 'SimpleAndroid' //package name        publications = ['MyPublication'] // 上面自定义的使用 maven-publish 插件上传的文件        licenses = ['Apache-2.0']        vcsUrl = 'https://github.com/Fishoo0/SimpleAndroid.git' //Version Control 仓库的地址        version {            name = '3.0'    //版本名字            desc = 'First upload from AndroidStudio'            released = new Date()//            vcsTag = '1.3.0'//            attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']        }    }}
原创粉丝点击