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