对接的第三方SDK分联调版(测试环境)跟正式版(生产环境),调试时用联调版,上线用正式版。两个环境对应不同的代码、资源文件,考虑到复用问题,因此新建一个library集成这个SDK,并上传到公共库,之后在项目中直接依赖即可。
2、需求及实现 2.1、需求因为分两个版本,且版本的代码、资源各不相同,因此这个library应该要实现以下需求
一个library module包含多个渠道flavor 可以单独上传flavor,并且互不影响 2.2、一个任务上传一个渠道正常情况下使用maven 插件是这样:
apply plugin: 'com.android.library'
apply plugin: 'maven'
然后用uploadArchives配置pom相关信息,和上传路径等:
uploadArchives {
repositories.mavenDeployer {
pom.groupId = GROUP
pom.artifactId = ARTIFACT_ID
pom.version = VERSION_NAME
repository(url: REPOSITORY_URL) {
authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
}
}
}
最后执行uploadArchives任务即可上传成功。
但是这样显然不符合需求,多个渠道的情况下会有多个aar,且pom.artifactId也是不一样的,需要针对性的配置。
2.3、一个任务同时上传多个渠道查了好几天资料,都没发现什么有用的信息,大多都是针对单个flavor的,终于在StackOverflow上找到了一些有用信息,照葫芦画瓢,最后gradle如下:
import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact //导入类
...
android {
publishNonDefault true
productFlavors {
//调试环境、测试环境
dev {
}
//生产环境、正式环境
production {
}
}
...
}
//配置要上传的文件信息
artifacts {
archives new DefaultPublishArtifact("production", "aar", "aar", null, new Date(), new File("build/outputs/aar", "lib-production-release.aar"))
archives new DefaultPublishArtifact("dev", "aar", "aar", null, new Date(), new File("build/outputs/aar", "lib-dev-release.aar"))
}
//上传任务配置
uploadArchives {
repositories.mavenDeployer {
...
//添加过滤器
//正式环境
addFilter('pro') { artifact, file ->
artifact.name.contains("production") //production就是artifacts里指定的name
}
//测试环境
addFilter('dev') { artifact, file ->
artifact.name.contains("dev")
}
//单独设置pom文件
pom('pro').groupId = GROUP
pom('pro').artifactId = PRODUCTION_MAVEN.ARTIFACT_ID
pom('pro').version = PRODUCTION_MAVEN.VERSION_NAME
pom('dev').groupId = GROUP
pom('dev').artifactId = DEV_MAVEN.ARTIFACT_ID
pom('dev').version = DEV_MAVEN.VERSION_NAME
}
}
这样执行uploadArchives之后,就会上传多个渠道的aar了,这里算完成了一半。
2.4、自定义上传任务要实现单个渠道上传,只靠uploadArchives这一个任务是不行的,因此要自定义上传任务,实现一样的功能。本质上uploadArchives也是Upload类型的Task,即我们可以照着uploadArchives自定义Task继承Upload:
//上传调试版aar
task uploadDev(type: Upload, group: "upload") {
repositories.mavenDeployer {
repository(url: uri('../repos')) {
}
addFilter('dev') { artifact, file ->
println("artifact " + artifact.name)
artifact.name.contains("dev")
}
pom('dev').groupId = MIGU_GROUP
pom('dev').artifactId = DEV_MAVEN.ARTIFACT_ID
pom('dev').version = DEV_MAVEN.VERSION_NAME
}
}
同步之后就会在upload目录下多了一个任务:
执行之后,就会发现这个错误:
No value has been specified for property ‘artifacts’.
这时看uploadArchives的源码是最好的,但是我不知道去哪里看,所以又耗费了我一些时间查资料。最后实在没辙,终于直面这个错误:既然没指定,那就显示的设置好了。
2.4.1 artifacts和configuration配置上传信息的时候用到了artifacts,但是不知道为什么要这样写,看了下源码,artifacts和configurations都是位于project下:
//Configures the published artifacts for this project.
* configurations {
* //declaring new configuration that will be used to associate with artifacts
* schema
* }
* task schemaJar(type: Jar) {
* //some imaginary task that creates a jar artifact with the schema
* }
* artifacts {
* //configuration name and the task:
* schema schemaJar
* }
void artifacts(Closure configureClosure);
根据注释的说明,应该有个默认的configuration,是这样写的:
configurations {
archives
}
所以我们才能使用archives来配置要上传的文件,而看了Upload的源码后发现,artifacts并没有直接设置的方法,而是通过Configuration获取的:
/**
* Returns the artifacts which will be uploaded.
*
* @return the artifacts.
*/
@InputFiles
public FileCollection getArtifacts() {
Configuration configuration = getConfiguration();
return configuration == null ? null : configuration.getAllArtifacts().getFiles();
}
也就说是要设置artifacts必须要先设置Configuration。通过打印configurations和上面的分析,archives就是我们的目标。所以手动把archives这个配置加进来:
//上传调试版aar
task uploadDev(type: Upload, group: "upload") {
//设置configuration
setConfiguration(project.configurations.getByName("archives"))
repositories.mavenDeployer {
...
}
到这里已经可以了。
2.4.2 自定义artifacts和configuration同样地,可以自定义configuration来过滤配置文件,自定义上传Task设置为该configuration即可
configurations {
dev
pro
}
//配置要上传的文件信息
artifacts {
pro new DefaultPublishArtifact("production", "aar", "aar", null, new Date(), new File("build/outputs/aar", "lib-production-release.aar"))
dev new DefaultPublishArtifact("dev", "aar", "aar", null, new Date(), new File("build/outputs/aar", "lib-dev-release.aar"))
}
//上传调试版aar
task uploadDev(type: Upload, group: "upload") {
//设置configuration
setConfiguration(project.configurations.getByName("dev"))
repositories.mavenDeployer {
...
}
2.4.3 最终版本
configurations {
devArchives
proArchives
}
//配置要上传的文件信息
artifacts {
proArchives new DefaultPublishArtifact("production", "aar", "aar", null, new Date(), new File("build/outputs/aar", "lib-production-release.aar"))
devArchives new DefaultPublishArtifact("dev", "aar", "aar", null, new Date(), new File("build/outputs/aar", "lib-dev-release.aar"))
}
//上传调试版aar
task uploadDev(type: Upload, group: "upload") {
setConfiguration(project.configurations.getByName("devArchives"))
repositories.mavenDeployer {
pom.groupId = MIGU_GROUP
pom.artifactId = DEV_MAVEN.ARTIFACT_ID
pom.version = DEV_MAVEN.VERSION_NAME
repository(url: uri('../repos')) {
}
}
}
//上传正式版aar
task uploadPro(type: Upload, group: "upload") {
setConfiguration(project.configurations.getByName("proArchives"))
repositories.mavenDeployer {
pom.groupId = MIGU_GROUP
pom.artifactId = PRODUCTION_MAVEN.ARTIFACT_ID
pom.version = PRODUCTION_MAVEN.VERSION_NAME
repository(url: uri('../repos')) {
}
}
}
生成的两个任务分别用于上传联调版和正式版。