如何使用 Gradle 创建 OBB 文件

How to create an OBB file using Gradle

如何使用 Gradle 和 android 插件为我的应用程序创建 OBB 文件?截至目前,我必须压缩或使用 jobb 工具来创建我的 OBB 文件。如果有一种方法可以完全通过 Gradle 创建 OBB 文件,那将简化我的构建过程和我的生活。

我当前的流程涉及创建一个 Gradle 任务,该任务会启动 shell 脚本。不完全可取,但它有效。不过,我想知道 Gradle 中的 zip 支持。

task buildOBBOSX(type:Exec) {
    workingDir '.'

    //note: according to the docs, the version code used is that of the "first"
    // apk with which the expansion is associated with
    commandLine './buildOBB.sh'

    //store the output instead of printing to the console:
    standardOutput = new ByteArrayOutputStream()

    ext.output = {
        return standardOutput.toString()
    }
}

也许这是最好的解决方案?可能吧。如果是这样,而且没有人推荐更好,我会添加它作为答案。

除了按照您现在尝试的方式进行操作外,我还发现:

Gradle:

all{currentFlavor ->
    task ("create"+ currentFlavor.name.substring(0, 1).toUpperCase() + currentFlavor.name.substring(1)+"Obb", type:Zip){
        archiveName = "main.${defaultConfig.versionCode}.${currentFlavor.applicationId}.obb"
    
        ext.destDir = new File(buildDir, "obb/${currentFlavor.name}")
        ext.destFile = new File(destDir, archiveName)
        duplicatesStrategy  DuplicatesStrategy.EXCLUDE
        doFirst {
                destDir.mkdirs()
        }
        description = "Creates expansion file for APK flavour ${currentFlavor.name}"    
        destinationDir = new File(buildDir, "obb/${currentFlavor.name}");
        entryCompression = ZipEntryCompression.STORED
        from "flavors/${currentFlavor.name}/obb", "obb"
        tasks.createObb.dependsOn("create"+ currentFlavor.name.substring(0, 1).toUpperCase() + currentFlavor.name.substring(1)+"Obb")
    }
}

来源:https://gitlab.labs.nic.cz/labs/tablexia/blob/devel/build.gradle#L154

手动(您在脚本中执行此操作):

示例,通过命令行:

jobb -d /temp/assets/ -o my-app-assets.obb -k secret-key -pn com.my.app.package -pv 11

来源:http://developer.android.com/tools/help/jobb.html

我的建议:

我建议为您的任务添加更多内容,类似于此 exec 方法的工作原理。这样,您可以使用 packageName:

传递参数或生成任务
def createOBB(def assetsFolder, def obbFile, def secretKey, def packageName) {
    def stdout = new ByteArrayOutputStream(), stderr = new ByteArrayOutputStream()
    exec {
        // jobb -d /temp/assets/ -o my-app-assets.obb -k secret-key -pn com.my.app.package -pv 11
        commandLine 'jobb', '-d', assetsFolder, '-o', obbFile, '-k', secretKey, '-pn', packageName, '-pv', '11'
        standardOutput = stdout
        errorOutput = stderr
        ignoreExitValue true // remove this if you want to crash if fail
    }
}