烹茶细论

Facebook BUCK编译APK包

ANT是google编译apk包的工具,但是发现编译效率不是那么高。编译是开启四进程的。
发现Facebook的BUCK开源工具,就尝试这使用,结果发现效果很不错

参考文档

  • http://facebook.github.io/buck/setup/quick_start.html

  • http://buck.readthedocs.org/en/latest/index.html
  • 安装很简单,按照上面的文档

    git clone git@github.com:facebook/buck.git
    cd buck
    ant
    设置环境变量
    

    我的其中一个BUCK文件

    #打包Android资源文件,一般生成R.java文件
    android_resource(
      name = 'res',
      res = 'res',
      assets = 'assets',
      deps = [
         '//depend_project_1/workspace:ITelephony',  #依赖工程
       ],
      package = 'com.XX.android.XX',   #资源包名称
    )
    
    #输出目标是一个包含了以及编译的class文件和资源文件的jar包
    android_library(
      name = 'mainsrc',
      srcs = glob(['src/**/*.java']),
      visibility = [ 'PUBLIC' ],
      deps = [
        ':res',
        ':support',
        ':zxing-core',
        ':aidl',
        ':IPackageStatsObserver',
        ':IInAppBillingService',
        ':AnyShare',
        ':universal',
        ':GetJarSDK',
        ':FlurryAgent',
        '//depend_project_1/workspace:google',
        '//depend_project_1/workspace:ITelephony',
    
      ]
    )
    
    #生成一个Android APK
    android_binary(
       name = 'main',
       manifest = 'AndroidManifest.xml',
       target = 'Google Inc.:Google APIs:19',
       keystore = ':debug_keystore',
       package_type = 'release',
       proguard_config = 'proguard.cfg',  #混淆
       deps = [
         ':mainsrc',
       ],
    )
    
    #apk包签名
    keystore(
      name = 'debug_keystore',
      store = 'debug.keystore',
      properties = 'debug.keystore.properties',
    )
    
    #aidl文件编译成java
    gen_aidl(
        name = 'aidl',
        aidl = 'src/android/content/pm/IPackageDataObserver.aidl',
        import_path = 'main_project/workspace/src',
        visibility = [ 'PUBLIC' ],
    )
    
    gen_aidl(
        name = 'PackageStats',
        aidl = 'src/android/content/pm/PackageStats.aidl',
        import_path = 'main_project/workspace/src/android/content/pm/',
        visibility = [ 'PUBLIC' ],
     )
    
    gen_aidl(
        name = 'IPackageStatsObserver',
        aidl = 'src/android/content/pm/IPackageStatsObserver.aidl',
        import_path = 'main_project/workspace/src/',
        visibility = [ 'PUBLIC' ],
    )
    
    gen_aidl(
       name = 'IInAppBillingService',
       aidl = 'src/com/android/vending/billing/IInAppBillingService.aidl',
       import_path = 'main_project/workspace/src/',
       visibility = [ 'PUBLIC' ],
    )
    
    #用来指向一个第三方的jar文件(比如junit.jar)并且用作java_library()规则的依赖
    
    prebuilt_jar(
      name = 'support',
      binary_jar = 'libs/android-support-v4.jar',
    )
    prebuilt_jar(
      name = 'libammsdk',
      binary_jar = 'libs/libammsdk.jar',
    )
    
    prebuilt_jar(
       name = 'weibo',
       binary_jar = 'libs/weibo.sdk.android.sso.jar',
    )
    
    prebuilt_jar(
       name = 'AnyShare',
       binary_jar = 'libs/AnyShare-FullSDK.jar',
    )
    
    prebuilt_jar(
       name = 'universal',
       binary_jar = 'libs/universal-image-loader-1.8.4-with-sources.jar',
    )   
    
    prebuilt_jar(
       name = 'GetJarSDK',
       binary_jar = 'libs/GetJarSDK-3.4.4.20130503.06.jar',
    ) 
    
    prebuilt_jar(
       name = 'FlurryAgent',
       binary_jar = 'libs/FlurryAgent.jar',
    )
    prebuilt_jar(
       name = 'zxing-core',
       binary_jar = 'libs/zxing-core.jar',
    )
    
    #生成定义路径,生成apk名字是main.apk
    project_config(
       src_target = ':main',
    )
    

    错误:

    error: constant expression required
    case R.styleable.AdvancedImageView_radius:

    这个是因为代码里面使用switch case
    将他改成if else形式
    原因:http://tools.android.com/tips/non-constant-fields
    其他错误解决没有记录。。

    速度比较

      ant 构建(四进程): 215 个渠道包,用时 3 小时 02 分,因为是四个进程,每个包构建的时间平均是 200 秒
    
    
    buck 构建(四进程): 相同代码,相同渠道,用时 50 分,平均一个包用时 56 秒