Home | History | Annotate | Download | only in articles
      1 page.title=Future-Proofing Your Apps
      2 @jd:body
      3 
      4 <p>It's important to implement your application so that it will not break as new
      5 versions of the Android platform are loaded onto the users device. The list
      6 below is based on our observations of five ways that we've seen bad apps fail.
      7 You can think of these as "anti-patterns" (that is, techniques to avoid) for
      8 Android development.</p>
      9 
     10 <p>If your application uses any of the dubious techniques below, break out 
     11 your IDE and duct tape, spackle, and patch up the app.</p>
     12 
     13 <p><b>Technique to Avoid, #1: Using Internal APIs</b></p>
     14 
     15 <p>Even
     16 though we've always strongly advised against doing so, some developers
     17 have chosen to use unsupported or internal APIs. For instance, many
     18 developers are using the internal brightness control and bluetooth
     19 toggle APIs that were present in 1.0 and 1.1. A bug -- which was
     20 fixed in Android 1.5 -- allowed apps to use those APIs without
     21 requesting permission. As a result, apps that used those APIs broke
     22 on 1.5. If you've used internal APIs in your apps, you need to update
     23 your apps to stop doing so. </p>
     24 
     25 <p><b>Technique to Avoid, #2: Directly Manipulating Settings</b></p>
     26 
     27 <p>Strictly speaking this one isn't evil, since this is a change in
     28 behavior that we made to Android itself. But we made it because some
     29 developers were doing naughty things: a number of apps were changing
     30 system settings silently without even notifying the user. For instance,
     31 some apps turn on GPS without asking the user, and others might turn on
     32 data roaming.</p>
     33 
     34 <p>As a result, applications can no longer directly
     35 manipulate the values of certain system Settings, even if they
     36 previously had permission to do so. For instance, apps can no longer
     37 directly turn on or off GPS. These apps won't crash, but the APIs in
     38 question now have no effect, and do nothing. Instead, apps will need to
     39 issue an Intent to launch the appropriate Settings configuration
     40 screen, so that the user can change these settings manually. For
     41 details, see the android.provider.Settings.Secure class, which you can
     42 find in the 1.5_pre SDK documentation (and later). Note that only
     43 Settings that were moved to the Settings.Secure class are affected.
     44 Other, less sensitive, settings will continue to have the same behavior
     45 as in Android 1.1.</p>
     46 
     47 <p><b>Technique to Avoid, #3: Going Overboard with Layouts</b></p>
     48 
     49 <p>Due to changes in the View rendering infrastructure, unreasonably deep
     50 (more than 10 or so) or broad (more than 30 total) View hierarchies in
     51 layouts are now likely to cause crashes. This was always a risk for
     52 excessively complex layouts, but you can think of Android 1.5 as being
     53 better than 1.1 at exposing this problem. Most developers won't need to
     54 worry about this, but if your app has very complicated layouts, you'll
     55 need to put it on a diet. You can simplify your layouts using the more
     56 advanced layout classes like FrameLayout and TableLayout.</p>
     57 
     58 <p><b>Technique to Avoid, #4: Bad Hardware Assumptions</b></p>
     59 
     60 <p>Android 1.5 includes support for soft keyboards, and there will soon be many
     61 devices that run Android but do not have physical keyboards. If your
     62 application assumes the presence of a physical keyboard (such as if you
     63 have created a custom View that sinks keypress events) you should make
     64 sure it degrades gracefully on devices that only have soft keyboards.
     65 For more information on this, keep on eye on this blog as we'll be
     66 posting more detailed information about handling the new soft keyboards.</p>
     67 
     68 <p><b>Technique to Avoid, #5: Incautious Rotations </b></p>
     69 
     70 <p>Devices running Android 1.5 and later can automatically rotate the screen,
     71 depending on how the user orients the device. Some 1.5 devices will do
     72 this by default, and on all others it can be turned on by the user.
     73 This can sometimes result in unpredictable behavior from applications
     74 that do their own reorientations (whether using the accelerometer, or
     75 something else.) This often happens when applications assume that the
     76 screen can only rotate if the physical keyboard is exposed; if the
     77 device lacks a physical keyboard, these apps do not expect to be
     78 reoriented, which is a coding error. Developers should be sure that
     79 their applications can gracefully handle being reoriented at any time.</p>
     80 
     81 <p>Also, apps that use the accelerometer directly to reorient themselves
     82 sometimes compete with the system doing the same thing, with odd
     83 results. And finally, some apps that use the accelerometer to detect
     84 things like shaking motions and that don't lock their orientation to
     85 portrait or landscape, often end up flipping back and forth between
     86 orientations. This can be irritating to the user. (You can lock your
     87 app's orientation to portrait or landscape using the
     88 <code>android:screenOrientation</code> attribute in the manifest file.)</p>
     89 
     90