Skip to main content

Top Skills to Master in the Age of AI

AI is finding it's way in  a wide variety of applications pertaining to  almost every industry. This AI driven rapidly evolving landscape has created a demand for a unique blend of technical, creative, and interpersonal skills highly sought-after by employers. Listed below are some specialized AI-related skills that are becoming increasingly valuable in the modern times. 1. AI Models Development Understanding how AI and ML work including the underlying algorithms, and learning to develop ML powered apps using tools like TensorFlow or PyTorch is a highly desirable skill to master in the age of AI. Furthermore, the skills in fine-tuning and adapting large pre-trained models (like GPT, BERT, or Vision Transformers) to specific use cases are also useful, allowing you to create specialized applications without starting from scratch. Leveraging pre-trained models and adapting them to new tasks with limited data is particularly useful in NLP and computer vision. 2. AI Models Deployme...

Guidelines for Energy Efficient Android Application Development

Mobile devices are ubiquitous in our daily lives. Users of mobile devices depend heavily on them for a variety of tasks such as calling, messaging, networking, taking pictures and making payments. Software applications running on mobile devices perform helpful tasks after processing the data from multiple embedded sensors and displaying relevant information to the user. As a software developer, it is very likely that you will come across mobile app development, at least once in your career. Mobile app development has some challenges in terms of usability, performance and privacy due to limited available resources and holding sensitive user information. 

Guidelines for Energy Efficient Android Application Development

Top 10 Mobile Battery Draining Apps 

Credit: https://techengage.com

One of the worst limitations of mobile devices is limited battery power. The embedded hardware such as sensors, CPU, display and network usage drains the battery, which limits the use of various innovative apps. Although, technological progress in battery and mobile hardware has improved the battery consumption, still a poorly designed mobile app can drain a battery faster. Software developers can utilize some of the best practices to optimize for battery performance. In this blog, I will enlist some recommended practices for Android app development, which have been proven to be effective in reducing the battery consumption:


Arkenlight Gammavoltaic Battery Prototype

Credit: Arkenlight

Avoid Reference to Array Length in Loops. A reference to the array length is expensive in Android. Avoiding references to the array length in each iteration of a loop can save energy. Initialize a variable with the length once and then use that as a constant to reduce the energy consumption.


Standard Vs Recommended Loop Structure

Avoid Pixel Overdraw. Overdraw refers to redrawing of a pixel in single rendering frame due to complex UI views described using several nested layouts in Android XML file. If each of the layouts in hierarchy has a background color, same pixel has to be drawn several times on each refresh. Although the advancements in GPU performance and OS optimizations have mitigated the overall effect of pixel overdraw, developers may optimize apps to avoid overdraw and reduce battery usage. A simple solution is to remove background color property from views being overdrawn.

Example of Android Hierarchical Views

Access Object Fields Directly. 'Getter' and 'Setter' methods to access field values are expensive operations in Android. Although, it is considered to be a bad design approach, developers may directly access the fields of objects rather than through a method if saving energy is the goal.  


Direct Vs Indirect Field Access


Use ViewHolder Pattern. When populating a ListView, many calls to the expensive method findViewById need to be made. This may result in performance issues and may drain the battery faster. The solution is to use ViewHolder class which caches previously drawn views and reduces the number of calls to the method findViewById.

Use Static Method Invocation. Static invocation is more efficient in Android, due to the lookup overhead incurred with virtual method invocations.


Static Vs Virtual Method Invocation

Avoid Memory Allocation During Drawing Operation: Avoid memory allocations during drawing or layout operation. This can cause garbage collection operations which may slow down UI and drain battery faster. It is recommended to allocate objects upfront and reuse them for each drawing operation.


Incorrect Vs Correct Way of Memory Allocation

Optimize Background Processes: Background processes may be battery hungry. Following the recommended 'Lazy First' approach, battery-intensive and non-critical background tasks may be reduced, deferred or batched. A broadcast may initiate multiple registered background tasks, impacting the battery performance. As a developer, you may reduce redundant background tasks by caching data instead of repeatedly waking up the device to re-download the data. Additionally, Android system provides APIs for application developers to optimize background tasks in order to improve battery consumption. For creating energy efficient Android apps, developers may use the job scheduler API (JobScheduler Class) introduced in Android 5.0 Lollipop (API 21), which can batch and defer similar jobs such as uploading user data to cloud once the battery is charging and network is connected.


Example of Scheduling a Job when Battery Charging and Network Connected

Once the conditions for job are met, the app receives a callback to onStartJob( ) method. It is important to return false and call jobFinished( ) method after the app is finished working on job so that the corresponding wakelock can be released.

