解决组件化使用ButterKnife在Module中的坑

本文详细介绍了ButterKnife在Android组件化开发中的配置及使用方法,包括Gradle配置、常见问题解决及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  学之广在于不倦,不倦在于固志。 ——晋·葛洪­

 (学问的渊博在于学习时不知道厌倦,而学习不知厌倦在于有坚定的目标)


前景:

   最近闲来无事便开始学习组件化开发,网上各种搜索之后还是有颇多收获,于是很兴奋的开始模块化开发......

   然而,刚开始就引入ButterKnife插件就各种问题,组件化学习的热情瞬间减半,不服输的性格让我和ButterKnife杠上了,于是开始ButterKnife的填坑之旅

001.网上各种搜索无果,基本都是studio 3.0.0之前的或者就是3.0.0版本的,又或者ButterKnife是8.4.0之前的,等等各种差异致使我的问题还没有解决?

   ---> 关于3.0.0之前版本的解决办法:

         这个大神给出了Nice的方案

   ---> 我的版本:

             studio 3.1.2    ButterKnife 8.8.1  编译版本 27   target 27

   ---> 组件化开发出的问题,单模块按照官网即可

              ButterKnife官网链接

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()
        //BaseRecyclerViewAdapterHelper
        maven { 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的解释:

    解析R2与R的区别

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 :欢迎探讨指正!文中链接为参考文献!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值