ButterKnife在組件化中的切換問題,報錯的解決方法

在moduel_lib中添加配置文件

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    api ('com.alibaba:arouter-api:1.3.0')
            {
              exclude module: 'support-v4'//根據(jù)組件名排除,如果不添加該代碼,則會出現(xiàn)地下的包報錯,原因是重復引入
             // exclude group: 'android.support.v4'//根據(jù)包名排除
            }
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    api 'com.jakewharton:butterknife:8.8.1'

}

記住添加的版本是8.8.1,這個時候也應用xml中的資源的時候用的都是R2,layout用的是R,構建工具是V4.4

如果是:V10.2.0版本,這個版本支持AndroidX,會出現(xiàn)構建工具問題,需要使用androidX,感覺麻煩你沒有用了。

在總項目的配置文件中添加

 dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
// 添加下邊的內(nèi)容,注意版本號,我這里用這個版本呢,
這個構建插件器,支持直接library和application的直接切換。
        classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0'
 
    }

在用到ButterKnife中添加依賴 apply it in your module,注意是每個用到ButterKnife注解的組件中,都要添加這個依賴。

if (isModule.toBoolean()) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}
apply plugin: 'com.jakewharton.butterknife'

這個時候就可以自由切換了,但是全部的R得改成R2,不需要再手動,切換了。

項目代碼:

public class MainActivity extends Activity {
   // Button btn;
    @BindView(R2.id.reregister)
    Button reregister;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //btn = (Button) findViewById(R.id.reregister);
        reregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/girls/list").navigation();
                MainActivity.this.finish();
            }
        });
    }
}

備注:碰到奇怪的現(xiàn)象,但是也總結一下把,應該是:
組件合并,控件用R2,組件拆分,用R;然后就是layout使用的是R來調用資源。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容