Use Finish Method to Kill Unused Activities: Use the finish( ) method to close activities which may not be required, triggering the call to activity onDestroy( ) method.


States and Events in Activity Lifecycle

Credit: developer.android.com

Map Display Reduction: If the app displays map to users, features may be reduced on map based on battery level below a specific threshold such as 50 %.

Use Black Background: Use a black wallpaper as a background for your app because many screens do not consume energy when drawing black pixels.

Optimize Location Services: In order to impose lighter burden on battery, it is recommended to use fused location provider API instead of framework API to integrate location services in the app. Provided below are few of the best practices to integrate location services while optimizing for power: 
  • Specify location accuracy using setPriority(PRIORITY_BALANCED_POWER_ACCURACY)  method, which typically uses Wi-Fi / Cell information and rarely uses GPS for power optimization.
  • For background location gathering, use largest possible value for setinterval( ) method to specify the interval at which location is computed for your app.
  • Input largest possible value to the setMaxWaitTime() method, in case your app doesn’t immediately need a location update.
  • Make a call to removeLocationUpdates( ) in the activity onPause( ) or onStop( ) methods, when an activity’s onStart( ) or onResume( ) methods contain a call to requestlocationUpdates( ).
  • Set a reasonable timeout when location updates should stop by calling setExpirationDuration( ) or by calling setExpirationTime( ).
Minimize Network Battery Consumption: Network requests drain the battery faster because they use mobile and Wi-Fi radios in high power active state. Provided below are few of the best practices to reduce battery drain while making network requests:

Typical 3G Wireless Radio State Machine

Credit: developer.android.com
  • Pre-fetching data by anticipating user actions is an efficient way to reduce the number of times radio is activated, thus minimizing the energy consumed by frequent radio operation. Generally, pre-fetching data may be useful for apps which may need to initiate another download every 2 to 5 minutes, between 1 to 5 megabytes. As an example: a music player app may fetch next buffer of a song in addition to the current one, a news reader app may pre-fetch all headlines and articles on startup.   
  • Network signal search is a major battery draining operation on a mobile device. Your app should check for connectivity before making a network request.
  • It is more efficient to reuse existing network connections than to initiate new ones. This approach reduces the network overhead involved in terminating and establishing new connections for each small request. As an example, for the news app, fetch all the news headlines in single GET request. 
  • Bundling small HTTP requests could save energy instead of sending multiple requests separately. If it is necessary to make multiple small HTTP requests, developers should try to bundle them into one larger request to save energy.
  • Use network profiler to monitor the network requests initiated by your app. By monitoring the network activity of your app, you can identify areas which can be optimized for better energy performance.
  • Optimize server-initiated network use by using Google Cloud Messaging (GCM) service allowing servers to notify client-side apps for any updates instead of apps querying servers for updates, enabling greater network efficiency and lowering power usage.
  • Reduce the amount of data sent or received over the network by using data compression techniques or by using specific protocols.
  • Cache frequently required common files locally on the device, instead of requesting them from server every time.

Android Studio Network Profiler Screen

Credit: developer.android.com

Monitor Energy Use with Profiler: Use the Energy Profiler to identify areas where your app uses more energy and optimize it accordingly. The Energy Profiler monitors and displays the energy used by CPU, network radio and GPS. It also indicates system events (wake locks, alarms, jobs, and location requests) that can affect energy consumption.


Android Studio Energy Profiler Screen

Credit: developer.android.com

Optimize the Use of Java Collections: In terms of energy efficiency, few Java Collection Classes are better as compared to alternate implementations. For example, insertion operation in 'ConcurrentHashMap' can consume less than 1/3 of the same operation in 'Hashtable'. Use energy profilers and static analysis tools to compare alternate implementations of Java collections and optimize your app accordingly.

Use Advertisements Effectively: Uploading and displaying 3rd-party advertisements take up a large percentage of an app's energy usage. Advertisements must be displayed using the recommended practices for best performance without compromising the overall income. 

Refactor Code Smells:  Some code smells and bad practices have a strong impact on battery consumption of an app. Leaking Thread (when the app does not properly terminate unused threads), Member Ignoring Method (non-static methods that do not access any internal properties of the class should be made static in order to increase their efficiency), Slow Loop (for is slower than for-each loop) and Internal Getter / Setter (use of getters and setters are expensive, thus, internal fields should be accessed directly) are examples of code smells with higher impact on energy. Use refactoring and static analysis tools to identify / fix code smells to improve energy efficiency.

Improving the energy efficiency of android apps is an active research area. It is of great importance to developers as well as business leaders because it directly impacts the usability of android apps. Developers of android applications may consider above listed recommendations, if energy efficiency is the objective.

Comments