Facebook Buck is a build system modelled on Blaze, Google’s internal build system, which was developed by former Google and Facebook engineers and is open sourced on Github.

Buck currently only supports Mac OS X and Linux,

Buck Environment Configuration

There are two ways to download Buck:

Homebrew way

Before installing Buck using Homebrew in OS X, ensure that XCode and the Command Line Tool are installed and updated to the latest version. Then run the following Command in Terminal:

$ brew update
$ brew tap facebook/fb
$ brew install --HEAD buck
Copy the code

If this approach does not work, it is recommended to try manual construction.

Manual build

Manual builds are compiled and installed from Buck’s source code. First, make sure your OS X meets the following requirements:

  • Oracle JDK 7
  • Apache Ant 1.8 (Newer)
  • Python 2.6 or 2.7
  • Git
  • C compiler: GCC or CLang

With the above environment in place, you can check Buck’s source code from Github and install it. Run the following command in Terminal:

$ git clone https://github.com/facebook/buck.git
$ cd buck
$ ant
$ ./bin/buck --help
Copy the code

The source code of Buck is relatively large, and the compressed package is close to 200M, so git Clone may wait a long time if the network is not good.

validation

buck --help
Copy the code

If all is well, you should get the following log message in Terminal:

buck build tool
usage:
  buck [options]
  buck command --help
  buck command [command-options]
available commands:
  audit       lists the inputs for the specified target
  build       builds the specified target
  clean       deletes any generated files
  fetch       downloads remote resources to your local machine
  install     builds and installs an application
  project     generates project configuration files for an IDE
  query       provides facilities to query information about the target nodes graph
  quickstart  generates a default project directory
  server      query and control the http server
  targets     prints the list of buildable targets
  test        builds and runs the tests for the specified target
  uninstall   uninstalls an APK
options:
 --help         : Shows this screen and exits.
 --version (-V) : Show version number.
Copy the code

The installationWatchman

Facebook is an open source file monitoring service that monitors files and records changes to files. When files change, it can trigger actions such as executing commands. Watchman was installed to keep Buck from parsing build files every time, and to cache other things and reduce compile time. Watchman installation is simple, with the following script:

brew install watchman
Copy the code

use

Currently, you can choose two ways to use:

Native use of Facebook

Quickly create an Android project built on Buck

You can quickly create an Android project using the touch. Buckconfig && buck quickstart command. This command will require you to complete the following two parameters:

  • --dest-dir: Directory of generated Android projects
  • --android-sdk: Directory of the Android SDK on your computer

The log information in Terminal is as follows:

[email protected]  /Users/CJS $ touch .buckconfig && buck quickstart
Buck does not appear to have been built -- building Buck!
All done, continuing with build.
Using watchman.
Using buckd.
Enter the directory where you would like to create the project:  /Users/CJS/Desktop/BuckDemo
Enter your Android SDK's location: /Users/CJS/Documents/Android/android-sdk/
Thanks for installing Buck!

In this quickstart project, the file apps/myapp/BUCK defines the build rules.

At this point, you should move into the project directory and try running:

    buck build //apps/myapp:app

or:

    buck build app

See .buckconfig for a full list of aliases.

If you have an Android device connected to your computer, you can also try:

    buck install app

This information is located in the file README.md if you need to access it
later.
[email protected]  /Users/CJS $
Copy the code

The generated Android project directory structure is as follows:

As you can see, there is a BUCK configuration file under each directory. Let’s preview the contents of the BUCK file under MyApp

android_binary(
  name = 'app',
  manifest = 'AndroidManifest.xml',
  keystore = ':debug_keystore',
  deps = [
    '//java/com/example/activity:activity',
  ],
)

keystore(
  name = 'debug_keystore',
  store = 'debug.keystore',
  properties = 'debug.keystore.properties',
)

project_config(
  src_target = ':app',
)
Copy the code

Go to the project root directory and enter the following command in Terminal to create the IntelliJ project:

$ buck project --ide IntelliJ
Copy the code

The log is as follows, indicating that the IntelliJ project is successfully created:

Using buckd. Waiting for Watchman command [/usr/local/bin/watchman watch /Users/CJS/Desktop/BuckDemo/.]... Timed out after 10000 ms waiting for Watchman command [/usr/local/bin/watchman watch /Users/CJS/Desktop/BuckDemo/.]. Disabling Watchman. [-] PROCESSING BUCK FILES... FINISHED 0.3s [+] GENERATING PROJECT... 0.4s Modified 8 IntelliJ Project Files. : Please resynchronize IntelliJ via File->Synchronize or Cmd-Opt-Y (Mac) or Ctrl-Alt-Y (PC/Linux) === Did you know === * You can run `buck project ` to generate a minimal project just for that target. * This will make your IDE faster when working on large projects. * See buck project --help for more info. --=* Knowing is half the battle!Copy the code

