解決元件化使用ButterKnife在Module中的坑
學之廣在於不倦,不倦在於固志。 ——晉·葛洪
(學問的淵博在於學習時不知道厭倦,而學習不知厭倦在於有堅定的目標)
前景:
最近閒來無事便開始學習元件化開發,網上各種搜尋之後還是有頗多收穫,於是很興奮的開始模組化開發......
然而,剛開始就引入ButterKnife外掛就各種問題,元件化學習的熱情瞬間減半,不服輸的性格讓我和ButterKnife槓上了,於是開始ButterKnife的填坑之旅
001.網上各種搜尋無果,基本都是studio 3.0.0之前的或者就是3.0.0版本的,又或者ButterKnife是8.4.0之前的,等等各種差異致使我的問題還沒有解決?
---> 關於3.0.0之前版本的解決辦法:
---> 我的版本:
studio 3.1.2 ButterKnife 8.8.1 編譯版本 27 target 27
---> 元件化開發出的問題,單模組按照官網即可
002.基於我的版本開始填坑:
---> 首先配置你的專案Project的 build.gradle中新增如下配置(圖中標紅):
圖中標紅字型,必須是8.4.0版本,否則子模組裡面新增 apply plugin xxx時會報錯!(各種嘗試啊。。。)
有老鐵反應8.5.0版本可以,經測可行,目前測試 8.4.0、8.5.0、8.5.1可行,其餘自行測試,感謝老鐵提醒!
buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' } } allprojects { repositories { google() jcenter() mavenCentral() //BaseRecyclerViewAdapterHelpermaven { url "https://jitpack.io" } } } task clean(type: Delete) { delete rootProject.buildDir }
---> 下來在你凡是使用ButterKnife的子模組中新增如下配置(圖中標紅):
apply plugin: 'com.android.library' apply plugin: 'com.jakewharton.butterknife' android { compileSdkVersion 27 ...... dependencies { ......
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' .....
---> 下來需要在你的核心模組(即你頂層模組)中需要新增如下配置(圖中標紅):
小記:此處核心模組即我所有模組都依賴這個模組,此處需要新增Butterknife的api 依賴
個人建議,你模組中一個地方引用Butterknife的api即可
apply plugin: 'com.android.library' apply plugin: 'com.jakewharton.butterknife' android { compileSdkVersion 27 ...... } dependencies { ...... //butterknife依賴 api 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' ...... }
---> 看一下我的主專案(即App moudle)(主模組依賴核心模組所以沒有Butterknife的api依賴):
apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { applicationId "xxx" minSdkVersion 15 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" multiDexEnabled true ...... } dependencies { ...... annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' ...... }
---> 配置完成之後,syns,之後多rebuild幾次、多rebuild幾次、多rebuild幾次,你懂的!
---> 注意在主專案和之前使用時一模一樣的,但是在子模組就需要把 R改為R2,注意!
主模組(app)
@BindView(R.id.tivTitleLeft) TextImageView mTivTitleLeft; @BindView(R.id.tvTitle) TextView mTvTitle; @BindView(R.id.iv_picture_one) ImageView mIvPictureOne; @BindView(R.id.tv_content_one) TextView mTvContentOne; @BindView(R.id.iv_picture_two) ImageView mIvPictureTwo; @BindView(R.id.tv_content_two) TextView mTvContentTwo;
子moudle中
@BindView(R2.id.tv_launcher_timer) AppCompatTextView mTvTimer = null;
003.關於子模組R2的解釋:
004.注意事項:
---> 在Android Studio 3.0 之後,annotationProcessor 代替了 android-apt
---> 子 moudle中的 butterknife 被主 module 使用,需要將 implementation 改為 api(關於兩者的區別自行百度)
---> 子moudle中的不可以使用switch case,必須要用if else來的代替,否則空指標等其他錯誤。
必須是註解裡面是R2,具體對應的id時使用R,不然還有坑
@OnClick({R2.id.textView, R2.id.button1, R2.id.button2, R2.id.button3, R2.id.image}) public void onViewClicked(View view) { int i = view.getId(); if (i == R.id.textView) { } else if (i == R.id.button1) { } else if (i == R.id.button2) { } else if (i == R.id.button3) { } else if (i == R.id.tv) { } }
Last :歡迎探討指正!文中連結為參考文獻!