68、Butterknife9.0.0在多module中配置使用

一、背景:

由于公司项目越来越大,决定使用组件化的方式进行开发。
在考虑将butterknife封装到base_module提供给其他module使用时发现,
在A module、B module中无法使用。在主工程 App使用时,总是报空指针。
(App依赖A module、B module、base_module)
(A module、B module依赖base_module)

经过入坑爬坑一番,大致摸索出 Butterknife在公共module配置后提供给各个子module使用。

二、配置方法:
1、环境:

由于butterknife不同的版本 对android studio 版本和android gradle plugin 版本都有不同的要求

android studio版本:4.0
AGP android gradle plugin 的版本3.5.0

 classpath 'com.android.tools.build:gradle:3.5.0'// plugin
2、具体步骤:

①、添加Butterknife的配置plugin在 buildscript写错位置概不负责!

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.android.tools.build:gradle:3.5.0'// plugin 注意版本
    classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0'
  }
}

②、在base_module中添加Butterknife依赖 这是在base_module!!! 这是在base_module!!! 这是在base_module!!!

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
dependencies {
  api 'com.jakewharton:butterknife:9.9.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0'
}

③、在A module或者B module配置

apply plugin: 'com.android.library'
// 官方文档 或者好多文章说只配置这段代码就行了
// 实际运行的过程中 总会出现空指针问题
// 所以还需要在dependencies中添加配置
apply plugin: 'com.jakewharton.butterknife'
dependencies {
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0'
}

在A module或者B module 以R2.id.XX 的方式 使用:

public class DoActivity extends Activity {
    @BindView(R2.id.pic_grid)
    NoScrollGridView mGrid;
	private Unbinder mUnbinder;

	  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.vps_activity_do_home_work);
        // 绑定
        mUnbinder = ButterKnife.bind(this);
    }
	// 别忘记解绑
	 @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}

③、在App配置:
在App的配置和在Amodule的配置相同

apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'
dependencies {
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0'
}

在App 以R.id.XX 的形式使用:

 @BindView(R.id.middle_tv)
 TextView mTitleMiddle;

至此Butterknife的配置使用已经完成

三、爬的坑:

1、由于项目中Buffterknife的版本比较老 ,是7.1.0的版本,所以在替换新的版本时

// 旧的使用
 @Bind(R.id.middle_tv)
// 新的使用
 @BindView(R.id.middle_tv)

2、由于Buffterknife在8.4.0之后才支持 在library中使用,所以
多module开发使用请升级到8.4.0以上版本

3、ButterKnife8.8.1版本

使用classpath ‘com.jakewharton:butterknife-gradle-plugin:8.8.1’,报错:

ERROR: Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.
Possible causes for this unexpected error include:
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)

The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)

Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

是因为butterknife的gradle插件还未支持gradle3.0,只能降级到8.4.0,或者升级到9.0.0。

先试着降级到8.4.0,报错

The Android Gradle plugin supports only Butterknife Gradle plugin version 9.0.0-rc2 and higher.
The following dependencies do not satisfy the required version:
root project 'AndroidModuleDemo' -> com.jakewharton:butterknife-gradle-plugin:8.4.0

提示只能在 9.0.0-rc2 以上版本,那么再使用 9.0.0 的稳定版本看下效果。

classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0'
四、建议和处理:

1、如果 module 中如果需要使用 butterknife 寻找控件,一定要添加 plugin,目的是为了生成R2文件。

apply plugin: 'com.jakewharton.butterknife'

还要在 module 下的 build.gradle 中添加 annotationProcessor,目的是为了自动生成java文件。

dependencies {
    annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0'
}

2、Library 中需要使用 R2.id. 才能找到控件,原因是 ButterKnife 的注解必须是常量,在Library 中的 R.id. 并不是常量,生成的 R2 才是 final 修饰的常量。**

@BindView(R2.id.tv_light)
TextView tvLight;

3、如果使用 androidx需要使用 10.1.0 版本,否则使用 9.0.0 版本。前提是,你可以正确的使用androidx。

4、如果AGP android gradle plugin 的版本3.6.0 及以上版本 请使用 10.2.1版本。否则会出现 R2 id 为 0x0的问题!

@IdRes
public static final int etPanNumber = 0x0;

@IdRes
public static final int etPassword = 0x0;

@IdRes
public static final int etPhoneNum = 0x0;

@IdRes
public static final int etPinCode = 0x0;```

处理方式:
①、降低AGP的版本 4.0–>3.5 (验证通过)

 classpath 'com.android.tools.build:gradle:3.5.0'// plugin

②、升级到AndroidX 使用10.2.1版本
在这里插入图片描述

后记:

如果不是维护旧的项目,可能不会再使用到Butterknife了。Butterknife作者也不建议再继续使用了。
都快如 databinding的大家庭吧!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值