arXiv:2609.31409cs.CRSep 2026

Manuscript Offprint

arXiv:2609.31409

Context-Aware Functional Modeling for Android Third-Party Library Detection

Dihao Fan, Jian Zhang, Yasai Shi, Chuan Luo, Xudong Liu, Yang Liu, Chunming Hu, Xu Wang

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Dihao Fan*, Jian Zhang*, Yasai Shi*, Chuan Luo*, Xudong Liu*, Yang Liu†, Chunming Hu*, Xu Wang*

*Beihang University, Beijing, China

{fandihao, zhangj_cs, shiyasai, chuanluo, liuxd, hucm, xuwang}@buaa.edu.cn

†Nanyang Technological University, Singapore

yangliu@ntu.edu.sg

Abstract—Third-party libraries (TPLs) are widely used in Android apps, but their reuse can introduce security risks and interfere with downstream program analyses. Existing Android TPL detection approaches face two key limitations: their handcrafted features are fragile under aggressive code transformations, and their whole-library matching strategies are ineffective when apps retain only part of a TPL.

In this paper, we propose LibFan, a learning-based Android TPL detection approach based on context-aware functional modeling. It realizes this modeling through two complementary components: context-aware contrastive learning at the method level and functional partitioning at the library level. At the method level, it learns semantic representations through contrastive training while incorporating outgoing call relationships and class-level context, improving robustness to obfuscation, shrinking, and optimization. At the library level, it partitions each TPL into functionally coherent units and determines library presence using the best-matching partition, thereby accommodating partial library reuse. To evaluate LibFan, we construct a new benchmark comprising 200 apps and 46 vulnerable TPLs, with each app compiled under four transformation configurations. Under the most challenging R8 full mode, LibFan achieves F1 scores of 81.3% at the library level and 47.6% at the version level, representing relative improvements of 64.9% and 35.6% over the state of the art, respectively.

I. INTRODUCTION

The Android operating system hosts one of the largest and most diverse software ecosystems in the world, with over three billion active users across more than 190 countries [1], [2]. A key factor behind Android's success is its open and modular architecture, which allows developers to rapidly build applications by incorporating a wide range of third-party libraries (TPLs) from the upstream software supply chain. Such libraries enable efficient implementation of essential functionalities, including network communication, cloud-based analytics, and crash reporting [3], [4], [5].

However, TPL reuse also introduces security and analysis challenges. First, vulnerabilities in TPLs can propagate into downstream apps. For instance, a known okhttp vulnerability allows man-in-the-middle attackers to bypass certificate pinning [6], and such risks may persist due to complex dependency chains and delayed updates [7], [8]. Second, accurate TPL identification is also important for Android analyses such as malware classification [9], [10], [11], app testing [12], [13], [14], [15], [16], and repackaging detection [17], [18], where TPL code can otherwise introduce substantial noise.

Therefore, many studies focus on detecting TPLs in Android apps. However, accurate detection is challenged by common code transformations applied during app build and release processes, including obfuscation (e.g., identifier renaming and package flattening), shrinking (e.g., removing unused code), and optimization (e.g., method inlining and class merging). These transformations, introduced by tools such as Android's R8 compiler [19], ProGuard [20], DashO [21], and Obfuscapk [22], can substantially alter the syntactic structure of TPL code, making it difficult to determine whether a given TPL is present in an app [23], [24].

Existing work has proposed various approaches to tackle the above challenges [25], [26], [27], [28]. For example, LibPecker [29] extracts class-level features with weighted similarity scoring to tolerate code customization. LibScan [30] adopts a two-stage detection strategy, starting with class-level signature matching followed by method-level static feature comparison; it further enhances method-level matching using call-chain-opcode similarity, achieving strong performance under obfuscation. LibHunter [31] takes a step further by optimizing both class-level and method-level feature design to specifically counter code optimization techniques such as call site optimization and method inlining, thereby improving TPL detection performance in optimization-heavy scenarios.

Despite recent advances, two critical challenges in Android TPL detection remain underexplored:

