0

Use BUCK in Android projects to speed up your build-time.

Life is short to wait for the extra time to build our tech-systems, we need to speed up!

In this trend, the giant tech companies are relying on their own customized build system. For example: Buck (Facebook), Bazel (Google) & Pants (Twitter) where the syntax of the 03 build systems are subset or related to Python. This article is mainly focused to build Android applications by Buck. Basically, I am going to write why you should use Buck to build your Android code instead of Gradle, Ant or Maven.

So, let's move on it!

What is Buck?

Buck is a declarative build system developed & used by Facebook. According to their documentation it encourages the creation of small, reusable modules consisting of code and resources, and supports a variety of languages on many platforms. It's a cross-platform build tool to compile the large scaled system at Facebook. It's now an open-source project specialized for compiling the Android & iOS projects.

There is an example of the basic syntax of Buck as following:

java_binary(
    name = "buck-android-support",
    deps = [
        "//android/com/facebook/buck/android/support/exopackage:exopackage",
    ],
)

Benefits of Buck

Since, there are already many articles are written to advertise the benefits of Buck, so I am highlighting only the key-points here:

  • Buck is FAST. It reduces incremental build times by keeping track of unchanged modules so that the minimal set of modules is rebuilt, (mentioned here: https://buckbuild.com).
  • Produces same build result everywhere by the pre-defined inputs.
  • Buck works well with the version control system i.e. Git.
  • Buck sripts are easy to understand.
  • It's a reliable build system because of the production usage at Facebook, Uber and Dropbox. It's not going away!

Buck was initially used to build the Facebook, Facebook Messenger & Instagram application. After being an open source project, it's now continuously developing. But still Buck doesn't support Data-binding & Kotlin in the Android projects. Hopefully, it will be resolved in future.

Installation in Android Projects

For the Android projects, I am mentioning 02 ways to get it in your application as following:

Install Manually

There is a document written in the offcial site: https://buckbuild.com/setup/getting_started.html to get it manually. Let me to summarize it:

(1) Download & install the following components:

(2) Set environment variables:

You will need to tell Buck about the source of Android SDK and/or NDK. Buck will look at the value of the ANDROID_HOME & ANDROID_NDK environment variable to locate the files of SDK & NDK respectively on your system. There is a guideline on how to set it in several operating systems: https://spring.io/guides/gs/android

(3) Install Buck:

Now, you can clone the Buck project from here: https://github.com/facebook/buck. It's recommended to clone/copy it in the root directory of the Android project to use the build rule easily. You can run the following commands:

git clone https://github.com/facebook/buck.git
cd buck
ant
./bin/buck --help

(4) Build sample projects using Buck:

Once the Buck is installed successfully, you may try to build the sample projects as following:

cd buck
git clone [email protected]:fbsamples/bucksamples.git
cd bucksamples/cross-platform-scale-2015-demo/

After that, you can find the following files to use Buck:

  • android/BUCK: This is a build file including the necessary build rules as the following example:
android_binary(
  name = 'app',
  manifest = 'AndroidManifest.xml',
  target = 'Google Inc.:Google APIs:22',
  keystore = ':debug_keystore',
  deps = [
    '//java/com/buck/example:activity',
    '//res/com/buck/example:res',
  ],
)

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

project_config(
  src_target = ':app',
)
  • .buckconfig: This is a configuration file is used for several flag and alias settings for the project. For example, you can set a simple alias for your Andorid project as below:
[alias]
    app = //apps/example:app

After setup the Buck files, you can use buck build command as the following example:

buck build demo_app_android
[-] PROCESSING BUCK FILES...FINISHED 0.0s [100%]
[-] DOWNLOADING... (0.00 B/S AVG, TOTAL: 0.00 B, 0 Artifacts)
[-] BUILDING...FINISHED 0.7s [100%] (1/1 JOBS, 0 UPDATED, 0 [0.0%] CACHE MISS)

For the first-time, it will take a longer time, but once you have built, then it will keep the track of unchanged modules & gives the faster build result from the second time. The Buck outputs will be found in the /buck-out/ folder.

To run the built project, you need to use buck install --run command as the following example:

buck install --run demo_app_android
Installing apk on emulator-5554 (android-emulator).
[-] PROCESSING BUCK FILES...FINISHED 0.1s [100%]
[-] DOWNLOADING... (0.00 B/S AVG, TOTAL: 0.00 B, 0 Artifacts)
[-] BUILDING...FINISHED 0.8s [100%] (1/1 JOBS, 0 UPDATED, 0 [0.0%] CACHE MISS)
[+] INSTALLING...0.9s
Successfully ran install apk //android:demo-app on 1 device(s)
Starting activity com.facebook.buck.demo/.App...
Successfully ran start activity on 1 device(s)

Warning: Well...! If you want to install it manually using the above commands you will stuck on several problems & it will waste your time. You can follow a boilerplate to ease your work but still you may encounter several problems depending on your system. But, it will be appreciated if you gain the basic knowledge on the Buck build procedure.

Install OkBuck (Alternative way)

OkBuck is a gradle plugin to install Buck build system automatically on a Gradle project. It's used & developed by Uber for installing the Buck system easily. The installation process in the Android projects is given below:

In the build.gradle file of the project's root directory:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.uber:okbuck:0.20.9'
    }
}

repositories {
    jcenter()
}

apply plugin: 'com.uber.okbuck'

Also, you must need to ensure the signing configuration in the application's gradle as it is part of the Buck build system:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        ...
        targetSdkVersion 25
        ...
    }
    signingConfigs {
        config {
            keyAlias '...'
            keyPassword '...'
            storeFile file('...')
            storePassword '...'
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.config
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

After synced the gradle file, you need to run the buck-wrapper using gradlew as following:

./gradlew :buckWrapper

Then you can run the following commands in your terminal to achieve the faster build-time!

# List all buck targets
./buckw targets

# Build a target (example given below)
./buckw build //app:bin_debug

# Install an apk target  (example given below)
./buckw install --run appDebug

You can experience the build time by changing/not-changing your source code to log the speed!

References


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí