在 Android 的日常開發(fā)中,我們會使用到大量的第三方庫或者自己編寫的組件庫牲芋,這些依賴庫中資源加上主工程本身的資源撩笆,可能會發(fā)生同名沖突,會發(fā)生資源相互覆蓋的現(xiàn)象缸浦。
常用的解決方案:
1.在 module 中的所有資源前加上它的 module 名稱夕冲,例如library_help_text
- 在app模塊引用greet字符串就會出現(xiàn)資源沖突問題。解決方法也挺簡單裂逐,就是給每個子模塊給資源名添加前綴歹鱼,具體做法是在每個子模塊的build.gradle文件的android塊內(nèi)添加一行代碼:resourcePrefix “資源名前綴”。
// me模塊的build.gradle文件...android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
resourcePrefix "me_"
...}...// user模塊的build.gradle文件...android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
resourcePrefix "user_"
...}...
如果修改后卜高,那么對應(yīng)的資源文件也要做相應(yīng)的修改弥姻。
// user模塊
<resources> ...
<string name="user_greet">Hello!</string> ...</resources>
// me模塊
<resources> ...
<string name="me_greet">Hi!</string> ...</resources>
- 設(shè)置android.nonTransitiveRClass=true
在使用時南片,如app引用a module 下的red 顏色資源,需要從從R.color.red改為 com.excample.a.R.color.red. - 啥都不做庭敦,自己命名的時候多注意疼进,別重復
refer:
https://juejin.cn/post/7170562275374268447
https://www.epubit.com/articleDetails?id=Na133fc81-af1b-4669-bfa5-0556dd825df7
https://juejin.cn/post/7096344799103418399
https://juejin.cn/post/6844903993894780942