Method and system for merging Android nested dependency to AAR file based on Gradle

Through static code analysis and Gradle dependency analysis, Android nested dependency is merged to generate flat AAR files, which solves the problem of APK volume redundancy and improves compilation speed and dependency management efficiency.

CN120447956APending Publication Date: 2025-08-08INSPUR SOFTWARE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510498449.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-21
Publication Date
2025-08-08

AI Technical Summary

Technical Problem

Nested dependencies in existing Android projects require manual analysis, which makes it difficult to optimize APK volume redundancy.

Method used

Through static code analysis and Gradle dependency graph analysis, nested dependencies are dynamically identified, multi-level transitive dependencies are merged into a flat structure, a single AAR file containing a complete dependency chain is generated, compatible versions are filtered using BFS algorithm and uninvoked dependencies are filtered using ASM bytecode analysis to prevent class conflicts.

Benefits of technology

Optimizes the compilation and construction speed of Android applications, simplifies dependency management, and reduces APK volume redundancy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120447956A_ABST
    Figure CN120447956A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of mobile applications, in particular to a method and system for merging Android nested dependencies to an AAR file based on Gradle, which dynamically identifies nested dependencies of all modules in an Android project through static code analysis and Gradle dependency graph analysis, automatically merges multi-level transitive dependencies into a flat structure, and improves the reliability of the AAR file. A single AAR file containing a complete dependency chain is generated, and the problem of APK volume redundancy caused by nesting dependency is solved; the method has the beneficial effects that traversal is started from a root project, all Projects are found out, the Project is traversed, the dependency condition of the current Project is analyzed, the Project Name and the Project Dedenense List of the Project are collected, and the unwanted dependency is removed on the basis of the most complete dependency principle. The most complete dependency principle specifically refers to listing all dependency links of the current project and removing contained dependency paths.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of mobile application technology, and in particular to a method and system for merging Android nested dependencies into AAR files based on Gradle. Background Art

[0002] Current Android project code is mostly component-based, encompassing numerous basic capability libraries and business libraries. Dependencies between these libraries, and between them and shell projects, are often manually analyzed and sorted out. To determine the number of times a specific library is referenced or to query other dependent libraries, manual analysis is required to search through all Gradle files. Summary of the Invention

[0003] The purpose of the present invention is to provide a method and system for merging Android nested dependencies into AAR files based on Gradle, so as to solve the problems raised in the above background technology.

[0004] To achieve the above objectives, the present invention provides the following technical solutions: a method for merging Android nested dependencies into AAR files based on Gradle, which dynamically identifies the nested dependencies of all modules in the Android project through static code analysis and Gradle dependency graph parsing, automatically merges multi-level transitive dependencies into a flat structure, and generates a single AAR file containing the complete dependency chain, thereby solving the APK volume redundancy problem caused by nested dependencies.

[0005] Preferably, it includes an automated execution process: automatically triggering the dependency analysis task in the afterEvaluate phase of the Gradle build lifecycle, listening to dependency resolution events through DependencyResolutionListener, capturing dependency call links between modules in real time, and optimizing the recognition efficiency of duplicate dependencies based on the LRU cache mechanism.

[0006] Preferably, the merge strategy includes: using the BFS algorithm to traverse the dependency tree and filter the highest compatible version according to the semantic version control specification; marking the provided scope dependencies as lazy loading to avoid non-runtime dependencies from being packaged; and filtering empty dependency declarations that are not actually called through ASM bytecode analysis.

[0007] Preferably, the generated AAR file has the following features: using jarjar rules to isolate the namespace of the merged dependency packages to prevent class conflicts; injecting the dependency mapping table into META-INF to support dynamic loading at runtime; generating a dependency reduction report through ProGuard configuration to show the method number / volume comparison before and after the merger.

[0008] The preferred integration method includes: providing @MergeDependency annotation to mark modules that need to be merged, supporting configuration of the mergeStrategy parameter in build.gradle to select aggressive merge or conservative merge mode, and triggering an independent analysis task through the . / gradlewgenerateMergedAar command. The generated AAR file is automatically injected into the flatDir repository of the main project to achieve incremental updates.

[0009] A system for merging Android nested dependencies into AAR files based on Gradle includes a dependency identification module. This module dynamically identifies the nested dependencies of all modules in an Android project through static code analysis and Gradle dependency graph parsing, automatically merges multi-level transitive dependencies into a flat structure, and generates a single AAR file containing the complete dependency chain, thus solving the problem of APK size redundancy caused by nested dependencies.

[0010] Preferably, it also includes an automated execution module, which automatically triggers the dependency analysis task in the afterEvaluate phase of the Gradle build lifecycle, listens to dependency resolution events through DependencyResolutionListener, captures dependency call links between modules in real time, and optimizes the recognition efficiency of duplicate dependencies based on the LRU cache mechanism.

