Android Studio依赖冲突排查解决方法

一、直入主题

1、先使用cd命令回到AS项目的根目录,也就是gradlew所在的目录,正常来说,你点开Android Studio的Teminal,默认显示的路径就是项目的根目录,如下图所示:
在这里插入图片描述
2、输入命令:gradlew 子模块名:dependencies,就可以显示出对应项目的依赖树,比如:
1)先看看Demo build.gradle设置的依赖关系

debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.3'

2)输入命令打印Demo模块依赖树

gradlew :Demo:dependencies 
+--- com.squareup.leakcanary:leakcanary-support-fragment:1.6.3
|    +--- com.squareup.leakcanary:leakcanary-android:1.6.3
|    |    +--- com.squareup.leakcanary:leakcanary-analyzer:1.6.3
|    |    |    +--- com.squareup.leakcanary:leakcanary-watcher:1.6.3
|    |    |    +--- com.squareup.haha:haha:2.0.4
|    |    |    |    \--- org.jetbrains.trove4j:trove4j:20160824
|    |    |    \--- com.android.support:support-annotations:26.0.0
|    |    \--- com.android.support:support-core-utils:26.0.0
|    |         +--- com.android.support:support-annotations:26.0.0
|    |         \--- com.android.support:support-compat:26.0.0
|    |              \--- com.android.support:support-annotations:26.0.0
|    \--- com.android.support:support-fragment:26.0.0
|         +--- com.android.support:support-compat:26.0.0 (*)
|         +--- com.android.support:support-core-ui:26.0.0
|         |    +--- com.android.support:support-annotations:26.0.0
|         |    \--- com.android.support:support-compat:26.0.0 (*)
|         \--- com.android.support:support-core-utils:26.0.0 (*)
+--- com.squareup.leakcanary:leakcanary-android:1.6.3 (*)
+--- com.squareup.leakcanary:leakcanary-analyzer:1.6.3 (*)
+--- com.squareup.leakcanary:leakcanary-watcher:1.6.3
+--- com.squareup.haha:haha:2.0.4 (*)
+--- org.jetbrains.trove4j:trove4j:20160824
+--- com.android.support:support-annotations:26.0.0
+--- com.android.support:support-core-utils:26.0.0 (*)
+--- com.android.support:support-compat:26.0.0 (*)
+--- com.android.support:support-fragment:26.0.0 (*)
+--- com.android.support:support-core-ui:26.0.0 (*)
+--- com.squareup.leakcanary:leakcanary-support-fragment:1.6.3 (*)

3)从上图打印结果可以看到com.squareup.leakcanary:leakcanary-support-fragment:1.6.3这个库依赖了很多其他第三方库
3、假如某个库的类出现冲突了,我们可以在类似这样设置exclude group: '组名', module: '模块名'来排除掉传递依赖
1)比如上面Demo 的build.gradle想要排除com.android.support:support-fragment:26.0.0这个依赖,那么可以写成:

debugImplementation ('com.squareup.leakcanary:leakcanary-support-fragment:1.6.3'){
        exclude group: 'com.android.support', module: 'support-fragment'
}

2)重新打印一下Demo的依赖树是否排除掉依赖

+--- com.squareup.leakcanary:leakcanary-support-fragment:1.6.3
|    \--- com.squareup.leakcanary:leakcanary-android:1.6.3
|         +--- com.squareup.leakcanary:leakcanary-analyzer:1.6.3
|         |    +--- com.squareup.leakcanary:leakcanary-watcher:1.6.3
|         |    +--- com.squareup.haha:haha:2.0.4
|         |    |    \--- org.jetbrains.trove4j:trove4j:20160824
|         |    \--- com.android.support:support-annotations:26.0.0
|         \--- com.android.support:support-core-utils:26.0.0
|              +--- com.android.support:support-annotations:26.0.0
|              \--- com.android.support:support-compat:26.0.0
|                   \--- com.android.support:support-annotations:26.0.0
+--- com.squareup.leakcanary:leakcanary-android:1.6.3 (*)
+--- com.squareup.leakcanary:leakcanary-analyzer:1.6.3 (*)
+--- com.squareup.leakcanary:leakcanary-watcher:1.6.3
+--- com.squareup.haha:haha:2.0.4 (*)
+--- org.jetbrains.trove4j:trove4j:20160824
+--- com.android.support:support-annotations:26.0.0
+--- com.android.support:support-core-utils:26.0.0 (*)
+--- com.android.support:support-compat:26.0.0 (*)
+--- com.squareup.leakcanary:leakcanary-support-fragment:1.6.3 (*)

