An Observational Study On The State of REST API Uses in Android Mobile Applications
An Observational Study On The State of REST API Uses in Android Mobile Applications
net/publication/335499796
CITATIONS                                                                                                 READS
24                                                                                                        803
6 authors, including:
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Yann-Gaël Guéhéneuc on 24 February 2021.
   Abstract—REST is by far the most commonly-used style for                business rules, and business processes of vital information-
designing APIs, especially for mobile platforms. Indeed, REST              systems, which are remote services for architectural, efficiency,
APIs are well suited for providing content to apps running on              and security reasons.
small devices, like smart-phones and tablets. Several research
works studied REST APIs development practices for mobile apps.                REST (Representational State Transfer) APIs (Application
However, little is known about how Android apps use/consume                Programming Interfaces) have been a mainstream form of
these APIs in practice through HTTP client libraries. Conse-               services for some years now. They have been well suited for
quently, we propose an observational study on the state of the             providing content to mobile devices, like smart-phones and
practice of REST APIs use in Android mobile apps. We (1)                   tablets, because they offer a lightweight and flexible imple-
build a catalogue of Android REST mobile clients practices; (2)
define each of these practices through a number of heuristics              mentation for end users and mobile apps developers. Several
based on their potential implementations in Android apps, and              research works studied best practices in the implementation
(3) propose an automatic approach to detect these practices. We            of REST APIs [1], [2], [3], [4]. However, little is known on
analyze 1,595 REST mobile apps downloaded from the Google                  how Android apps use/consume these APIs in practice through
Play Store and mine thousands of StackOverflow posts to study              dedicated HTTP client libraries.
REST APIs uses in Android apps. We observe that developers
have always used HttpURLConnection class for REST APIs
                                                                              These libraries were first implemented for mobile apps
implementation in Android apps. However, since the apparition of           with “low-level” APIs provided by programming languages
REST third-party libraries such as Okhttp, Retrofit and Google             (sockets) or class libraries (HttpURLConnection). They can be
Volley, Android REST clients have been increasingly relying on             implemented now using dedicated, third-party libraries, such
the facilities offered by these libraries. Also, we observe that           as Google Volley or Retrofit. We ask in this paper how such
developers used to ignore some good practices of REST APIs
uses in Android apps. Such practices are the use of HTTP
                                                                           dedicated HTTP client libraries are being used in practice by
third-party libraries, caching responses, timeout management,              developers. Such knowledge is important for service providers
and error handling. Moreover, we report that only two good                 because (1) mobile apps run on mobile devices that have many
practices are widely considered by Android developers when                 constraints in terms of memory, battery, computational power,
implementing their mobile apps. These practices are network                and competition for resources, (2) it would help them to offer
connectivity awareness and JSON vs. XML response parsing.
We also find that Retrofit is the most targeted third-party HTTP
                                                                           more features that ease the Android developers’ work, and (3)
client library by Android developers because of its ease of use and        it would help them improve the usability and performance of
provided features. Thus, we conclude that service providers must           REST client libraries [5].
strive to make their libraries as simple as possible while mobile-            Consequently, we propose an observational study of the
service consumers should consider existing libraries to benefit            state of the practice of the use of REST APIs by Android
from their features, such as asynchronous requests, awareness to
                                                                           apps. We answer the following research questions:
connectivity, timeout management, and cached responses.
                                                                              • RQ1: What is the state of the practices in the use of
                                                                                REST APIs by Android apps? We want to observe the
                          I. I NTRODUCTION
                                                                                use of practices by developers and their prevalence.
   The global market has experienced a tremendous increase                    • RQ2: What is the state of the implementation of HTTP
in the number of mobile users over the past decades. In 2018,                   client libraries in Android REST clients? We want to
Google PlayStore boasted close to 2.2 millions of mobile                        observe the implementations used by developers for
applications (apps) while the Apple AppStore has over 2                         REST APIs, which will inform the choice of HTTP client
millions1 . Most of these mobile apps access remote data,                       libraries.
                                                                              By answering these research questions, we want to recom-
  1 https://www.statista.com/markets/424/topic/538/mobile-internet-apps    mend to developers the libraries and practices to adopt based
on their prevalence in implementations and their benefits for      proposed SODA-R (Service Oriented Detection for Anti-
developers.                                                        patterns in REST) [4], a heuristics-based approach to detect
   Therefore, we provide four contributions. We review the lit-    (anti)patterns in REST systems. They relied on heuristics
