問題一
主APP里面報(bào)錯(cuò):
tools:replace specified at line: for attribute android:appComponentFactory, but no new value specified main manifest (this file), line
Attribute application@appComponentFactory value=(androidx.core.app.CoreComponentFactory) from [androidx.core:core:1.3.1] AndroidManifest.xml:24:18-86
is also present at [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 value=(android.support.v4.app.CoreComponentFactory).
Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:9:5-24:19 to override.
按照提示添加,
tools:replace="android:appComponentFactory"
仍然報(bào)錯(cuò),
> Task :app:processDebugMainManifest FAILED
[androidx.versionedparcelable:versionedparcelable:1.1.0] C:\Users\username\.gradle\caches\transforms-2\files-2.1\ee32a41b299ebcf4e78d691344f52243\versionedparcelable-1.1.0\AndroidManifest.xml Warning:
Package name 'androidx.versionedparcelable' used in: androidx.versionedparcelable:versionedparcelable:1.1.0, com.android.support:versionedparcelable:28.0.0.
H:\Android\practice\QQ\app\src\main\AndroidManifest.xml:9:5-25:19 Error:
tools:replace specified at line:9 for attribute android:appComponentFactory, but no new value specified
H:\Android\practice\QQ\app\src\main\AndroidManifest.xml Error:
Validation failed, exiting
最后在APP里面的清單文件加入栅迄,不用再別的module里面添加
android:appComponentFactory="renyizifu任意字符鉴裹,不能為空衫贬,仍然報(bào)錯(cuò)千劈,但是不影響運(yùn)行"
tools:replace="android:appComponentFactory"
2021-11-23 19:02:20.045 14285-14285/com.cc.qq E/LoadedApk: Unable to instantiate appComponentFactory
java.lang.ClassNotFoundException: Didn't find class "com.cc.qq.AppComponentFactory" on path: DexPathList[[zip file "/data/app/com.cc.qq-VXr3KxaFWBhe82itimOPVA==/base.apk"],nativeLibraryDirectories=[/data/app/com.cc.qq-VXr3KxaFWBhe82itimOPVA==/lib/arm64, /system/lib64, /hw_product/lib64, /system/product/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:209)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.LoadedApk.createAppFactory(LoadedApk.java:270)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:1005)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:1129)
at android.app.LoadedApk.getResources(LoadedApk.java:1419)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2662)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2644)
繼續(xù)報(bào)錯(cuò)
Duplicate class android.support.v4.app.INotificationSideChannel found
in modules core-1.3.1-runtime (androidx.core:core:1.3.1)
and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.3.1-runtime (androidx.core:core:1.3.1) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.3.1-runtime (androidx.core:core:1.3.1) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.3.1-runtime (androidx.core:core:1.3.1) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)
Duplicate class android.support.v4.graphics.drawable.IconCompatParc
....
Duplicate class androidx.core.graphics.drawable.IconCompatParcelizer found in modules core-1.3.1-runtime (androidx.core:core:1.3.1) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)
Duplicate class androidx.core.internal.package-info found in modules core-1.3.1-runtime (androidx.core:core:1.3.1) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0)
Duplicate class androidx.versionedparcelable.CustomVersionedParcelable found in modules versionedparcelable-1.1.0-runtime (androidx.versionedparcelable:versionedparcelable:1.1.0) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0)
Duplicate class androidx.versionedparcelable
* What went wrong:
Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
> 2 files found with path 'META-INF/androidx.cursoradapter_cursoradapter.version'.
Adding a packagingOptions block may help, please refer to
https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.PackagingOptions.html
....
解決方案:根目錄下的gradle.properties添加
# 這是因?yàn)榛旌现С謳爝汉住Mㄟ^添加這些行選擇androidX作為您的支持庫
android.useAndroidX=true
#上面一句默認(rèn)存在,添加下面一句
android.enableJetifier=true
總的添加代碼為
android.useAndroidX=true
# 這是因?yàn)榛旌现С謳焖市颉Mㄟ^添加這些行選擇androidX作為您的支持庫
android.enableJetifier=true
android:appComponentFactory="任意字符"
tools:replace="android:appComponentFactory"
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.QQ"
android:appComponentFactory="任意字符x"
tools:replace="android:appComponentFactory">
組件化注意事項(xiàng)
一忍些、commonLib包中引入第三方的庫為api 而非implementation,不然Module引入調(diào)用不到該庫依賴
implementation 'androidx.appcompat:appcompat:1.2.0'
// 替換成最新版本, 需要注意的是api
api 'com.alibaba:arouter-api:1.5.2'
annotationProcessor 'com.alibaba:arouter-compiler:1.5.2'
api "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// optional - RxJava2 support for Room
api "androidx.room:room-rxjava2:$room_version"
// optional - RxJava3 support for Room
api "androidx.room:room-rxjava3:$room_version"
二坎怪、組件化里面的字module處理事項(xiàng)
1罢坝、組件里面的module里面需要合并AndroidManifest.xml資源、android 目錄下,跟defaultConfig 同級(jí)
2嘁酿、判斷是否為發(fā)布模式變更application和Library隙券,跟android節(jié)點(diǎn)同級(jí),根目錄
3闹司、判斷是否為發(fā)布模式變更是否有applicationID等娱仔,android-defaultConfig 節(jié)點(diǎn)下
4、有自定義處理器需要添加選項(xiàng)配置參數(shù)defaultConfig 節(jié)點(diǎn)
比如order中
if (isRelease) {
// 如果是發(fā)布版本時(shí)游桩,各個(gè)模塊都不能獨(dú)立運(yùn)行
apply plugin: 'com.android.library'
} else {
apply plugin: 'com.android.application'
}
android {
...
defaultConfig {
....
if (!isRelease) {
// 如果是集成化模式牲迫,不能有applicationId組件化模式能獨(dú)立運(yùn)行才能有applicationId
applicationId "com.cc.order"
}
// 這個(gè)方法接收三個(gè)非空的參數(shù),第一個(gè):確定值的類型借卧,第二個(gè):指定key的名字盹憎,第三個(gè):傳值(必須是String)
// 為什么需要定義這個(gè)?因?yàn)閟rc代碼中有可能需要用到跨模塊交互铐刘,如果是組件化模塊顯然不行
// 切記:不能在android根節(jié)點(diǎn)陪每,只能在defaultConfig或buildTypes節(jié)點(diǎn)下
buildConfigField("boolean", "isRelease", String.valueOf(isRelease))
// 在gradle文件中配置選項(xiàng)參數(shù)值(用于APT傳參接收)
// 切記:必須寫在defaultConfig節(jié)點(diǎn)下
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName: project.getName(), packageNameForAPT: packageNameForAPT]
}
}
// 配置資源路徑,方便測試環(huán)境镰吵,打包不集成到正式環(huán)境
sourceSets {
main {
if (!isRelease) {
// 如果是組件化模式檩禾,需要單獨(dú)運(yùn)行時(shí)
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
} else {
// 集成化模式,整個(gè)項(xiàng)目打包apk
manifest.srcFile 'src/main/AndroidManifest.xml'
java {
// release 時(shí) debug 目錄下文件不需要合并到主工程
exclude '**/debug/**'
}
}
}
}
}
三疤祭、清單文件合并子Module中在modulename->src->main->下創(chuàng)建debug文件盼产,跟java同級(jí),復(fù)制AndroidManifest.xml到debug目錄中画株。
四辆飘、AutoService、SPI機(jī)制的resource谓传、跨進(jìn)程通信的aidl蜈项、清單文件合并的debug文件都在src->main目錄下
三、集成的問題
1续挟、各個(gè)module的布局一定不能名字重復(fù)
2紧卒、各個(gè)module的path路徑一定要注意不能重復(fù)
3、初始化ARouter的時(shí)候看看打印日志if環(huán)境诗祸,不然AROuter日志不打印
轉(zhuǎn)換異常跑芳,但是導(dǎo)包正常,一般是布局名字重復(fù)直颅,更改布局名字
2021-11-24 01:05:32.922 3684-3684/com.cc.qq E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cc.qq, PID: 3684
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cc.qq/com.cc.personnal.MainActivity}: java.lang.ClassCastException: com.cc.qq.databinding.ActivityMainBindingImpl cannot be cast to com.cc.personnal.databinding.ActivityMainBinding
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4060)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4247)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2613)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8668)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
Caused by: java.lang.ClassCastException: com.cc.qq.databinding.ActivityMainBindingImpl cannot be cast to com.cc.personnal.databinding.ActivityMainBinding
at com.cc.personnal.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:8214)
at android.app.Activity.performCreate(Activity.java:8202)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
各個(gè)module的path路徑一定要注意不能重復(fù)博个,不匹配的情況,寫成常量
package com.cc.common.arouter;
public class RouterPath {
public static final String APP_GROUP="app";
public static final String ORDER_GROUP="order";
public static final String PERSONAL_GROUP="personal";
//app
public static final String ROUT_APP_MainActivity="/app/MainActivity";
//order
public static final String ROUT_ORDER_MainActivity="/order/MainActivity";
//personal
public static final String ROUT_PERSONAL_MainActivity="/pesonal/MainActivity";
}
3、注意初始化的BuildConfig不要弄錯(cuò)了功偿,沒有日志證明if為false
// 這兩行必須寫在init之前盆佣,否則這些配置在init過程中將無效
if (BuildConfig.DEBUG) {
// 打印日志
ARouter.openLog();
// 開啟調(diào)試模式(如果在InstantRun模式下運(yùn)行,必須開啟調(diào)試模式!線上版本需要關(guān)閉,否則有安全風(fēng)險(xiǎn))
ARouter.openDebug();
}
// 盡可能早共耍,推薦在Application中初始化
ARouter.init(this);
四虑灰、注解處理器的問題,每個(gè)gradle文件下要添加注解處理器的生成目錄
android{
...
defaultConfig{
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
}
五痹兜、注解處理器dependencies下的依賴問題
1穆咐、commonLibrary依賴ARouter:api
,透傳
2字旭、AppModule对湃、OrderModule、PersonalModule依賴commonLibrary:implementation
3遗淳、AppModule依賴OrderModule熟尉、PersonalModule:implementation
4、AppModule依賴了OrderModule洲脂、PersonalModule,可不依賴commonLirabry剧包,那AppModule依賴了OrderModule恐锦、PersonalModule改為api
五的點(diǎn)有問題驗(yàn)證,dependencies中最好都加上
implementation project(':common') // 公共基礎(chǔ)庫
implementation 'com.alibaba:arouter-api:1.5.2'
annotationProcessor 'com.alibaba:arouter-compiler:1.5.2'