解决 包含第三方aar的module打包 之后无法直接引用第三方
2019-08-17 本文已影响0人
sjj_dot
解决 包含第三方aar的module打包 之后无法直接引用第三方类的问题.
将第三方的aar jar统统放到maven仓库。
推荐方案(jitpack.io 打包)
1、建立一个空项目创建libmodule 放入第三方包。
2、将项目上传到github。
3、按jitpack.io规则直接添加依赖。(在第一次依赖同步的时候打包,需要几分钟时间。也可以先到jitpack打包好再同步)
打包(插件二选一)
如果使用 maven-publish
插件。执行对应模块 publish
任务
如果使用 maven
插件。执行对应模块 uploadArchives
任务
部分示例代码
//获取配置的仓库地址。
String localRepo() {
if (hasProperty("LOCAL_REPO")) {
return LOCAL_REPO
}
if (System.getenv("LOCAL_REPO") != null) {
return new File(System.getenv("LOCAL_REPO")).toURI().toString()
}
return new File(rootDir, "repository").toURI().toString()
}
ext.localRepo = localRepo()
//maven-publish用来打包第三方的jar aar
apply plugin: 'maven-publish'
publishing {
publications {
chardet(MavenPublication) {
groupId = 'com.sjianjun.ext'
artifactId ='chardet'
version '0.0.1'
artifact('./libs/chardet.jar')
}
}
repositories {
maven {
url localRepo
}
}
}
//输出 将lib中的文件部署到本地的//maven-publish 配置模板。
static String getSimpleName(File file) {
return file.name.replaceAll("-","_").split("\\.")[0].toLowerCase()
}
task publishTemplate{
File[] files = new File("./libs").listFiles()
for (File file : files) {
String simpleName = getSimpleName(file)
println(simpleName + "(MavenPublication) {")
println("groupId = 'com.sjianjun.ext'")
println("artifactId ='$simpleName'")
println("version '0.0.1'")
println(" artifact('./libs/${file.name}')")
println("}")
}
for (File file : files) {
String simpleName = getSimpleName(file)
println("api 'com.sjianjun.ext:$simpleName:0.0.1'")
}
}
//用于打包lib。
apply plugin: 'maven'
task sourceJar(type: Jar) {
getArchiveClassifier().set('sources')
from sourceSets.main.java.srcDirs
}
artifacts {
archives kotlinSourcesJar
}
uploadArchives {
repositories.mavenDeployer {
// 配置本地仓库路径,项目根目录下的repository目录中
repository(url: localRepo)
pom.groupId = "com.sjianjun"// 唯一标识(通常为模块包名,也可以任意)
pom.artifactId = project.name.toLowerCase() // 项目名称(通常为类库模块名称,也可以任意)
pom.version = "0.0.1" // 版本号
}
}
//eg:
//apply plugin: 'maven-publish'
//
//publishing {
// publications {
// chardet(MavenPublication) {
// groupId = 'com.sjianjun.ext'
// artifactId ='chardet'
// version '0.0.1'
// artifact('./libs/chardet.jar')
// }
// }
// repositories {
// maven {
// url new File(rootDir, "repository").toURI()
// }
// }
//}
//eg:android lib
//apply plugin: 'maven'
//
//task sourceJar(type: Jar) {
// classifier = 'sources'
// from android.sourceSets.main.java.srcDirs
//}
//artifacts {
// archives sourceJar
//}
//
//uploadArchives {
// repositories.mavenDeployer {
// // 配置本地仓库路径,项目根目录下的repository目录中
// repository(url: new File(rootDir, "repository").toURI())
// pom.groupId = "com.sjianjun"// 唯一标识(通常为模块包名,也可以任意)
// pom.artifactId = project.name.toLowerCase() // 项目名称(通常为类库模块名称,也可以任意)
// pom.version = "0.0.1" // 版本号
// }
//}
//eg:java or kotlin lib
//apply plugin: 'maven'
//
//task sourceJar(type: Jar) {
// getArchiveClassifier().set('sources')
// from sourceSets.main.java.srcDirs
//}
//artifacts {
// archives kotlinSourcesJar
//}
//
//uploadArchives {
// repositories.mavenDeployer {
// // 配置本地仓库路径,项目根目录下的repository目录中
// repository(url: new File(rootDir, "repository").toURI())
// pom.groupId = "com.sjianjun"// 唯一标识(通常为模块包名,也可以任意)
// pom.artifactId = project.name.toLowerCase() // 项目名称(通常为类库模块名称,也可以任意)
// pom.version = "0.0.1" // 版本号
// }
//}
建议将打包的库放到github方便使用。