erature extensively and compile a catalogue of seven practices     and detection rules for eight REST anti-patterns and five
related to the development of mobile apps using REST APIs.         patterns. They applied their tool on a set of 12 widely-
These practices pertain to the use of dedicated, third-party       used REST APIs including BestBuy, DropBox, and Facebook.
libraries and help to understand how Android apps use/con-         Then, Palma et al. proposed a syntactic and semantic approach
sume REST APIs. Then, we propose a framework, PIRAC,               to detect REST linguistic (anti-)patterns, which they define as
that automatically detects occurrences of the practices using      poor/good practices in the naming, documentation, and choice
detection rules. Then, we conduct an observational study on        of identifiers in REST APIs [7]. Finally, Palma et al. proposed
over 1,595 REST mobile apps out of 9,173 apps downloaded           UniDoSA [6], a unified approach that (1) embeds a unified
from the Google PlayStore to report how they use/consume           meta-model for the three main service technologies REST,
REST APIs. Finally, we mine 12,478 StackOverflow posts to          SCA, and SOAP and (2) detects the presence of anti-patterns
assess the importance of the identified practices from the point   in service-based systems.
of view of Android developers.                                        A set of automatic approaches for the detection of REST
   The remainder of this paper is as follows. Section II           (anti)patterns are proposed in these works. However, they
describes related works. Section III details the design of our     specifically evaluated APIs without considering any interaction
study. Section IV reports our observations. Section V answers      with clients, in particular mobile clients, as we do here. Other
our research questions based on our observations while Section     works proposed similar (anti)patterns detection approaches in
VI describes the threats to validity of our observational study.   service applications. They implemented other techniques, such
Finally, Section VII concludes with future work.                   as bi-level optimization problems [8] or ontologies [9].
                                                                      In contrast, in this paper, we consider and automatically
                     II. R ELATED W ORK                            detect practices related to the development of REST mobile
   Several research works have been proposed in the literature     clients. We take also into account the interactions among
on bad and good practices in REST APIs. However, few are           clients and REST APIs, not on the service side but on the
the works that study how Android apps use/consume REST             client one. We study in details the use of REST APIs libraries
APIs.                                                              for mobile clients and how Android mobile apps use/consume
   In the context of mobile apps, Rodriguez et al. [1] were        REST APIs.
the first to study the traffic of HTTP requests from mobile
clients. They evaluated the conformance of some state-of-the-                                III. S TUDY D ESIGN
art design best practices of REST APIs from the perspective           This section presents the design of our study, which aims
of mobile clients. They analyzed these practices on a large        to answer our research questions. Answering these questions
data-set of 78 GB of HTTP requests collected from a mobile-        led us to conduct an observational study on hundreds of
Internet traffic-monitoring site. However, the best practices      Android apps from the Google PlayStore. First, we reviewed
analyzed are common to any kinds of REST APIs and they             the literature and developers’ forums to build a catalogue
focused specifically on HTTP requests.                             of Android REST clients practices. Second, we developed
   Oumaziz et al. [5] conducted an empirical study on 500          a tool, PIRAC2 , to detect each of the identified practices.
popular Android apps and 15 popular services to identify           Third, we validated the detection precision of PIRAC on 80
best practices when using/consuming REST APIs for Android          Android apps collected from the F-Droid repository. Fourth,
mobile clients. They showed that Android clients generally         we applied PIRAC on 9,173 mobile apps downloaded from
favour invoking REST APIs by using official dedicated service      Google PlayStore to study the state of the practices in Android
libraries instead of invoking services with a generic HTTP         clients. Finally, we conducted an observational study on Stack-
client like HttpURLConnection. They also presented which           Overflow to assess the importance of the identified practices
good practices service libraries should be implemented fol-        and the use of Android HTTP libraries from the developers’
lowing an online survey and manual analyses of the apps. In        point of view. We describe here the first three steps and the
this paper, (1) we go more in details to identify how dedicated    last one in the next section.
service libraries are used by Android clients. (2) We propose
a tool to automate the detection of these practices in Android     A. Step 1: Cataloguing Android REST Mobile Clients Prac-
mobile apps. (3) We empirically analyze more than 9,000            tices
Android mobile clients to study the usage of REST APIs in            To answer our first research question, we performed a
Android mobile apps.                                               domain analysis of development practices related to Android
   Several works were carried out for the detection of service     REST clients by studying their definitions and specifications
