Short answer. The short story is that Android is doing what it says, creating an optimized version of each app for the new version of Android you just upgraded to. This process makes each app start as fast as possible with the new Android version.
Android vitals can help improve your app's performance by alerting you, via the Play Console, when your app's startup times are excessive. Android vitals considers your app's startup times excessive when the app's: Cold startup takes 5 seconds or longer. Warm startup takes 2 seconds or longer.
In android, by pressing a back button or home button. So put an event key listener for back & home button and terminate the service.
App Standby Buckets help the system prioritize apps' requests for resources based on how recently and how frequently the apps are used. Based on app usage patterns, each app is placed in one of five priority buckets. The system limits the device resources available to each app based on which bucket the app is in.
At some point you are going to get to the point where using known tricks will hit their limits. The best thing to do at this point is profile your code and see what areas are the bottle-necks based on your specific requirements.
Investigating RAM usage using MAT and Using Traceview: an article on how to use the tools to profile your application.
Track and squash allocations. The more you allocate, the more often the garbage collector will need to run, stopping your process from doing anything else for relatively long periods of time, such as 100ms or so.
The best tool I know for this is the Allocation Tracker included in DDMS.
Not only GC can have an impact on the user experience, but superfluous allocations and GC do consume some computing resources.
Here's an example and a small trick. In my app, I have a clock which shows the current (audio) time, including tenth of seconds. This is updated often. And TextView performs allocations internally whenever you call setText() with a CharSequence. But it doesn't allocate anything with the setText(char[] text, int start, int len) variant. This isn't documented, and no one answered when I asked about it.
There are many ones like this. And this is one of the reasons why my app contains 50% native code (but there are other reasons).
Apart from this, I can recommend that you experiment with ProGuard. It performs several optimization passes, and logs such informations as unused methods within the project, which can help you removing leftovers in your code.
If your app will have a lot of screen time, use black wherever you can. That will reduce the battery consumption of the worst part of the device: the screen, specially in the AMOLED phones and tablets.
For applications with multiple activities, check you are not restarting activities that just need to be brought to the front by using the appropriate Intent flags. Check that your heap is under control, and that unnecessary views, bindings and contexts aren't being created.
I find the best tool for showing you all these as the app runs is:
adb shell dumpsys meminfo 'your apps package name'
When using SQLlite, put special attention on indexes. Don't assume anything. I got tremendous speedups in Zwitscher, when I put indexes on columns commonly used for search.
A few tips that can help you optimize your app in terms of UI:
use convertView
for list adapters - it would be very expensive if you create a new view inside Adapter.getView()
as this routine is called for every position in the list. Using convertView
lets you reuse the already created view. Good example (together with the use of ViewHolder
) can be found in ApiDemos.
It might happen that your layouts are not fully optimized and can be improved (for instance by using merging or removing parents). Android tool layoutopt will find such a situation for you. It can be used with HierarchyViewer for inspecting individual views. More info here.
remove the background drawable - Android framework used to have (does it still have?) a problem with detecting what views should be drawn. There is a chance that your (default) background drawable will be drawn only to be subsequently hidden by your opaque UI. To get rid of this wasteful drawing simply remove background drawable.
It can be done using a custom style
<resources>
<style name="Theme.NoBackground" parent="android:Theme">
<item name="android:windowBackground">@null</item>
</style>
</resources>
A few tips that can help you optimize your app in terms of battery usage:
check networking type and wait until user gets in the area with wifi or 3G (and not roaming) and only then allow him to use connection
use gzip for textual data whenever possible to speed up download and parsing
recycle complex java objects such as XmlPullParserFactory
/BitmapFactory
/StringBuilder
/Matcher
etc.
For more battery tricks see Coding for Life - Battery Life, That Is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With