3)从上图,可以看到已经排除掉依赖com.android.support:support-fragment:26.0.0,以此类推,就可以一个个排除掉有冲突的依赖即可,有时命令行输出信息太多,看不过来,可以把打印信息输出到文件:

gradlew :Demo:dependencies >a.txt

二、总结与拓展

1、有人会问,我怎么知道gradlew这个命令怎么用呢?

同样,先是用cd命令回到项目的根目录,然后点开Terminal,输入命令:gradlew --help,然后你可以看到:

USAGE: gradlew [option...] [task...]

-?, -h, --help            Shows this help message.
-a, --no-rebuild          Do not rebuild project dependencies. [deprecated]
-b, --build-file          Specify the build file.
--build-cache             Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds.
-c, --settings-file       Specify the settings file.
--configure-on-demand     Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating]
--console                 Specifies which type of console output to generate. Values are 'plain', 'auto' (default), 'rich' or 'verbose'.
--continue                Continue task execution after a task failure.
-D, --system-prop         Set system property of the JVM (e.g. -Dmyprop=myvalue).
-d, --debug               Log in debug mode (includes normal stacktrace).
--daemon                  Uses the Gradle Daemon to run the build. Starts the Daemon if not running.
--foreground              Starts the Gradle Daemon in the foreground. [incubating]
-g, --gradle-user-home    Specifies the gradle user home directory.
-I, --init-script         Specify an initialization script.
-i, --info                Set log level to info.
--include-build           Include the specified build in the composite. [incubating]
-m, --dry-run             Run the builds with all task actions disabled.
--max-workers             Configure the number of concurrent workers Gradle is allowed to use. [incubating]
--no-build-cache          Disables the Gradle build cache.
--no-configure-on-demand  Disables the use of configuration on demand. [incubating]
--no-daemon               Do not use the Gradle daemon to run the build. Useful occasionally if you have configured Gradle to always run with the daemon by default.
--no-parallel             Disables parallel execution to build projects. [incubating]
--no-scan                 Disables the creation of a build scan. For more information about build scans, please visit https://gradle.com/build-scans. [incubating]
--offline                 Execute the build without accessing network resources.
-P, --project-prop        Set project property for the build script (e.g. -Pmyprop=myvalue).
-p, --project-dir         Specifies the start directory for Gradle. Defaults to current directory.
--parallel                Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use. [incubating]
--profile                 Profile build execution time and generates a report in the <build_dir>/reports/profile directory.
--project-cache-dir       Specify the project-specific cache directory. Defaults to .gradle in the root project directory.
-q, --quiet               Log errors only.
--recompile-scripts       Force build script recompiling. [deprecated]
--refresh-dependencies    Refresh the state of dependencies.
--rerun-tasks             Ignore previously cached task results.
-S, --full-stacktrace     Print out the full (very verbose) stacktrace for all exceptions.
-s, --stacktrace          Print out the stacktrace for all exceptions.
--scan                    Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (https://gradle.com/build-scans) [incubating]
--status                  Shows status of running and recently stopped Gradle Daemon(s).
--stop                    Stops the Gradle Daemon if it is running.
-t, --continuous          Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change. [incubating]
-u, --no-search-upward    Don't search in parent folders for a settings file.
--update-locks            Perform a partial update of the dependency lock, letting passed in module notations change version. [incubating]
-v, --version             Print version info.
-w, --warn                Set log level to warn.
--warning-mode            Specifies which mode of warnings to generate. Values are 'all', 'summary'(default) or 'none'
--write-locks             Persists dependency resolution for locked configurations, ignoring existing locking information if it exists [incubating]
-x, --exclude-task        Specify a task to be excluded from execution.

从上图可以知道,gradlew命令无非就是gradlew [option...] [task...],其中 option就是上图打印出来的类似--scan,而task,点开Android Studio右侧的Gradle-projects,比如,刚才dependencies task:
在这里插入图片描述
从上图可知,想要执行某个模块的某个任务,就是直接模块名:任务名,比如,显示Demo模块有什么任务,可以输入以下命令:gradlew :Demo:tasks

------------------------------------------------------------
All tasks runnable from project :Demo
------------------------------------------------------------

Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for the base and test modules
sourceSets - Prints out all the source sets defined in this project.

Build tasks
-----------
assemble - Assemble main outputs for all the variants.
assembleAndroidTest - Assembles all the Test applications.
assembleAy - Assembles main outputs for all Ay variants.
assembleDebug - Assembles main outputs for all Debug variants.
assembleFt - Assembles main outputs for all Ft variants.
assembleRelease - Assembles main outputs for all Release variants.
assembleSswl - Assembles main outputs for all Sswl variants.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
bundle - Assemble bundles for all the variants.
bundleAy - Assembles bundles for all Ay variants.
bundleDebug - Assembles bundles for all Debug variants.
bundleFt - Assembles bundles for all Ft variants.
bundleRelease - Assembles bundles for all Release variants.
bundleSswl - Assembles bundles for all Sswl variants.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileAyDebugAndroidTestSources
compileAyDebugSources
compileAyDebugUnitTestSources
compileAyReleaseSources
compileAyReleaseUnitTestSources
compileFtDebugAndroidTestSources
compileFtDebugSources
compileFtDebugUnitTestSources
compileFtReleaseSources
compileFtReleaseUnitTestSources
compileSswlDebugAndroidTestSources
compileSswlDebugSources
compileSswlDebugUnitTestSources
compileSswlReleaseSources
compileSswlReleaseUnitTestSources

Cleanup tasks
-------------
lintFix - Runs lint on all variants and applies any safe suggestions to the source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in project ':Demo'.
components - Displays the components produced by project ':Demo'. [incubating]
dependencies - Displays all dependencies declared in project ':Demo'.
dependencyInsight - Displays the insight into a specific dependency in project ':Demo'.
dependentComponents - Displays the dependent components of components in project ':Demo'. [incubating]
help - Displays a help message.
model - Displays the configuration model of project ':Demo'. [incubating]
projects - Displays the sub-projects of project ':Demo'.
properties - Displays the properties of project ':Demo'.
tasks - Displays the tasks runnable from project ':Demo'.

Install tasks
-------------
installAyDebug - Installs the DebugAy build.
installAyDebugAndroidTest - Installs the android (on device) tests for the AyDebug build.
installAyRelease - Installs the ReleaseAy build.
installFtDebug - Installs the DebugFt build.
installFtDebugAndroidTest - Installs the android (on device) tests for the FtDebug build.
installFtRelease - Installs the ReleaseFt build.
installSswlDebug - Installs the DebugSswl build.
installSswlDebugAndroidTest - Installs the android (on device) tests for the SswlDebug build.
installSswlRelease - Installs the ReleaseSswl build.
uninstallAll - Uninstall all applications.
uninstallAyDebug - Uninstalls the DebugAy build.
uninstallAyDebugAndroidTest - Uninstalls the android (on device) tests for the AyDebug build.
uninstallAyRelease - Uninstalls the ReleaseAy build.
uninstallFtDebug - Uninstalls the DebugFt build.
uninstallFtDebugAndroidTest - Uninstalls the android (on device) tests for the FtDebug build.
uninstallFtRelease - Uninstalls the ReleaseFt build.
uninstallSswlDebug - Uninstalls the DebugSswl build.
uninstallSswlDebugAndroidTest - Uninstalls the android (on device) tests for the SswlDebug build.
uninstallSswlRelease - Uninstalls the ReleaseSswl build.

Verification tasks
------------------
check - Runs all checks.
connectedAndroidTest - Installs and runs instrumentation tests for all flavors on connected devices.
connectedAyDebugAndroidTest - Installs and runs the tests for ayDebug on connected devices.
connectedCheck - Runs all device checks on currently connected devices.
connectedFtDebugAndroidTest - Installs and runs the tests for ftDebug on connected devices.
connectedSswlDebugAndroidTest - Installs and runs the tests for sswlDebug on connected devices.
deviceAndroidTest - Installs and runs instrumentation tests using all Device Providers.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
lint - Runs lint on all variants.
lintAyDebug - Runs lint on the AyDebug build.
lintAyRelease - Runs lint on the AyRelease build.
lintFtDebug - Runs lint on the FtDebug build.
lintFtRelease - Runs lint on the FtRelease build.
lintSswlDebug - Runs lint on the SswlDebug build.
lintSswlRelease - Runs lint on the SswlRelease build.
lintVitalAyRelease - Runs lint on just the fatal issues in the ayRelease build.
lintVitalFtRelease - Runs lint on just the fatal issues in the ftRelease build.
lintVitalSswlRelease - Runs lint on just the fatal issues in the sswlRelease build.
test - Run unit tests for all variants.
testAyDebugUnitTest - Run unit tests for the ayDebug build.
testAyReleaseUnitTest - Run unit tests for the ayRelease build.
testFtDebugUnitTest - Run unit tests for the ftDebug build.
testFtReleaseUnitTest - Run unit tests for the ftRelease build.
testSswlDebugUnitTest - Run unit tests for the sswlDebug build.
testSswlReleaseUnitTest - Run unit tests for the sswlRelease build.

发布了36 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43278826/article/details/100552252
今日推荐