anti-patterns [1], [2], [3], [4], [6], [7]. For example, in the    in the literature as well as in online resources and articles.
context of REST APIs implementation, Palma et al. evaluated        This domain analysis allowed us to identify seven practices
the design of several REST APIs and proposed different
approaches to detect automatically REST (anti)patterns. They         2 http://git.sofa.uqam.ca/mabdellatif/pirac/tree/master
that we classified into two sets: (1) good and bad practices for           retry the requests in some chosen time-intervals until
Android REST clients and (2) other practices that are neither              successfully, etc.
good nor bad practices.
                                                                    List of Other Practices for Android REST Clients
                                                                      1) Synchronous vs. Asynchronous requests: REST APIs
List of Good and Bad Practices for Android REST Clients                  requests can be synchronous or asynchronous. For syn-
  1) 3 Use of third-party HTTP client vs. 7 HttpURL-                     chronous requests, the code execution will block until
     Connection: This practice concerns the use of third-                the API call returns. For asynchronous requests, calls to
     party libraries to manage REST requests. It is recom-               remote APIs are made while the execution continues.
     mended that mobile HTTP queries should be encapsu-                  Android developers should carefully choose whether to
     lated in a method proposed by the interface of official             invoke REST APIs synchronously or asynchronously
     third-party libraries, such as OkHttp, Retrofit, Google             based on their needs to increase the responsiveness of
     Volley, etc. A Non-encapsulated HTTP Query must be                  their apps.
     manually built by the developer with all the needed pa-        B. Step 2: Detection of the Practices
     rameters using HttpURLConnection. This process could
                                                                       We developed a framework, PIRAC, to detect the seven
     be long and complicated in some cases and could make
                                                                    identified practices. As depicted in Figure 1, our framework
     the code difficult to maintain.
                                                                    takes as input Android APKs, their corresponding meta-data,
  2) 3 Caching vs. 7 Non caching: Caching is the
                                                                    and a list of HTTP client libraries, which we use to filter
     ability to keep copies of frequently accessed data in
                                                                    the code to analyze. Our tool uses the SOOT framework
     several places along the request–response path. Some
                                                                    [11] to parse the byte-code of mobile apps and extract all
     third-party Android REST libraries offer facilities to
                                                                    the information needed for our analyses, such as classes
     manage response caching, such as removing a single
                                                                    and methods. PIRAC creates models for Android mobile
     cached response, clearing the entire cache, retrieving
                                                                    apps based on the information extracted by SOOT,and those
     the date of a cached response, so that developers can
                                                                    extracted from the reconstructed manifest file. Then, we apply
     accurately decide when an update should be made. It
                                                                    the detection algorithms for the identified practices to detect
     is recommended to cache frequent REST requests to
                                                                    their uses.
     reduce bandwidth usage, network latency, and battery
                                                                       An Android APK contains the compiled source code of
     consumption.
                                                                    the app as well as that of third-party and Android libraries.
  3) 3 Network connectivity aware vs. 7 Unaware
                                                                    Running our analyses on the entire packaged code would
     REST service invocation: This practice pertains to the
                                                                    (1) produce misleading results and (2) affect the execution
     validation of the network connectivity before sending
                                                                    time of our analyses. Thus, we filter the application code
     REST request. It is recommended to check network
                                                                    to differentiate the code of the app currently under analysis
     connectivity (1) to offload heavy REST queries when
                                                                    from the code belonging to Android SDK and third-party
     the device is connected to WiFi, (2) to increase device
                                                                    libraries. We rely on a list of third-party Android libraries,
     battery life, (3) to avoid charges related to limited mobile
                                                                    which contains 1,353 package names of the most used libraries
     data, and (4) to detect network changes and resume
                                                                    identified by Li et al. [12]. This list has not been updated since
     incomplete REST requests.
                                                                    2016 so we updated it by adding 1,176 package names of the
  4) 3 JSON vs. 7 XML: This practice pertains to REST
                                                                    libraries that we manually collected from Android community
     responses parsing by mobile clients. It is recommended
                                                                    Web sites3 .
     to parse REST responses with JSON as it is more
                                                                       After filtering the app code, we construct models of the
     human-readable than XML. Also, JSON is more CPU-
                                                                    APKs that embed all the required information to apply our
     friendly to parse as it is more compact than XML [10].
                                                                    detection heuristics. Afterwards, we identify classes of interest
  5) 3 Timeouts vs. 7 Perpetual requests: This practice
                                                                    that are related to REST APIs services calls. Finally, we
     is related to setting or not timeouts for REST requests.
                                                                    analyze these classes and identify the practices based on our
     There are several types of timeouts: connection timeout,
                                                                    detection rules.
     read timeout, write timeout, etc. If a mobile client fails
                                                                       In the following, we describe some of the detection rules
     to establish a connection to the server within the set
                                                                    that we use in our framework.
     connection timeout, it will consider that the request
                                                                          a) Android REST clients identification: Identifying auto-
     failed. It is recommended to set proper timeouts values
                                                                    matically Android apps that make REST calls can be a very
     to make mobile apps more responsive and user friendly.
                                                                    complicated task, especially with the use of static analyses
  6) 3 Specification vs. 7 Non specification of a behaviour
                                                                    methods. Indeed, we can only rely on some used practices
     for failed requests: The specification of a behaviour
                                                                    in the apps source code to verify whether a given app is
     when REST requests fail is highly recommended to in-
                                                                    potentially using a REST API. We rely primarily on these
     crease usability and responsiveness of the mobile client
                                                                    rules:
     apps. Possible behaviours include to drop the requests
     until some change to the network connectivity or to              3 https://android-arsenal.com/
                                                                                                   Detection rules
                                                                                                     3rd party libs