Third party buck + Gradle usage

Combined with the “Okbuck Gradle Plugin” (which generates scripts that sometimes need to be manually adjusted), it is very convenient for Android Gradle projects. At the same time, the Buck plugin can be used for idea (install the plugin directly in Android Studio, there are still some problems, need to manually adjust).

OkBuck

The basic configuration

Add configuration to build.gradle in the root directory of the project:

Buildscript {repositories {jCenter ()} dependencies {classpath 'com.uber:okbuck:0.9.4'}} 'com.uber.okbuck'Copy the code

For the most part, this completes the configuration. OkBuck is hosted in JCenter, so jCenter () must be added to the list of BuildScript and AllProjects repositories, and must come before the Apply Plugin section.

After the OkBuck plugin is applied, two Gradle Tasks, OkBuck and buckWrapper, will be generated in the project

  • okbuckThe BUCK configuration file will be generated
  • buckWrapperBuck Wrapper is similar to gradle Wrapper in that OkBuck can be used to serve us more intelligently

We highly recommend using buckWrapper:./gradlew buckWrapper.

You can run the./buckw targets command to view all the targets that can be built. The generated.buckconfig.local file will specify multiple aliases, such as appDevDebug, appProdRelease, Another -appDebug, etc., from which BUCK build commands can be determined, such as./buckw build appDevDebug, etc.

Custom Configuration

Okbuck {buildToolVersion "24.0.2" target "Android-24" linearAllocHardLimit = [app: buildToolVersion "24.0.2" target "Android-24" linearAllocHardLimit = [app: 16 * 1024 * 1024 ] primaryDexPatterns = [ app: [ '^com/uber/okbuck/example/AppShell^', '^com/uber/okbuck/example/BuildConfig^', '^android/support/multidex/', '^com/facebook/buck/android/support/exopackage/', '^com/github/promeg/xlog_android/lib/XLogConfig^', '^com/squareup/leakcanary/LeakCanary^', ] ] exopackage = [ appDebug: true ] appLibDependencies = [ 'appProd': [ 'buck-android-support', 'com.android.support:multidex', 'libraries/javalibrary:main', 'libraries/common:paidRelease', ], 'appDev': [ 'buck-android-support', 'com.android.support:multidex', 'libraries/javalibrary:main', 'libraries/common:freeDebug', ] ] annotationProcessors = [ "local-apt-dependency": ['com.okbuck.apt.ExampleProcessor'] ] buckProjects = project.subprojects extraBuckOpts = [ 'appDebug', [ "binary": ["trim_resource_ids = True"] ] ] wrapper { repo = 'https://github.com/facebook/buck.git' remove = ['.buckconfig.local', "**/BUCK"] keep = [".okbuck/**/BUCK"] } }Copy the code

Explain in detail

  • buildToolVersionSpecify the Android SDK build-tools version. Default24.0.2
  • targetSpecifies the Android Target SDK version that can be run/tools/android list targets --compactGet, default isandroid-24
  • linearAllocHardLimitandprimaryDexPatternsBoth maps are used to configure the linearAllocHardLimit and primaryDexPatterns sections of BUCK multidex. For more detailed instructions on multidex configuration, seemultidex wiki, if multidex is not used (not inbuild.gradleOpen in file), you can ignore these two parameters
  • exopackageandappLibDependenciesBoth maps are used to configure BUCK Exopackage. See exopackage configuration for more detailsexopackage wikiIf exopackage is not used, you can ignore these three parameters
  • annotationProcessorsUsed to declare the annotation handler in the project. Key is the Module path and value is the full name of the annotation handler class.
  • buckProjectsUsed to control which modules will be built using BUCK. The default is all modules in the project
  • You can set the map key according to the following rules:
  • If you specify the module name, you can set all flavors and build types, for example:app
  • If you specify the module name and flavor name, you can set all build types of the specified flavor, e.g. ‘appDemo’.
  • If you specify the module name and build type name, you can set all the flavors of the specified build type, e.g. ‘appDebug’.
  • Specify the module name, flavor name, and build type name, e.g. ‘appDemoRelease’

Problem # #

  1. The code file compiled by Buck contains Chinese characters, causing garbled characters. IDE is set to UTF-8, but Chinese loaded through strings.xml should not be a problem.
  2. Gradle’s latest plugin fixes the switch case problem, but Buck does not compile.
  3. Use the buck plugin for idea.buckconfig or .buckconfig.localAdd ~~~ [httpServer] port = 0 ~~~ to enable buck in ide
  4. If the script generated by the Buck for Gradle plugin cannot be found when running buck target, please modify it manually.