Thursday, 24 September 2015

Building Apps with Over 65K Methods

As the Android platform has continued to grow, so has the size of Android apps. When your application and the libraries it references reach a certain size, you encounter build errors that indicate your app has reached a limit of the Android app build architecture.


About the 65K Reference Limit

Android application (APK) files contain executable byte code files in the form of Dalvik  Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.

Multidex support prior to Android 5.0

Versions of the platform prior to Android 5.0 use the Dalvik runtime for executing app code. By default, Dalvik limits apps to a single classes.dex bytecode file per APK. In order to get around this limitation, you can use the multidex support library, which becomes part of the primary DEX file of your app and then manages access to the additional DEX files and the code they contain.

Multidex support for Android 5.0 and higher

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files. ART performs pre-compilation at application install time which scans for classes(..N).dex files and compiles them into a single .oat file for execution by the Android device. For more information on the Android 5.0 runtime, see Introducing ART.

Avoiding the 65K Limit


Before configuring your app to enable use of 65K or more method references, you should take steps to reduce the total number of references called by your app code, including methods defined by your app code or included libraries. The following strategies can help you avoid hitting the dex reference limit:
  • Review your app's direct and transitive dependencies - Ensure any large library dependency you include in your app is used in a manner that outweighs the amount of code being added to the application. A common anti-pattern is to include a very large library because a few utility methods were useful. Reducing your app code dependencies can often help you avoid the dex reference limit.
  • Remove unused code with ProGuard - Configure the ProGuard settings for your app to run ProGuard and ensure you have shrinking enabled for release builds. Enabling shrinking ensures you are not shipping unused code with your APKs.
Using these techniques can help you avoid the build configuration changes required to enable more method references in your app. These steps can also decrease the size of your APKs, which is particularly important for markets where bandwidth costs are high.

Configuring Your App for Multidex with Gradle


The Android plugin for Gradle available in Android SDK Build Tools 21.1 and higher supports multidex as part of your build configuration. Make sure you update the Android SDK Build Tools tools and the Android Support Repository to the latest version using the SDK Manager before attempting to configure your app for multidex.
Setting up your app development project to use a multidex configuration requires that you make a few modifications to your app development project. In particular you need to perform the following steps:
  • Change your Gradle build configuration to enable multidex
  • Modify your manifest to reference the MultiDexApplication class
Modify your app Gradle build file configuration to include the support library and enable multidex output, as shown in the following Gradle build file snippet:
public class App extends android.support.multidex.MultiDexApplication {
//your code here
}

Limitations of the multidex support library

The multidex support library has some known limitations that you should be aware of and test for when you incorporate it into your app build configuration:
  • The installation of .dex files during startup onto a device's data partition is complex and can result in Application Not Responding (ANR) errors if the secondary dex files are large. In this case, you should apply code shrinking techniques with ProGuard to minimize the size of dex files and remove unused portions of code.
  • Applications that use multidex may not start on devices that run versions of the platform earlier than Android 4.0 (API level 14) due to a Dalvik linearAlloc bug (Issue 22586). If you are targeting API levels earlier than 14, make sure to perform testing with these versions of the platform as your application can have issues at startup or when particular groups of classes are loaded. Code shrinking can reduce or possibly eliminate these potential issues.
  • Applications using a multidex configuration that make very large memory allocation requests may crash during run time due to a Dalvik linearAlloc limit (Issue 78035). The allocation limit was increased in Android 4.0 (API level 14), but apps may still run into this limit on Android versions prior to Android 5.0 (API level 21).
  • There are complex requirements regarding what classes are needed in the primary dex file when executing in the Dalvik runtime. The Android build tooling updates handle the Android requirements, but it is possible that other included libraries have additional dependency requirements including the use of introspection or invocation of Java methods from native code. Some libraries may not be able to be used until the multidex build tools are updated to allow you to specify classes that must be included in the primary dex file.

Monday, 14 September 2015

Discussions - Marshmallow Android 6.0 with new Features

Marshmallow Android 6.0 with new Features

Google Finally announced that the next generation Android M is “Marshmallow”. This news was announced by Dave Burke. The Google will soon allow developers to upload apps base on Android 6.0 to be uploaded on the PlayStore.

Android 6.0 is mainly aimed at stability and performance improvements, and will bring new features such as – Fingerprint Scanner, Android Pay , MultiWindow , Tap on Now, New app Permissions Model, Support for the USB Type-C, Doze for Battery .

Here i describe few features that has been introduced in Marshmallow :

1) Support Fingerprint: API for Fingerprint support will be provided and if developer want they can incorporate the API into their app.

2) App Permission: How many time you want to restrict the permission that are given to apps. Now in Android M the new API allows users to decide what permission they want to give to any particular app.

3) Google Now: Now searching easy is Easy on Android M. It is a voice assisted search that allows you to search anything without leaving the app currently running. I think that one is best facility.

Main Feature
4) Doze for Battery : This battery saving feature of Marshmallow that will allow the phone battery to last as much as two and half time longer than Lollipop. It will detect if the phone is not touched or moved for a long period of time it will go to sleep, hence saving precious battery.

Lollipop Android 5.0 with new Features:

1.RecylcerView
2.CardView
3.Snack Bar
4.co-ordinating layout
5.TextInputLayout (EditText)
6. elevation
7.Ripple effect
8. FAB
9.SwipeRefresh Layout
10.alert Dialog
11.Vector Drawables
12.Activity Transitons
13. Themes
14.Navigation Drawer
15.Color Pallete 
16. Layout Manager(stagged, Grid)
17.AppBarLayout
18.CollapsingToolbarLayout
19.collabration layout 
20.Paralllax Model
21.reveal animations

Difference between asynctask and service in android

By default, a Service object runs on the main thread, also called the foreground thread. The only real difference between a Service and an Activity is that a Service doesn't have a UI. An *IntentService* runs on a separate thread, but its thread stops once it's processed every instruction in its main method, so by default it doesn't maintain its state. 

An AsyncTask is initiated by the main thread, but runs in a separate thread. However, if the Activity that started the AsyncTask is moved to the background, the AsyncTask stops, and its work may not be completed. 
Remember that the main thread runs your UI. Don't block it with long-running operations, or the user will see Application Not Responding (ANR). In addition, more recent versions of the platform don't allow you to run network operations on the main thread. 

For operations that you want to run in the background, but you're willing to restart if your Activity disappears, use an AsyncTask. For a discrete operation that you want to run to completion without communication, using a background thread, use IntentService. 

If you want a "presence" for your app that continues to chug along, perhaps doing nothing, even when your app isn't in the foreground, consider a bound Service.