Cache
                                                                                                      Sync/Async
                                                                                                       requests
                MANIFEST                                                                                Error                                Detected practices
                                                                                                       handling
Json/XML
Figure 1. Detection of REST APIs practices usage in Android apps with PIRAC
   1) The use of Android INTERNET permission. Android                           use of REST API requests. To detect such behaviour, our tool
       apps require Internet permission to access the mobile                    detects the invocations of methods that provide information
       network. This information is explicitly defined in the                   about network connectivity from the ConnectivityManager
       Android manifest file.                                                   class.
   2) The referencing of an HTTP client library. The com-                             e) JSON vs. XML Response Parsing: The responses from
       munication with REST APIs is primarily based on the                      REST APIs come in multiple formats, mainly JSON and XML.
       HTTP protocol. REST apps must use an HTTP library                        The use of JSON is recommended due to its size relatively
       to communicate through this protocol. We rely on a                       to XML, its readability by developers, and its ease of use.
       list of 75 HTTP client libraries collected from a Maven                  To detect the usage of one of these two data formats, our
       repository4 .                                                            framework detects the usage of the most common JSON and
   When we detect these practices in an app, it is automatically                XML libraries as well as their instantiation in a code executing
marked as “Potentially Using a REST API”. For a better                          an HTTP call.
accuracy of the detection of our targeted practices, we ensured                       f) Timeout vs. Perpetual REST Requests: Each one of
that the HTTP client library is executing REST calls and that it                the studied client libraries provide classes or methods to
is not just referenced for other purposes/uselessly (dead code).                configure a timeout for their HTTP requests. To detect a
Indeed, the simple presence of an HTTP client library in the                    timeout configuration, PIRAC detects invocations of these
APK file does not guarantee that this app uses/consumes REST                    methods or instantiation of classes with a timeout value in
APIs. Also, some Android apps may reference an HTTP client                      the constructor.
library to use only some of its classes without executing any                         g) Specification vs. Non-specification of a Behaviour
REST calls.                                                                     Upon Failure: When a request fails, regardless of the rea-
      b) Use of Third-party HTTP Library vs. HttpURLCon-                        son, developers should implement custom logic and show an
nection: When it comes to executing HTTP calls, developers                      adapted message to the end user. To detect this behaviour, in
can rely on the native HttpURLConnection API or choose to                       the case of HttpURLConnection, we detect the retrieval
use an external HTTP library. Our tool detects the usage of                     of HTTP status codes after a request. For external libraries,
HttpURLConnection or an external library by analyzing the                       we detect the usage of specific library error-handling methods
instantiation of objects and calls made with the specific Java                  (e.g., onFailure() or response.isSuccessful()).
methods of each of the libraries.
                                                                                      h) Synchronous vs. Asynchronous Requests: When using
      c) Cached vs. Non-cached Responses: Developers can
                                                                                third-party libraries, it is simple to perform asynchronous
use the caching capabilities offered by the HTTP client
                                                                                requests because these libraries offer specific methods with
