1 编写构建gradle脚本代码
1.1 配置publication和repository
在指定moudle目录下新建名为"maven-publish.gradle"文件,其声明的publication和repository如下所示:
groovy
apply plugin: 'maven-publish'
// This creates a task called publishReleaseToMyRepoRepository that
// consists of the name of the publication and the name of the repository.
publishing {
publications {
release(MavenPublication) {
groupId = 'yourGroupId'
artifactId = 'yourArtifactId'
version = '1.1.1'
afterEvaluate {
from components.release //software component name is release.
}
}
}
repositories {
maven {
name = 'myrepo' // repository name is myrepo
url = "${project.buildDir}/repo" // the repository is generated inside the build folder of the project, under a repo directory.
}
}
}
上述配置经build后会生成一个 publishReleaseToMyRepoRepository
的task。
在gradle面板中可以搜索到这个task。
1.2 配置输出aar文件的名字
在指定moudle 目录下的gradle文件中配置如下:
groovy
plugins {
id 'com.android.library'
}
apply from: 'maven-publish.gradle'
android {
...
defaultConfig {
...
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
...
libraryVariants.all { variant ->
def path = "xxx_${variant.buildType.name}"+
"_${versionNameStr}.aar"
variant.outputs.forEach {
it.outputFileName = path
}
}
}
并引用上文中的maven-publish.gradle 脚本
apply from: 'maven-push.gradle'
2 编译aar
通过gradle面板按照1.1中的图示找到 task: publishReleaseToMyRepoRepository
双击后,便开始执行该task,然后在module->build->repo目录下输出了aar。