3

How do I create an OBB file for my application using Gradle with the android plugin? As of now, I have to either zip or use the jobb tool to create my OBB file. If there were a means of creating the OBB file entirely through Gradle it would simplify my build process and my life.

MY current process involves creating a Gradle task that kicks of a shell script. Not totally desirable but it works. I am wondering however about zip support in Gradle.

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()
    }
}

Maybe this is the best solution? Possibly. If so, and nobody recommends better, I will add it as an answer.

stevebot
  • 23,275
  • 29
  • 119
  • 181
  • Please note, there are many questions with similar titles that answer the question "how do I integrate an OBB file" but none that tackle how to _create_ a file in the OBB format entirely through Gradle. – stevebot Apr 03 '15 at 01:15

1 Answers1

3

Besides doing it the way you are trying to do it now, I found this:

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")
    }
}

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

Manually(you are doing this in your script):

Example, via command line:

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

Source: http://developer.android.com/tools/help/jobb.html

My Suggestions:

I'd recommend adding more to your task, similar to how this exec method works. This way, you can pass params or generate tasks using your 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
    }
}
Community
  • 1
  • 1
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185