3. Gradle样式

3.1 依赖

3.1.1 依赖的版本

有时,多个依赖性有相同的版本那么相同的版本应该定义为变量。 例如

final SUPPORT_LIBRARY_VERSION = '23.4.0'
compile "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:percent:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:customtabs:$SUPPORT_LIBRARY_VERSION"

未来只需要更改一个变量值就更改多个依赖的版本,这样是非常方便的。

3.1.2 依赖的分组

有时,我们需要根据包名把依赖进行分组,组与组之间用空行隔开,比如:

compile "com.android.support:percent:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:customtabs:$SUPPORT_LIBRARY_VERSION"
compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.5'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.jakewharton.timber:timber:4.1.2'
compile 'com.github.bumptech.glide:glide:3.7.0'

compiletestCompileandroidTestCompile 依赖应该分到它们相应的组,比如:

// App Dependencies
compile "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION"
// Instrumentation test dependencies
androidTestCompile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION"
// Unit tests dependencies
testCompile 'org.robolectric:robolectric:3.0'

这两种方法使得寻找依赖变得容易,也使得依赖变得干净和整洁🙌

3.1.3 单独的依赖

有时,依赖只用于应用或者测试,就应该确保他们只编译在 compiletestCompileandroidTestCompile。比如,robolectric依赖只用于单元测试,他就应该是这样:

testCompile 'org.robolectric:robolectric:3.0'