【问题标题】:Android/Gradle: Add an asset after the compile phaseAndroid/Gradle:在编译阶段后添加资产
【发布时间】:2015-08-20 02:17:05
【问题描述】:

我的任务是根据 Android Gradle 构建中的已编译类生成元数据文件。我可以通过在编译任务之后执行它来运行它:

android.applicationVariants.all { variant ->
    def variantName = variant.name.capitalize()
    def compileSourcesTaskName = "compile${variantName}Sources"
    def compileSourcesTask = project.tasks.findByName(compileSourcesTaskName)
    compileSourcesTask.finalizedBy "myTaskThatGeneratesAssets"
}

很遗憾,此时 Android 已经处理了这些资产。新文件不会包含在组装的 APK 中。

similar question 的答案建议在对齐/签名之前调用 aapt add 将文件添加到 APK。这似乎可行,但该帖子并未涉及实现细节。在the Android Gradle plugin 中调用aapt 的代码对于构建脚本来说看起来相当复杂,我不确定如何访问它引用的@​​987654326@。

不胜感激有关如何实施此功能或任何其他解决方案的建议!

【问题讨论】:

    标签: android gradle


    【解决方案1】:

    好的,这就是我的最终结果。它做了两个假设,可能会在更高版本的 android gradle 插件(我使用的是 1.3.0)上中断:

    1. 平台工具的路径是${android.getSdkDirectory().getAbsolutePath()}/build-tools/${android.buildToolsVersion}/
    2. 中间(资源)APK 的路径是${buildDir}/intermediates/res/resources-${variant.baseName}.ap_

    只要这些是真的,这应该会在资源 APK 已经构建后生成一个使用 aapt 添加新资产文件的任务:

    def overlayDir = ... // path to a resources overlay directory that contains "assets/my.json"
    def addMyAssetTaskName = "add${variantName}MyAsset"
    task "${addMyAssetTaskName}" (type: Exec) {
        dependsOn myTaskThatGeneratesAssets
        workingDir overlayDir
        def aaptCommand = "${android.getSdkDirectory().getAbsolutePath()}/build-tools/${android.buildToolsVersion}/aapt"
        def apkPath = "${buildDir}/intermediates/res/resources-${variant.baseName}.ap_"
        commandLine aaptCommand, 'add', apkPath, "assets/my.json"
    }
    

    然后我在上面的问题中使用finalizedBy addMyAssetTaskName

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 2016-10-26
      相关资源
      最近更新 更多