libraries or develop their own caching strategy. To detect
                                                                                callbacks. To detect synchronous/asynchronous requests from
response caching, PIRAC detects the use of relevant meth-
                                                                                third-party libraries, we implemented a detection approach
ods and classes provided by the libraries, which allow such
                                                                                specific to each kind of libraries. We rely on the detec-
operations. PIRAC detects also the creation and use of caching
                                                                                tion of third-party REST clients methods dedicated to syn-
folders dedicated to Android apps.
      d) Network-connectivity Aware vs. Unaware REST                            chronous/asynchronous REST requests. The detection of asyn-
Requests: The Android SDK provides a class named                                chronous requests for Java HttpURLConnection is more
ConnectivityManager, which provides information                                 challenging. When using this library, developers must cus-
about the network connectivity of a device (network type,                       tomize and hard-code asynchronous requests, most commonly
availability, etc.). Developers can use this class to adapt their               using the AsyncTask class, which provides methods with
                                                                                callbacks. We analyze the bodies of the methods running in the
  4 https://mvnrepository.com/open-source/http-clients                          background to build a method-invocation call graph. Finally,
we search for a REST call in each of these methods.
C. Step 3: Validating Harissa for the Detection of REST
Mobile Clients Practices
   For our validation, we analysed 1448 Android apps from
F-Droid repository5 . We applied our tool on this dataset.
We manually validated each of our practices in 80 Android
REST clients chosen randomly from the dataset by checking
the source code of each app. We chose 80 apps to reach a
confidence level of 95%. Table I summarises the detection
precision and recall of each targeted practice. The precision
detection for each targeted practice by our tool is satisfactory
as it varies between 81.91% and 100%. The recall of our tool
is also satisfactory as it varies between 80% and 95.33%. We
reached an average detection precision of 93.70% while we
had an average detection recall of 87.66%. These detection                    Figure 2. Distribution of Android apps by category
results confirm the reliability of our tool to detect our targeted
practices.
 Practices                               Precision   Recall
 Use of third-party HTTP Li-             100%        80%
 brary vs. HttpURLConnec-
 tion
 Cache usage                             99.38%      92.85%
 Connectivity aware clients              95.62%      95.33%
 JSON vs. XML                            89.05%      88.69%
 Timeout setting                         90.00%      86.36%
 Specification of a behavior at          81.91%      85.39%
                                                                                    Figure 3. Android REST clients LOCs
 request failure
 Synchronous                vs.          100%        85%
 asynchronous calls
 Average                                 93.70%      87.66%
                                    Table I
         OVERVIEW OF THE DETECTION PRECISION OF OUR TOOL
                              IV. O BSERVATIONS
A. Dataset
   To conduct our observational study, we downloaded ran-
domly 9,173 Android apps from the Google PlayStore. We
used Androzoo6 to collect APKs and meta-data for these
apps. As depicted in Figure 2, the apps belong to varied
categories, such as games, communication, weather, etc. We                  Figure 4. Evolution of the use of HTTP libraries in time
applied PIRAC on the data-set to extract all REST clients for
a total number of 1,595 Android REST clients. These apps are
also of different sizes, as shown in Figure 3.                             a) Use vs. Non-use of Third-party Libraries for HTTP
                                                                     Requests: Based on our previous work [5], the most used An-
    Observation 1: We observe that the three main cat-               droid libraries to execute HTTP requests are OkHttp, Retrofit,
    egories that use the most Android REST clients are               Google Volley, and Java HttpURLConnection. We focused
    Lifestyle, Business& Finance, and Video& Media.                  on these libraries and studied their usage evolution in REST
                                                                     mobile clients. We noticed that Java HttpURLConnection is
                                                                     the oldestlibrary to execute HTTP requests in REST Android
B. Observations about the Identified Practices
                                                                     clients. With Figure 4, we noticed that HttpURLConnection is
   In this section, we present our observations about the            getting less used by developers compared to newer third-party
distribution of good and bad practices of REST APIs in               libraries (i.e., OkHttp, Retrofit, and Google Volley).
Android clients.                                                        In 2014, OkHttp, Retrofit, and Google Volley have been
  5 https://f-droid.org/en/                                          released to ease and simplify the management of HTTP
  6 https://androzoo.uni.lu/                                         requests by Android clients. We observe that between 2014
 80%
70%
60%
50%
40%
30%
20%
10%
 0%
   2012         2013          2014              2015         2016          2017      2018
                                                Year