Buck: apk_genrule()


An apk_genrule() rule is used to post-process an APK. What separates an apk_genrule from a genrule is apk_genrules are known by BUCK to produce APKs, so commands like buck install or buck uninstall still work. Additionally, apk_genrule() rules can be inputs to other apk_genrule() rules.


  • name (required) #

    The name of the rule.

  • apk (required) #

    The input android_binary() rule. The path to the APK can be accessed with the $APK shell variable.

  • srcs (defaults to []) #

    A list of files to be used in the shell command.

  • cmd (defaults to None) #

    The shell command to run to generate the output file. It is the fallback of bash and cmd_exe. It will be subject to the following environment variable substitutions:

    A string expansion of the srcs argument delimited by the environment_expansion_separator argument where each element of srcs will be translated into an absolute path.
    The absolute path to a directory to which sources are copied to prior to running the command.
    The output file for the genrule(). The file specified by this variable must always be written by this command. If not, the execution of this rule will be considered a failure, halting the build process.
    It is also possible to expand references to other rules within the cmd, using builtin string parameter macros. This expansion takes two supported forms:
    $(classpath //path/to:target)
    Expands a build rule for something that has a java classpath to the transitive classpath of that rule. If the rule does not have (or contribute to) a classpath, an exception will be thrown and the build will break.
    $(exe //path/to:target)
    Expands a build rule that results in an executable to the commands necessary to execute that command. For example, a java_binary() may expand to a call to java -jar path/to/target.jar. Files that are executable (perhaps generated by a genrule()) will also be expanded. If the build rule does not generate an executable output an exception will be thrown and the build will break.
    $(location //path/to:target)
    Expands to the location of the output of the build rule. This means that you can refer to these without needing to be aware of how Buck is storing data on the disk mid-build.
    $(maven_coords //path/to:target)
    Expands to the Maven coordinates for the build rule. This allows you to access the Maven coordinates for Maven-aware build rules. The format of the expansion is: <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>
    Note that all build rules expanded in the command will be automatically considered to be dependencies of the genrule().

  • bash (defaults to None) #

    The platform-specific version of parameter cmd. It runs on UNIX in which bash is installed and has a higher priority than cmd. Command specified will be run with /bin/bash -c.

  • cmd_exe (defaults to None) #

    The platform-specific version of parameter cmd. It runs on Windows and has a higher priority than cmd. Command specified will be run with cmd.exe /c.

  • type (defaults to None) #

    The type of this genrule. This is used for logging and is particularly useful for grouping different genrules that share an underlying logical "type".

  • out (defaults to None) #

    This argument only exists for historical reasons and it does not have any effect. It will be deprecated and removed in the future.

  • environment_expansion_separator (defaults to " ") #

    The delimiter between paths in environment variables that are populated by buck in genrules, like SRCS, that can contain multiple paths. This can be useful if paths contain spaces.

  • tests (defaults to []) #

    List of build targets that identify the test rules that exercise this target.

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule in its deps.

  • licenses (defaults to []) #

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query.

  • labels (defaults to []) #

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query attrfilter.


Here is an example of a couple apk_genrule() open up an APK, do some super signing, and then zipalign that APK again.
# Building this rule will produce a file named messenger.apk.
  name = 'messenger',
  manifest = 'AndroidManifest.xml',
  target = 'Google Inc.:Google APIs:16',
  keystore = '//keystores:prod',
  package_type = 'release',
  proguard_config = 'proguard.cfg',
  deps = [

  name = 'messenger_super_sign_unalign',
  apk = ':messenger',
  bash = '$(exe //java/com/facebook/sign:super_sign) --input $APK --output $OUT',
  cmd_exe = '$(exe //java/com/facebook/sign:super_sign) --input %APK% --output %OUT%',
  out = 'messenger_super_sign_unalign.apk',

  name = 'messenger_super_sign',
  apk = ':messenger_super_sign_unalign',
  bash = '$ANDROID_HOME/tools/zipalign -f 4 $APK $OUT',
  cmd_exe = '%ANDROID_HOME%\\tools\\zipalign -f 4 %APK% %OUT%',
  out = 'messenger_super_sign.apk',