首先在工程的根build.gradle里配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
buildscript { repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.1.3" classpath "com.jakewharton:butterknife-gradle-plugin:10.2.3" } } ext { // 关键字。项目的组件化配置 lib = [ butterknife : 'com.jakewharton:butterknife:10.2.3', butterknife_compiler: 'com.jakewharton:butterknife-compiler:10.2.3', ] } |
然后在基础库组件中引入
1 2 3 4 5 6 7 |
def extend = rootProject.ext // 项目/build.gradle内的ext。 apply plugin: 'com.android.library' // 当前组件仅作为库使用 dependencies { api extend.lib.butterknife } |
最后在UI相关组件中引用基础库组件,即自动引入了Butterknife
1 2 3 4 5 6 7 8 9 |
def extend = rootProject.ext // 项目/build.gradle内的ext。 apply plugin: 'com.jakewharton.butterknife' dependencies { implementation project(":moduleBase:libraryBase") annotationProcessor extend.lib.butterknife_compiler } |
最终代码里使用“R2”访问view。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import butterknife.ButterKnife; import butterknife.OnClick; public class LoginActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); ButterKnife.bind(this); } @OnClick(R2.id.btn_loginin) // 本module的applicationId下的R2 public void onBtnLoginClick() { // dosth } } |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3176.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设