Android重命名库项目的输出

Jef*_*eff 3 android android-gradle-plugin

我以为我的google-fu会让我失望,但我不知道如何将版本号添加到我的图书馆项目的输出中。

我正在使用Android Studio(gradle)构建库,并将其包含在其他项目中。我希望能够向文件中添加一个版本,以跟踪给定项目使用的库的版本,因此我希望版本号在生成的.aar中。

我不知道这一点。有指针吗?

Jef*_*eff 5

重命名com.android.library模块的输出文件与com.android.application模块的输出仅稍有不同。

在com.android.application gradle插件中,您可以放置

android.applicationVariants.all { variant ->
    def file = variant.outputFile
    variant.outputFile = 
        new File(file.parent, 
                 file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
}
Run Code Online (Sandbox Code Playgroud)

但是在com.android.library gradle插件中,您可以使用:

android.libraryVariants.all { variant ->
    def file = variant.outputFile
    variant.outputFile = 
        new File(file.parent, 
                 file.name.replace(".aar", "-" + defaultConfig.versionName + ".aar"))
}
Run Code Online (Sandbox Code Playgroud)

如果您只想对特定的变体执行此操作,则可以这样:

if(variant.name == android.buildTypes.release.name) {
}
Run Code Online (Sandbox Code Playgroud)


pat*_*ckf 5

Android Gradle 插件 v2.+

较新 (2.+) Android Gradle 插件版本没有属性variant.outputFile。这对我有用:

android.libraryVariants.all { variant ->
    variant.outputs.each { output ->
        output.outputFile = new File(
                output.outputFile.parent,
                output.outputFile.name.replace((".aar"), "-${version}.aar"))
    }
}
Run Code Online (Sandbox Code Playgroud)

有关 v2.3 dsl 的完整说明,请参阅文档

Android Gradle 插件 v3.+

版本 3 插件不再支持outputFile。这是因为在配置阶段不再创建特定于变体的任务。这导致插件无法预先知道其所有输出,但这也意味着更快的配置时间。请注意,您需要使用all而不是each因为该对象在新模型的配置时不存在。

android.libraryVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.aar"
    }
}
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅 v3 迁移指南。

  • 我正在使用 gradle 插件 3.0.0-alpha4,这不再对我有用:`错误:(21, 0) 无法获取 com.android.build.gradle.internal 类型的对象的未知属性“versionName”。 api.LibraryVariantImpl.` 请注意,这确实适用于我使用 `applicationVariants` 的应用程序。 (5认同)