Automatic Android Deprecated-API Usage Update by Learning from Single Updated Example

Stefanus A. Haryono, Ferdian Thung, Hong Jin Kang, Lucas Serrano, Gilles Muller, Julia Lawall, David Lo, Lingxiao Jiang · DOI

TL;DR

This paper introduces CocciEvolve, a novel approach to automatically update Android applications when their APIs become deprecated. Unlike previous methods requiring multiple examples, CocciEvolve learns to fix these deprecations using just a single "after-update" code snippet, significantly simplifying the process for developers.

What Problem Does This Paper Address?

The Android operating system is constantly evolving, leading to the deprecation of APIs. When an API is deprecated, developers must update their applications to use the new, recommended APIs. This is crucial for maintaining compatibility with current and future Android versions and for ensuring apps don't break. However, manually updating these APIs can be a tedious and time-consuming task, especially since a deprecated API might be used in numerous places within an app. Existing automated tools, like AppEvolve, often require developers to provide multiple before-and-after code examples to learn from, which can be a significant hurdle. This paper aims to address the challenge of automating these updates more efficiently by reducing the dependency on extensive example data.

Methods

CocciEvolve tackles the problem through a three-component pipeline:

  1. Source File Normalization: This is a key innovation. Developers often write semantically equivalent code in syntactically different ways. CocciEvolve normalizes both the example code and the target code to be updated. This involves extracting API calls, their arguments, and the objects they are invoked on into temporary variables. This step ensures that the learning process isn't hindered by minor stylistic differences in code.
  2. Updated API Block Extractor: This component identifies the relevant code block containing the deprecated API usage and its replacement. It specifically looks for blocks that include an Android version check (e.g., if (Build.VERSION.SDK_INT >= ...)), which is characteristic of how developers handle API deprecations to maintain backward compatibility.
  3. API Update Semantic Patch Creation: Using the normalized code block from the previous step, CocciEvolve generates a "semantic patch" using the Semantic Patch Language (SmPL). This patch describes the transformation needed to replace the deprecated API with the new one, including the necessary version checks. The use of SmPL makes the generated patches readable and understandable to developers.

CocciEvolve then applies this generated semantic patch to the normalized target file to perform the update.

Datasets

The authors evaluated CocciEvolve using a dataset of 112 target source files collected from public GitHub repositories. These files were chosen because they used 10 commonly deprecated Android APIs that were also used in the original evaluation of the AppEvolve tool. The "after-update" examples used to train CocciEvolve were sourced from the AppEvolve replication package, with only a single example used per API.

Key Insights and Ideas

  • The Power of a Single Example: The most significant insight is that learning to update deprecated APIs can be effectively achieved with just one "after-update" example. This drastically lowers the barrier to entry for automated API update tools.
  • Normalization is Crucial: Code normalization is presented as a critical step to overcome the challenge of syntactic variations in semantically equivalent code. This allows the tool to generalize better and avoid failures due to minor code style differences.
  • Semantic Patches for Readability: Using a semantic patch language like SmPL makes the generated transformations transparent and understandable to human developers, which is a significant advantage for adoption and debugging.

Key Results

CocciEvolve demonstrated strong performance in its experiments:

  • It successfully updated 96 out of 112 target files, achieving an 85% success rate.
  • This is a significant improvement over AppEvolve, which, according to the paper's evaluation on a comparable dataset, could only update 20 target files.
  • CocciEvolve's failures were primarily concentrated on two APIs: getAllNetworkInfo() and requestAudioFocus(...), which the authors note might require more sophisticated data-flow analysis than currently implemented.

Limitations and Future Work

The authors acknowledge several limitations and suggest future directions:

  • Limited Data-Flow Analysis: CocciEvolve currently lacks sophisticated data-flow analysis, which can be necessary for handling updates where new objects for replacement APIs are created outside the immediate code block.
  • Code Slicing: Future work could involve performing code slicing to identify more relevant code for API updates, potentially extending beyond method boundaries.
  • Code Denormalization: The authors plan to explore code denormalization to restore the original coding style after updates, making the changes less jarring for developers.

Summary updated

BibTeX
@inproceedings{haryono2020automatic,
  author = {Stefanus A. Haryono and Ferdian Thung and Hong Jin Kang and Lucas Serrano and Gilles Muller and Julia Lawall and David Lo and Lingxiao Jiang},
  title = {{Automatic Android Deprecated-API Usage Update by Learning from Single Updated Example}},
  booktitle = {ICPC},
  year = {2020},
  doi = {10.1145/3387904.3389285},
}
Figure