About the author: CSDN content partner and technical expert, built an APP with tens of millions of daily users from scratch.
Focus on sharing original series of articles in various fields, specializing in java backend, mobile development, business realization, artificial intelligence, etc. I hope everyone will support me.
Table of contents
1. Introduction
We continue to summarize and learn, review the past and learn the new.
2. Overview
This article describes how to view android dependency library relationships.
3. View dependencies
3.1 Method 1
Tasks->help->dependencies
You can see the dependent libraries in the project and their import relationships
3.2 Method 2
Tasks->android->androidDependencies
You can see the dependent libraries in the project and their import relationships
The above two methods will output the following log:
_internal-unified-test-platform-android-device-provider-gradle - A configuration to resolve the Unified Test Platform dependencies.
\--- com.android.tools.utp:android-device-provider-gradle:30.2.0
+--- com.android.tools:common:30.2.0
| +--- com.android.tools:annotations:30.2.0
| +--- com.google.guava:guava:30.1-jre
| | +--- com.google.guava:failureaccess:1.0.1
| | +--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
| | +--- com.google.code.findbugs:jsr305:3.0.2
| | +--- org.checkerframework:checker-qual:3.5.0
| | +--- com.google.errorprone:error_prone_annotations:2.3.4
| | \--- com.google.j2objc:j2objc-annotations:1.3
| +--- net.java.dev.jna:jna-platform:5.6.0
| | \--- net.java.dev.jna:jna:5.6.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.31
| +--- org.jetbrains.kotlin:kotlin-stdlib:1.5.31
| | +--- org.jetbrains:annotations:13.0
| | \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.5.31
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.31
| \--- org.jetbrains.kotlin:kotlin-stdlib:1.5.31 (*)
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.31 (*)
+--- com.google.protobuf:protobuf-java:3.10.0
+--- com.android.tools.utp:android-device-provider-gradle-proto:30.2.0
| \--- com.google.protobuf:protobuf-java:3.10.0
\--- com.google.testing.platform:android-device-provider-local:0.0.8-alpha07
3.3 Method 3
./gradlew :app:dependencies >log.txt
Save the input to the log through the command line for easier viewing.
4. Remove duplicate dependencies
Many third-party libraries are used in the project, and it is easy to introduce libraries with different versions of the same name. In this case, we need to use the exclude statement to remove the specified dependent library in the gradle file.
implementation("com.kuaishou.koom:koom-java-leak:2.1.0") {
exclude group: "com.google.android.material"
}
compileOnly ("com.xx:yy:$er_version") {
exclude group:"com.xx", module: "common-x"
}
After we remove it, we need to check whether the code is effective.
You can find the External Libraries directory in the project view mode of android studio. Check in this directory to see if redundant dependent libraries have been removed.
As shown below:
Force the entire project to depend on a fixed version of a third-party library
Of course, the above writing method may be more troublesome, because each related third-party library must be written, so we can force the entire project to rely on a fixed version of the third-party library to avoid repeatedly relying on different versions of the same library.
The method is as follows: under the project build file
configurations.all {
resolutionStrategy {
force "com.kuaishou.koom:koom-java-leak:2.2.2"
}
}
In addition, unused files can be prevented from being packaged directly through obfuscation.
5. Find components that are no longer used in Gradle dependencies (useless libraries)
With the continuous iteration of the Android project, the gradle configuration becomes more and more bloated. There may be many public libraries in dependencies that are no longer used, which may slow down the compilation speed in severe cases.
Recommend a tool to everyone that can help us find these components that are no longer used
Tool library github
Usage 1
root build.gradle
plugins {
id 'com.autonomousapps.dependency-analysis' version "${latest_version}"
}
settings.gradle
pluginManagement {
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/" }
gradlePluginPortal()
}
}
Usage 2
root build.gradle
buildscript {
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
classpath "com.autonomousapps:dependency-analysis-gradle-plugin:${latest_version}"
}
}
apply plugin: "com.autonomousapps.dependency-analysis"
After adding dependencies, synchronize and the following tasks will appear:
use
Executing buildHealth or other subtasks helps us generate inspection results of gradle configuration from different dimensions, including but not limited to:
- Discover all dependent components, including direct dependencies and transitive dependencies
- Discover directly dependent components that are not used
- Discover all binary dependencies (ABI)
- Optimization suggestions for gradle, such as modification suggestions for dependency methods, etc.
Assuming we click on the buildHealth task, there will be result output in the command line
> Task :buildHealth
There were dependency violations. See report at E:\jnitest1\build\reports\dependency-analysis\build-health-report.txt
内容如下:
Advice for :app
Unused dependencies which should be removed:
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.code.gson:gson:2.8.9'
These transitive dependencies should be declared directly:
androidTestImplementation 'androidx.test:monitor:1.4.0'
Existing dependencies which should be modified to be as indicated:
androidTestImplementation 'junit:junit:4.12' (was testImplementation)
At the same time, under the build directory of each module, there will also be a reports directory
jnitest1\app\build\reports
as shown below: a>
Principle:
If you want to find components without dependencies, you naturally need to perform a static scan on the code.
The plug-in tool uses ANTLR to analyze the compiled bytecode: collect the classes used in the project and the classes actually provided by the components, and through comparison, you can find components that are no longer used (without classes) Used by the project)
https://dev.to/autonomousapps/dependency-analysis-gradle-plugin-using-bytecode-analysis-to-find-unused-dependencies-509n
ReferenceHow to find components that are no longer used in Gradle dependencies
You can also use lint to check,
by selecting code > Analyze Code > Run Inspection By Name
and then enter in the dialog box unused library
6. Recommended reading
Data Structures and Algorithms
Without permission, shall not be reproduced