Android:如何在 aar 中包含 License.txt 文件?



我想在我的 .aar 文件中包含一个 LICENSE.txt 文件。我该怎么做?

提前谢谢。

您可以通过添加 Gradle 任务来实现此目的,这些任务解压缩您的 aar,将许可证文件复制到其中并重新压缩 aar。

应该在"捆绑发布"任务和"上传存档"任务之间完成

这就是我能做到的:

android {
// (...)
def libraryModuleName = 'your-library-module-name'
def outputAarDir = rootProject.file(libraryModuleName + '/build/outputs/aar')
def outputAarUnzipedDir = rootProject.file(libraryModuleName + '/build/outputs/aar/unziped')
def aarReleaseFile = rootProject.file(libraryModuleName + '/build/outputs/aar/' + libraryModuleName + '-release.aar')
task unzipAar(type: Copy) {
from zipTree(aarReleaseFile)
into outputAarUnzipedDir
}
task addLicenseFileInUnzipedAar(type: Copy, dependsOn: 'unzipAar') {
def fromDir = rootProject.file(libraryModuleName + '/')
from fromDir
into outputAarUnzipedDir
include 'LICENSE.txt'
}
task reZipAar(type: Zip, dependsOn: 'addLicenseFileInUnzipedAar') {
from outputAarUnzipedDir
include '*'
include '*/*'
archiveName libraryModuleName + '-release.aar'
destinationDir(outputAarDir)
}
afterEvaluate {
bundleRelease.finalizedBy(reZipAar)
}
}

然后将这些任务添加到标准生成过程中。

构建后的 gradle 控制台:

(...)
:your-library-module-name:bundleRelease
:your-library-module-name:unzipAar
:your-library-module-name:addLicenseFileInUnzipedAar
:your-library-module-name:reZipAar

最新更新