组件化开发中使用ButterKnife的使用

在组件化开发中Butterknife的使用存在许多的问题,最开始的时候我以为直接引入Butterknife直接引入到项目中就可以了呢!但是后来发现不行,会有各种各样的错误,所以用这篇文章记录一下,防止其他人采坑!!!

先来说一下项目,我们是在公共modules中引入了一些基础组件的!所以我的主要的一些引用都放在那个基础模块中去了!其实这个基础组件也就相当于一个类库而已了!

先说一下你可能遇到的问题:

  • 编译不通过报错;
  • butterknife报空指针问题;
  • 项目中一些类库不起作用;
  • 在其它Module中使用Butterknfie怎么使用(主要是解决R资源的问题)

这些都是我在集成的时候遇到的问题,我们一个一个解决上面的问题;

1. 编译不通过怎么解决:

只要是配置正确的话(亲测可用),不会出现编译不通过的问题,但是这里要注意的就是各个配置的位置!!!

1.1 关于配置

  1. 首先你要在 项目 的build.gradle中添加如下内容:

classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'复制代码

请注意这里使用的不是最新的版本(当前最新版本是8.8.1),这里使用的是8.4.0这个版本,别问我为什么!我是真的不知道,如果换成最新的话,各种报错!请原谅我的无知,就酱紫。。。

 

整体代码如下:

buildscript {

 

    repositories {

        google()

        jcenter()

    }

    dependencies {

        classpath "com.android.tools.build:gradle:$localGradlePluginVersion"

        //为了解决Butterknife组件化开发中的问题,但是这里只能是8.4.0、8.5.0、8.5.1

        classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'

        // NOTE: Do not place your application dependencies here; they belong

        // in the individual module build.gradle files

    }

}

 

allprojects {

    repositories {

        google()

        jcenter()

    }

}

 

task clean(type: Delete) {

    delete rootProject.buildDir

}

}复制代码

  1. 在主项目里,也就是(app的build.gradle中添加如下内容)

annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'复制代码

这里为什么只添加了一个,没有添加另一个呢?因为我项目中把基础类库分到了相应的module中,所以这里这么写呢?因为**'com.jakewharton:butterknife:8.8.1'这个东西要写在相应的library module**中,否则会报相应的空指针。所以才把这两个内容分开的!

 

整体代码如下:

apply plugin: 'com.android.application'

 

android {

    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {

        applicationId "com.jinlong.used"

        minSdkVersion rootProject.ext.minSdkVersion

        targetSdkVersion rootProject.ext.targetSdkVersion

        versionCode rootProject.ext.versionCode

        versionName rootProject.ext.versionName

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {

        release {

            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }

    }

}

 

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    //引入基础包,这个基础包是整体是一些基础的数据

    implementation project(':modules:common')

    //引入butterknife

    //这个东西一定要放在这个里面,否则会报错的(空指针)!

    annotationProcessor "com.jakewharton:butterknife-compiler:$butterknifeVersion"

}复制代码

别管我写的乱七八糟的,那个是便于版本管理的!

  1. 最后在library module的类库只添加相应butterknife的引用

api 'com.jakewharton:butterknife:8.8.1'复制代码

细心的朋友可能会问?这个api是个什么鬼?这里为什么用api呢?其实是这样的,在Android studio 3.1.2之后的版本中(据说是因为编译更快):

  • compile 要用 implementation 或 api 替换
  • testCompile 要用 testImplementation 或 testApi 替换
  • androidTestCompile 要用 androidTestImplementation 或 androidTestApi 替换 有什么区别呢?是这样的,前面是implementation引用的只能在Module中使用,在其他引用这个类库的module中是使用不了的!而api这个呢,就可以在引用这个类库的module中去使用!因为我这个是在基础类库中使用的,所以只能使用api类型的!就这么简单。

 

整体代码如下:

apply plugin: 'com.android.library'

 

android {

    compileSdkVersion rootProject.ext.compileSdkVersion

 

    defaultConfig {

        minSdkVersion rootProject.ext.minSdkVersion

        targetSdkVersion rootProject.ext.targetSdkVersion

        versionCode rootProject.ext.versionCode

        versionName rootProject.ext.versionName

 

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

 

    buildTypes {

        release {

            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }

    }

 

}

 

dependencies {

    //这里踩了一个坑 implementation 只能是在模块内部使用而api则是其他模块可以使用

    //compile 要用 implementation 或 api 替换

    //testCompile 要用 testImplementation 或 testApi 替换

    //androidTestCompile 要用 androidTestImplementation 或 androidTestApi 替换

 

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    api "com.android.support:appcompat-v7:$rootProject.appcompatVersion"

    api "com.android.support.constraint:constraint-layout:$rootProject.constraintLayoutVersion"

    testImplementation "junit:junit:$rootProject.junitVersion"

    androidTestImplementation "com.android.support.test:runner:$rootProject.runnerVersion"

    androidTestImplementation "com.android.support.test.espresso:espresso-core:$rootProject.espressoCoreVersion"

 

    //引入butterknife

    api "com.jakewharton:butterknife:$butterknifeVersion"

}复制代码

  1. 组件化开发的时候,要在相应的组件中添加下面这句

apply plugin: 'com.jakewharton.butterknife'复制代码

这句添加在最上面(也就是你判断是组件还是module的地方),就可以了!

在组件中把所有R换成R2就可以了!

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值