美文网首页AndroidAndroidStudio系列移动测试开发
基于Facebook Buck改造Android构建系统之初体验

基于Facebook Buck改造Android构建系统之初体验

作者: asce1885 | 来源:发表于2015-09-28 16:57 被阅读10304次

    @author ASCE1885的 Github 简书 微博 CSDN
    本文由于潜在的商业目的,不开放全文转载许可,谢谢!

    自从Android开发切换到Android Studio之后,就一直使用Gradle进行项目的构建,随着工程Module的增加,代码的一处改动,都要花费几分钟的时间重新编译,实在是浪费时间,一两个月前就想着使用Facebook的Buck来替换Gradle,换取更快的编译速度,后来由于其他事情就耽搁了。当时在知乎上提问:《国内有Android技术团队在使用facebook的buck进行代码构建吗?》,后面得知微信已经切换到了Buck,最近参与到一个新项目中,也在对原来的代码进行分模块重构,工程的构建时间依然是一个严峻的问题,因此是时候重新开始使用Buck进行改造了。

    Facebook Buck 是个构建系统,以Google的内部构建系统“blaze”为模型,它是由前Google,现Facebook工程师开发并在Github上面开源的,关于Gradle和Buck的构建速度的一个对比如下:

    Buck当前只支持 Mac OS X 和 Linux,Windows 筒靴请掩泪飘过~~
    本文以Mac OS X平台为例进行介绍。

    Buck环境配置

    有两种方式可以下载Buck:

    Homebrew方式

    OS X系统使用Homebrew方式安装Buck之前,需要首先确保安装了 XCode 和 Command Line Tool ,并更新到最新版本,接着在Terminal中执行如下命令即可:

    $ brew update
    $ brew tap facebook/fb
    $ brew install --HEAD buck
    

    如果因为种种原因,这种方式走不通的话,建议尝试手动构建方式。

    手动构建方式

    手动构建就是从Buck源码进行编译安装,首先需要确保你的 OS X 满足以下条件:

    在具备以上环境之后,就可以从Github上面检出Buck的源码然后进行编译安装了,在Terminal中执行如下命令:

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

    其中Buck的源码比较大,压缩包接近200M,所以网络不佳的话git clone可能会等待很长时间,也很容易出错,你也可以选择到我的网盘地址去下载,当然前提是你不怕引入BuckGhost😏。

    如果一切正常的话,你将会在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.
    

    以后会频繁使用到buck安装目录下面的bin/buck命令,所以为了方便在Terminal中直接执行buck命令,需要将该目录添加到环境变量中,cd到用户主目录,打开.bash_profile文件:

    $ cd ~
    $ vim ~/.bash_profile
    

    添加如下配置:

    export PATH=$HOME/buck/bin:$PATH
    

    接着执行如下命令使该配置立即生效:

    $ source ~/.bash_profile
    

    安装 Watchman

    Facebook 开源的一个文件监控服务,用来监视文件并且记录文件的改动情况,当文件变更它可以触发一些操作,例如执行一些命令等等。安装watchman,是为了避免Buck每次都去解析 build files,同时可以缓存其他一些东西,减少编译时间。Watchman安装很简单,脚本如下:

    $ brew install watchman
    

    安装 Android SDK 和 Android NDK

    相信作为Android开发者,电脑上应该都已经有了这两个函数库,需要注意的一点是,使用Buck编译Android代码,需要在用户主目录的.bash_profile文件中配置这两个函数库的环境变量如下:

    export ANDROID_HOME=/Users/guhaoxin/Library/Android/sdk/
    export ANDROID_NDK=/Users/guhaoxin/Library/Android/android-ndk-r10/
    

    如果你的系统存在多个版本的NDK,那么也可以配置ANDROID_NDK_REPOSITORY变量指向包含多个NDK版本的目录。

    快速创建基于 Buck 构建的 Android 工程

    使用touch .buckconfig && buck quickstart命令可以快速创建一个Android工程,该命令执行过程中会要求你补全如下两个参数的值:

    • --dest-dir:生成的Android工程的目录
    • --android-sdk:电脑上Android SDK的目录

    Terminal 中执行的日志信息如下:

    guhaoxindeMacBook-Pro:~ guhaoxin$ 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/guhaoxin/Desktop/BuckDemo
    Enter your Android SDK's location: /Users/guhaoxin/Library/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.
    guhaoxindeMacBook-Pro:~ guhaoxin$ 
    

    生成的Android工程目录结构如下:

    可以看到,每个目录下面都有一个BUCK配置文件,我们先预览下myapp下面的BUCK文件内容,目前有个初步印象就好:

    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',
    )
    

    进入到工程根目录,在Terminal中输入如下命令创建IntelliJ工程:

    $ buck project --ide IntelliJ
    

    日志记录如下,表明IntelliJ工程创建成功:

    Using buckd.
    Waiting for Watchman command [/usr/local/bin/watchman watch /Users/guhaoxin/Desktop/BuckDemo/.]...
    Timed out after 10000 ms waiting for Watchman command [/usr/local/bin/watchman watch /Users/guhaoxin/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 <target>` 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!
    

    参考资料:
    Facebook Buck和xctool:针对Android和iOS的开源构建工具
    利用Buck进行高效Android编译
    Gerrit为何会选择Buck

    欢迎关注我的微信公众号

    相关文章

      网友评论

      • lumenghz:LZ,我看你的教程第一次接触buck,但是quick start新建出来的工程并不符合android studio的工程规范,也就是很多包都需要我们手动去移动和新建的意思吗?
        lumenghz:@asce1885 好的,谢谢
        asce1885:@鲁猛没昵称 工程结构保持标准的不变,只需在module根目录增加一个BUCK文件进行配置即可
      • lumenghz: lumeng@retina ⮀ ~/Documents/android workspace/bucktest ⮀ buck project --ide IntelliJ
        Using watchman.
        Warning raised by BUCK file parser: Traceback (most recent call last):
        Warning raised by BUCK file parser: File "/Users/lumeng/Documents/android workspace/bucktest/.buckd/tmp/buck_run.CMV4KW/buck4628461920040960205.py", line 18, in <module>
        Warning raised by BUCK file parser: from pathlib import Path, PureWindowsPath, PurePath
        Warning raised by BUCK file parser: ImportError: No module named pathlib
        [+] PROCESSING BUCK FILES...0.4s
        BUILD FAILED: Parse error for build file /Users/lumeng/Documents/android workspace/bucktest/apps/myapp/BUCK: Parser exited unexpectedly

        lz,我在执行创建IntelliJ工程的时候提示这个错误,是为什么,求解
      • 3925e0b0b726:大神好为毛我编译老师出这个啊,求教[error][command:3cf79603-65b8-4306-80ba-9012ee4cc5bf][tid:14][com.facebook.buck.json.ProjectBuildFileParser] Process java.lang.UNIXProcess@60f6d0b5 exited with error code 1
        BUILD FAILED: Parse error for build file /Users/xulong/Documents/myworkspace/MyBuck/BUCK: End of input at line 1 column 1

        prebuilt_jar(
        name = 'android-support-v4',
        binary_jar = './libs/android-support-v4.jar',
        visibility = [ 'PUBLIC' ],
        )

        android_resource(
        name = 'res',
        res = './res',
        assets = './assets',
        package = 'com.myBuck',
        visibility = [ 'PUBLIC' ],
        )

        android_library(
        name = 'src',
        srcs = glob(['./src/**/**/*.java']),
        deps = [
        ':build_config',
        ':res',
        ':android-support-v4',
        ],
        )

        android_binary(
        name = 'debug',
        package_type = 'DEBUG',
        manifest = './AndroidManifest.xml',
        target = 'android-14’,
        deps = [
        ':res',
        ':src',
        ],
        )

        android_build_config(
        name = 'build_config',
        package = 'com.myBuck',
        )
        这是我的BUCK文件,求赐教
        asce1885:@xulong 看这个log是在解析buck文件时出错,你可以尝试逐步精简buck文件内容来定位到出问题的代码行
        3925e0b0b726:@asce1885 没有,你有办法吗
        asce1885:@xulong 解决了吗?
      • 轻微:我比较好奇的是使用Buck 这个编译的话,那之前的 gradle 引入的第三库怎么在 buck 管理
        asce1885:@轻微 第三方库需要下载下来,放到lib中
      • 3Zero:卧槽!一把辛酸泪呀!!!花了我几个网上去学习groovy
        asce1885:@3Zero groovy还是很有用的 :smile:
      • yjxandroid:为啥我创建不了 IntelliJ 项目?
        yjxandroid:@asce1885 不是错误 是我自己看错了 哈哈 :blush:
        yjxandroid:@asce1885 o IntelliJ project files modified.
        :: Please resynchronize IntelliJ via File->Synchronize or Cmd-Opt-Y (Mac) or Ctrl-Alt-Y (PC/Linux)

        就是这个错误了
        asce1885:@yjxandroid 提示什么错误?
      • 8cc64b155343:gradle还没用熟练又来一个buck....好在我没有mac
        asce1885:@米代 :sweat_smile:

      本文标题:基于Facebook Buck改造Android构建系统之初体验

      本文链接:https://www.haomeiwen.com/subject/xrvwcttx.html