[0011] Preferably, it also includes a merge strategy module, which uses the BFS algorithm to traverse the dependency tree and filter the highest compatible version according to the semantic version control specification; marks the provided scope dependencies as lazy loading to avoid non-runtime dependencies from being packaged; and filters empty dependency declarations that are not actually called through ASM bytecode analysis.

[0012] Preferably, it also includes an AAR file generation and feature processing module. The AAR file generated by this module uses the jarjar rule to namespace isolate the merged dependency packages to prevent class conflicts; injects the dependency mapping table into META-INF to support dynamic loading at runtime; and generates a dependency reduction report through ProGuard configuration to show the method number / volume comparison before and after the merger.

[0013] Preferably, an integration module is also included, which provides the @MergeDependency annotation to mark the modules that need to be merged, supports configuring the mergeStrategy parameter in build.gradle to select aggressive merge or conservative merge mode, and triggers an independent analysis task through the . / gradlewgenerateMergedAar command. The generated AAR file is automatically injected into the flatDir repository of the main project to achieve incremental updates.

[0014] Compared with the prior art, the present invention has the following beneficial effects:

[0015] The present invention proposes a Gradle-based method and system for merging Android nested dependencies into AAR files. The method and system traverse from the root project to identify all projects, analyze the current project's dependencies by traversing the projects, collect the projectName and projectDepedencyList of the project, and remove unnecessary dependencies based on the principle of the most complete dependency. The most complete dependency principle specifically lists all dependency links of the current project and removes the included dependency paths. Based on Gradle's dynamic querying of the relationships between all dependent libraries, users can better manage the various dependent libraries, optimize componentization solutions, and accelerate the compilation and construction of Android applications. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] Figure 1 Flow chart of the method of the present invention. DETAILED DESCRIPTION

[0017] In order to clearly and completely describe the objectives and technical solutions of the present invention and make the advantages more clearly understood, the embodiments of the present invention are further described in detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are part of the embodiments of the present invention, not all of them, and are only used to explain the embodiments of the present invention, not to limit the embodiments of the present invention. All other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.

[0018] For example 1, please refer to Figure 1 The present invention provides a technical solution: a method for merging Android nested dependencies into AAR files based on Gradle. Through static code analysis and Gradle dependency graph parsing, the nested dependency relationships of all modules in the Android project are dynamically identified, and multi-level transitive dependencies are automatically merged into a flat structure to generate a single AAR file containing the complete dependency chain, thereby solving the APK volume redundancy problem caused by nested dependencies.

[0019] It includes automated execution processes: automatically triggering dependency analysis tasks in the afterEvaluate phase of the Gradle build lifecycle, listening to dependency resolution events through DependencyResolutionListener, capturing dependency call links between modules in real time, and optimizing the recognition efficiency of duplicate dependencies based on the LRU cache mechanism.

[0020] This includes merging strategies: using the BFS algorithm to traverse the dependency tree and filter the highest compatible version according to the semantic versioning specification; marking provided scope dependencies for lazy loading to prevent non-runtime dependencies from being packaged; and filtering empty dependency declarations that are not actually called through ASM bytecode analysis.

[0021] The generated AAR file has the following features: it uses jarjar rules to isolate the namespace of the merged dependency packages to prevent class conflicts; injects a dependency mapping table into META-INF to support dynamic loading at runtime; and generates a dependency reduction report through ProGuard configuration, showing the method count and size comparison before and after the merge.

[0022] Integration methods include: providing the @MergeDependency annotation to mark modules that need to be merged, supporting the configuration of the mergeStrategy parameter in build.gradle to select aggressive or conservative merge mode, and triggering an independent analysis task through the . / gradlewgenerateMergedAar command. The generated AAR file is automatically injected into the flatDir repository of the main project to achieve incremental updates.

[0023] Example 2, based on Example 1, proposes a system for merging Android nested dependencies into AAR files based on Gradle, including a dependency identification module. This module dynamically identifies the nested dependencies of all modules in the Android project through static code analysis and Gradle dependency graph parsing, automatically merges multi-level transitive dependencies into a flat structure, and generates a single AAR file containing a complete dependency chain, thereby solving the APK volume redundancy problem caused by nested dependencies.

[0024] It also includes an automated execution module, which automatically triggers the dependency analysis task in the afterEvaluate phase of the Gradle build lifecycle, listens to dependency resolution events through DependencyResolutionListener, captures dependency call links between modules in real time, and optimizes the recognition efficiency of duplicate dependencies based on the LRU cache mechanism.

[0025] It also includes a merge strategy module, which uses the BFS algorithm to traverse the dependency tree and filter the highest compatible version according to the semantic version control specification; marks the provided scope dependencies as lazy loading to prevent non-runtime dependencies from being packaged; and filters empty dependency declarations that are not actually called through ASM bytecode analysis.