• Fragility of Hand-crafted Features. Existing approaches largely rely on hand-crafted instruction-level features, using opcodes as the primary signal for method matching, sometimes supplemented by cues such as string literals. Although effective against common obfuscation, such features remain fragile under aggressive transformations, particularly compiler optimizations that rewrite instructions while preserving functionality. This fragility can impair method matching and propagate errors to library-level detection. While prior work attempts to tolerate such variations through heuristics such as incorporating call-chain opcodes, they remain fundamentally tied to low-level instruction patterns and do not learn high-level functional representations that remain stable across transformations.

• Partial TPL Presence in Apps. In practice, apps often reuse only a subset of a TPL's functionality. Our empirical analysis reveals that, after aggressive shrinking and optimization in release builds, only 34.6% of a TPL's classes and 16.7% of its methods remain in the final app on average. Still, most existing tools treat each TPL as a complete unit and aggregate matching evidence across all of its classes or methods. Consequently, even when the retained components are correctly matched, their evidence can be overwhelmed by the large number of unretained components, underestimating the overall app-TPL similarity and ultimately causing missed detections.

In this paper, we propose Library Finder for Android, namely LibFan, a learning-based TPL detection tool based on context-aware functional modeling. LibFan follows a three-stage workflow: class-level candidate filtering, context-aware method matching, and functional partition-based library scoring. It adopts the candidate-filtering strategy from prior work and introduces the latter two stages to model TPL functionality at the method and library levels, respectively.

Given the resulting candidate class pairs, LibFan performs method-level matching using a dual-encoder framework, instead of relying on fragile hand-crafted method features. Each method is independently encoded into a semantic representation using its decompiled Java source code together with two complementary forms of context: (1) a call context summarizing its outgoing calls and (2) a class context providing selectively chosen structural cues relevant to its functionality. The encoders are initialized from a pretrained code language model and fine-tuned using a contrastive objective constructed from transformed and non-transformed method pairs. This objective brings functionally equivalent methods closer in the embedding space while separating non-equivalent ones, enabling more robust matching under instruction-level rewriting introduced by obfuscation and compiler optimization.

At the library level, LibFan introduces a functional partition-based scoring strategy to address partial TPL presence caused by shrinking and optimization. Unlike existing approaches that treat a TPL as a single matching unit, it groups the library's methods into functionally coherent partitions according to their internal call relations. Each partition is initialized from a public entry point and its reachable methods, and overly small partitions are iteratively merged to avoid excessive fragmentation. LibFan then computes a match ratio for each partition independently and uses the highest ratio as the final app-TPL similarity. In this way, a strongly matched retained functionality can determine library presence without being diluted by other library components removed during compilation.

To evaluate LibFan, we construct a new benchmark, LibFanBench, since we identified that existing datasets often use only explicitly declared build-script dependencies as ground truth and overlook transitive dependencies introduced during compilation, leading to incomplete or inaccurate labels. LibFanBench comprises 200 open-source apps and 46 vulnerable TPLs. To assess robustness under increasingly aggressive transformations, each app is compiled into four variants using the D8/R8 toolchain: D8 without transformation, R8 with shrinking, R8 with optimization and shrinking, and R8 full mode with optimization, obfuscation, and shrinking. Ground-truth TPLs are identified from compilation artifacts, including the sdk-dependencies directory, yielding more complete and realistic labels. On this benchmark, LibFan achieves an F1 score of 81.3% at the library level and 47.6% at the version level on the most challenging variant (R8 full mode), representing relative improvements of 64.9% and 35.6% over the state-of-the-art, respectively.

Our contributions are summarized as follows:

  • We propose a context-aware contrastive learning framework for Android TPL detection. By jointly encoding a method's call relationship and class context, the fine-tuned dual-encoder model learns semantic method representations that remain robust under code transformations.

  • We design a functional partition-based similarity strategy to address missed detections caused by partial TPL reuse. Instead of aggregating matches over the entire library, LibFan scores the most relevant functional partition, improving robustness under code shrinking and optimization.

• We construct a more realistic benchmark comprising 200 apps and 46 vulnerable TPLs, accounting for transitive dependencies and four transformation configurations. On this benchmark, LibFan achieves 81.3% library-level and 47.6% version-level F1 scores under the most challenging R8 full mode, substantially outperforming existing approaches.

References (58)