ButterKnife可以大量的减少android开发过程中findViewById之类的枯燥代码。推荐在每一个android项目中使用,可以提高不少工作效率。ButterKnife的官网地址为:ButterKnife
重要类和结构
butterknife-annotations module
该子项目定义了butterknife所需要的注解,其中注解包括两类,一类用于绑定resource(例如:BindArray, BindBitmap, BindBool…), 一类用来view绑定回调(例如:OnClick, OnCheckedChanged,onItemclick…)
butterknife-compiler
该子项目定义了注解编译器ButterKnifeProcess,用于在编译阶段,对项目代码进行注解处理,生成一系列对应的java文件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
butterknife module
- Butterknife
暴露给用户的主要接口类,主要提供各种bind接口- BINDINGS:Map
, Constructor<? extends Unbinder>>
映射关系,class->unbinder,用于缓存之前解析过的class的结果。 - createBinding(Object target, View source)
所有bind接口最终都会进入该方法中。该方法会调用findBindingConstructorForClass方法,该方法通过反射机制获取对应ViewBinding类,进而获取对应的ViewBinding类的Constructor。在createBinding的最后调用改constructor实例化对应的Viewbinding类。由于每一个ViewBinding类都集成了Unbinder接口,因此返回的是一个Unbinder
- BINDINGS:Map
|
|
|
|
学习知识点
android-apt切换为annotationProcessor
当gradle插件版本升级到2.2之上后,可以使用androidProcessor替换android-apt。具体做法如下
修改根目录的gradle插件版本,并移除android-apt
12345678910buildscript {repositories {jcenter()}dependencies {classpath 'com.android.tools.build:gradle:2.2.2'//remove android-apt//classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'}}移除对应Module的build.gradle中对android-apt配置
12//remove//apply plugin: 'com.neenbedankt.android-apt'修改对应build.gradle的dependencies中原使用apt地方,换成annotationProcessor
123456dependencies {// update apt to annotationProcessor//apt 'com.google.dagger:dagger-compiler:2.2'annotationProcessor 'com.google.dagger:dagger-compiler:2.2'compile 'com.google.dagger:dagger:2.2}
gradle plugin checkstyle
checkstyle plugin提供了一些task用于对project质量进行测试,使用方式如下:
1234567...apply plugin: 'checkstyle'...checkstyle { //指定checkstyle一些配置configFile rootProject.file('checkstyle.xml')showViolations true}具体的checkstyle说明可以参考checkstyle
gradle from的使用
当多个子项目具有重复的gradle配置代码时,可以将重复部分提取出来,例如将多个子项目的upload maven代码统一提取出来,放到project项目的如下,例如放到项目根目录的gradle/gradle-maven-push.gradle:
12345678910111213141516171819202122232425262728293031323334353637apply plugin: 'maven'apply plugin: 'signing'afterEvaluate { project ->uploadArchives {repositories {mavenDeployer {beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }pom.groupId = GROUPpom.artifactId = POM_ARTIFACT_IDpom.version = VERSION_NAMErepository(url: RELEASE_REPOSITORY_URL) {authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)}snapshotRepository(url: SNAPSHOT_REPOSITORY_URL) {authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)}pom.whenConfigured { pom ->pom.dependencies.forEach { dep ->if (dep.getVersion() == "unspecified") {dep.setGroupId(GROUP)dep.setVersion(VERSION_NAME)}}}}}}signing {required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }sign configurations.archives}}在子项目的build.gradle中进行gradle from引用,例如
1apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
第三方类库
AutoService
A configuration/metadata generator for java.util.ServiceLoader-style service providersJavaPoet
JavaPoet is a Java API for generating .java source files.
Source file generation can be useful when doing things such as annotation processing or interacting with metadata files (e.g., database schemas, protocol formats). By generating code, you eliminate the need to write boilerplate while also keeping a single source of truth for the metadata.Auto-common
The Auto project has a set of common utilities to help ease use of the annotation processing environment.robolectric
Robolectric is a testing framework that de-fangs the Android SDK so you can test-drive the development of your Android app.