[0026] It also includes an AAR file generation and feature processing module. The AAR file generated by this module uses the jarjar rule to namespace isolate the merged dependency packages to prevent class conflicts; injects a dependency mapping table into META-INF to support dynamic loading at runtime; and generates a dependency reduction report through ProGuard configuration, showing the comparison of the number and size of methods before and after the merger.

[0027] It also includes an integration module, which provides the @MergeDependency annotation to mark modules that need to be merged, supports configuring the mergeStrategy parameter in build.gradle to select aggressive merge or conservative merge mode, and triggers an independent analysis task through the . / gradlewgenerateMergedAar command. The generated AAR file is automatically injected into the flatDir repository of the main project to achieve incremental updates.

[0028] While embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions, and variations may be made to these embodiments without departing from the principles and spirit of the invention, and that the scope of the invention is defined by the appended claims and their equivalents.

Claims

1. A method for merging Android nested dependencies into AAR files based on Gradle, characterized by: Through static code analysis and Gradle dependency graph parsing, it dynamically identifies the nested dependencies of all modules in the Android project, automatically merges multi-level transitive dependencies into a flat structure, and generates a single AAR file containing the complete dependency chain, solving the APK size redundancy problem caused by nested dependencies.

2. The method for merging Android nested dependencies into an AAR file based on Gradle according to claim 1, characterized in that: It includes automated execution processes: automatically triggering dependency analysis tasks in the afterEvaluate phase of the Gradle build lifecycle, listening to dependency resolution events through DependencyResolutionListener, capturing dependency call links between modules in real time, and optimizing the recognition efficiency of duplicate dependencies based on the LRU cache mechanism.

3. The method for merging Android nested dependencies into an AAR file based on Gradle according to claim 2, characterized in that: Including merging strategy: using BFS algorithm to traverse the dependency tree and filter the highest compatible version according to the semantic version control specification; Mark provided scope dependencies as lazy loaded to prevent non-runtime dependencies from being packaged; filter empty dependency declarations that are not actually called through ASM bytecode analysis.

4. The method for merging Android nested dependencies into an AAR file based on Gradle according to claim 3, characterized in that: The generated AAR file has the following characteristics: Use jarjar rules to isolate the namespace of the merged dependency packages to prevent class conflicts; inject the dependency mapping table into META-INF to support dynamic loading at runtime; generate a dependency reduction report through ProGuard configuration to show the comparison of the number and size of methods before and after the merger.

5. The method for merging Android nested dependencies into an AAR file based on Gradle according to claim 4, characterized in that: Integration methods include: providing the @MergeDependency annotation to mark modules that need to be merged, supporting the configuration of the mergeStrategy parameter in build.gradle to select aggressive or conservative merge mode, and triggering an independent analysis task through the . / gradlewgenerateMergedAar command. The generated AAR file is automatically injected into the flatDir repository of the main project to achieve incremental updates.

6. A system for the method of merging Android nested dependencies into AAR files based on Gradle according to claim 5, characterized in that: It includes a dependency identification module, which dynamically identifies the nested dependencies of all modules in the Android project through static code analysis and Gradle dependency graph parsing, automatically merges multi-level transitive dependencies into a flat structure, and generates a single AAR file containing the complete dependency chain, thus solving the APK volume redundancy problem caused by nested dependencies.

7. A system according to claim 6, characterized in that: It also includes an automated execution module, which automatically triggers the dependency analysis task in the afterEvaluate phase of the Gradle build lifecycle, listens to dependency resolution events through DependencyResolutionListener, captures dependency call links between modules in real time, and optimizes the recognition efficiency of duplicate dependencies based on the LRU cache mechanism.

8. A system according to claim 6, characterized in that: It also includes a merge strategy module, which uses the BFS algorithm to traverse the dependency tree and select the highest compatible version according to the semantic version control specification; Mark provided scope dependencies as lazy loaded to prevent non-runtime dependencies from being packaged; filter empty dependency declarations that are not actually called through ASM bytecode analysis.

9. A system according to claim 6, characterized in that: It also includes an AAR file generation and feature processing module. The AAR file generated by this module uses the jarjar rule to namespace isolate the merged dependency packages to prevent class conflicts; injects a dependency mapping table into META-INF to support dynamic loading at runtime; and generates a dependency reduction report through ProGuard configuration, showing the comparison of the number and size of methods before and after the merger.

10. A system according to claim 6, characterized in that: It also includes an integration module, which provides the @MergeDependency annotation to mark modules that need to be merged, supports configuring the mergeStrategy parameter in build.gradle to select aggressive merge or conservative merge mode, and triggers an independent analysis task through the . / gradlewgenerateMergedAar command. The generated AAR file is automatically injected into the flatDir repository of the main project to achieve incremental updates.