前面介绍了Gradle和BUCK两个编译工具的基本概念,本篇文章再来说说日常使用情况。本文不涉及配置方面的使用,毕竟大多时候配置工作是不重复的任务,具体操作可参见官方文档。这里主要讲讲日常工作会用到的使用两个工具编译,安装等操作的不同体验。
指令(Command)
指令操作是程序自动话的先决条件,两者都是基于指令来执行编译任务。其主指令分别为./gradlew和buck,然后调用子指令来执行相应的操作。
对于支持的子指令,Gradle可以通过./gradlew tasks
来罗列出来,并且这些指令直接来自于对应项目中的Gradle配置文件信息
[codesyntax lang=”text” lines=”normal”]
$ ./gradlew tasks Incremental java compilation is an incubating feature. :tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Android tasks ------------- androidDependencies - Displays the Android dependencies of the project. signingReport - Displays the signing info for each variant. sourceSets - Prints out all the source sets defined in this project. Build tasks ----------- assemble - Assembles the outputs of this project. assembleAndroidTest - Assembles all the Test applications. assembleDebug - Assembles all Debug builds. assembleRelease - Assembles all Release builds. build - Assembles and tests this project. buildDependents - Assembles and tests this project and all projects that depend on it. buildNeeded - Assembles and tests this project and all projects it depends on. classes - Assembles main classes. clean - Deletes the build directory. cleanBuildCache - Deletes the build cache directory. compileDebugAndroidTestSources compileDebugSources compileDebugUnitTestSources compileReleaseSources compileReleaseUnitTestSources jar - Assembles a jar archive containing the main classes. mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests. testClasses - Assembles test classes. Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Documentation tasks ------------------- javadoc - Generates Javadoc API documentation for the main source code. Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'Trial'. components - Displays the components produced by root project 'Trial'. [incubating] dependencies - Displays all dependencies declared in root project 'Trial'. dependencyInsight - Displays the insight into a specific dependency in root project 'Trial'. dependentComponents - Displays the dependent components of components in root project 'Trial'. [incubating] help - Displays a help message. model - Displays the configuration model of root project 'Trial'. [incubating] projects - Displays the sub-projects of root project 'Trial'. properties - Displays the properties of root project 'Trial'. tasks - Displays the tasks runnable from root project 'Trial' (some of the displayed tasks may belong to subprojects). Install tasks ------------- installDebug - Installs the Debug build. installDebugAndroidTest - Installs the android (on device) tests for the Debug build. uninstallAll - Uninstall all applications. uninstallDebug - Uninstalls the Debug build. uninstallDebugAndroidTest - Uninstalls the android (on device) tests for the Debug build. uninstallRelease - Uninstalls the Release build. Verification tasks ------------------ check - Runs all checks. connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices. connectedCheck - Runs all device checks on currently connected devices. connectedDebugAndroidTest - Installs and runs the tests for debug on connected devices. deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers. deviceCheck - Runs all device checks using Device Providers and Test Servers. lint - Runs lint on all variants. lintDebug - Runs lint on the Debug build. lintRelease - Runs lint on the Release build. test - Runs the unit tests. testDebugUnitTest - Run unit tests for the debug build. testReleaseUnitTest - Run unit tests for the release build. To see all tasks and more detail, run gradlew tasks --all To see more detail about a task, run gradlew help --task <task> BUILD SUCCESSFUL Total time: 0.956 secs
[/codesyntax]
BUCK的子指令可以通过buck --help
来打印出来,但是这些指令是通用的而非与项目直接联系。要用这些指令,还要提供BUCK build target。
[codesyntax lang=”text” lines=”normal”]
buck --help 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 cache makes calls to the artifact cache clean deletes any generated files distbuild attaches to a distributed build (experimental) doctor debug and fix issues of Buck commands fetch downloads remote resources to your local machine help shows this screen (or the help page of the specified command) and exits. install builds and installs an application machoutils provides some utils for Mach O binary files project generates project configuration files for an IDE publish builds and publishes a library to a central repository query provides facilities to query information about the target nodes graph rage create a defect report root prints the absolute path to the root of the current buck project run runs a target as a command server query and control the http server suggest suggests a refactoring for the specified build target targets prints the list of buildable targets test builds and runs the tests for the specified target uninstall uninstalls an APK verify-caches Verify contents of internal Buck in-memory caches. Options: --help (-h) : Shows this screen and exits. --version (-V) : Show version number.
[/codesyntax]
两者都支持一次执行多个指令。当然很多时候,我更喜欢在终端里用bash的&&
来连接多个指令。对于常用的指令,我也会定义别名来减少键盘敲击,比如
[codesyntax lang=”bash” lines=”normal”]
alias gr=./gradlew alias bk=buck alias bkb='bk build'
[/codesyntax]
Gradle的指令有一个我非常喜欢的特性,就是接受缩写形式子指令:选取子指令名字中的任意部分字母,只要按顺序匹配下来能找到指定task就能正确执行Gradle指令。这样就大大减少了输入。
BUCK的target往往是很长的路径,虽然通过bash的补全也无需太多记忆,但是输入起来还是相当繁琐。BUCK可以在配置文件里设置别名,但是个人还是觉得没有缩写形式那么好:既能通过全名提供更多含义,又能使用短名执行。
工具集成(IDE Integration)
从图形界面回到终端里已经提高现代化开发都是基于可视化的界面,大部分时间里也都是在集成开发环境中操作。对于Android开发,用得比较多的就是Android Studio和Intellij IDEA(好吧其实他们是一个东西)。从体验上讲,通过界面进行创建选择也是远远比敲击指令参数要好太多太多。
Gradle在Intellij的集成非常的完善:
在Android Studio里创建新的模块有可以直接通过界面来选择,并且提供各类模板
配置模块的依赖也很直观,可以选择类型以及作用域,保存后会自动更新相应的build.gradle文件
还可以直接在项目中创建Gradle的configuration来指定执行指令,这样就不需要切换到终端应用程序,直接在开发环境中运行。结合快捷键,操作起来就更加得心应手
因为Gradle是开源的,所以在Intellij中也能直接跳转和浏览Gradle的源码
上边说的这一切,BUCK都做不到。BUCK在Intellij里的唯一集成可能就是官方提供的插件,但是也仅限于BUCK文件内容和格式的调整。创建新的BUCK模块或者添加依赖都只能通过手动直接编辑文本文件来完成。所以其进步的空间还有很大。
以上都是个人对Gradle和BUCK的闲言碎语,我不是编译工具的编写者,只是使用者。我也没法深刻的提供意见来说明在项目中选择哪个会更有利,只是日常工作和个人项目中都会使用这两个工具,所以写点个人体验心得来抒发一下。