1 page.title=Creating Multiple APKs for Different Screen Sizes 2 parent.title=Maintaining Multiple APKs 3 parent.link=index.html 4 5 trainingnavtop=true 6 previous.title=Creating Multiple APKs for Different API Levels 7 previous.link=api.html 8 next.title=Creating Multiple APKs for Different GL Textures 9 next.link=texture.html 10 11 @jd:body 12 13 <style type="text/css"> 14 .blueCell { background-color: #9fc5e8;} 15 .greenCell { background-color: #b6d7a8;} 16 .redCell { background-color: #ea9999;} 17 .blackCell { background-color: #000000;} 18 </style> 19 20 <div id="tb-wrapper"> 21 <div id="tb"> 22 23 <!-- table of contents --> 24 <h2>This lesson teaches you to</h2> 25 <ol> 26 <li><a href="#Confirm">Confirm You Need Multiple APKs</a></li> 27 <li><a href="#ChartReqs">Chart Your Requirements</a></li> 28 <li><a href="#CreateLibrary">Put All Common Code and Resources in a Library Project.</a></li> 29 <li><a href="#CreateAPKs">Create New APK Projects</a></li> 30 <li><a href="#AdjustManifests">Adjust the Manifests</a></li> 31 <li><a href="#PreLaunch">Go Over Pre-launch Checklist</a></li> 32 </ol> 33 34 <!-- other docs (NOT javadocs) --> 35 <h2>You should also read</h2> 36 <ul> 37 <li><a href="http://developer.android.com/google/play/publishing/multiple-apks.html">Multiple APK 38 Support</a></li> 39 <li><a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple Screens</a></li> 40 </ul> 41 42 </div> 43 </div> 44 45 46 <p>When developing your Android application to take advantage of multiple APKs on Google Play, 47 its important to adopt some good practices from the get-go, and prevent unnecessary headaches 48 further into the development process. This lesson shows you how to create multiple APKs of your 49 app, each covering a different class of screen size. You will also gain some tools necessary to 50 make maintaining a multiple APK codebase as painless as possible.</p> 51 52 53 <h2 id="Confirm">Confirm You Need Multiple APKs</h2> 54 55 <p>When trying to create an application that works across multiple sizes of Android devices, 56 naturally you want your application to take advantage of all the available space on larger devices, 57 without sacrificing compatibility or usability on the smaller screens. It may seem at the outset as 58 though multiple APK support is the best solution, but this often isnt the case. The <a 59 href="{@docRoot}google/play/publishing/multiple-apks.html#ApiLevelOptions">Using Single APK 60 Instead</a> section of the multiple APK developer guide includes some useful information on how to 61 accomplish this with a single APK, including use of our support library. You should also read the 62 guide to <a href="{@docRoot}guide/practices/screens_support.html">supporting multiple screens</a>, 63 and theres even a <a 64 href="http://android-developers.blogspot.com/2011/03/fragments-for-all.html">support library</a> you 65 can download using the Android SDK, which lets you use fragments on pre-Honeycomb devices (making 66 multiple-screen support in a single APK much easier).</p> 67 68 <p>If you can manage it, confining your application to a single APK has several advantages, 69 including:</p> 70 71 <ul> 72 <li>Publishing and testing are easier</li> 73 <li>Theres only one codebase to maintain</li> 74 <li>Your application can adapt to device configuration changes</li> 75 <li>App restore across devices just works</li> 76 <li>You dont have to worry about market preference, behavior from "upgrades" from one APK to the 77 next, or which APK goes with which class of devices</li> 78 </ul> 79 80 <p>The rest of this lesson assumes that youve researched the topic, studiously absorbed the 81 material in the resources linked, and determined that multiple APKs are the right path for your 82 application.</p> 83 84 <h2 id="ChartReqs">Chart Your Requirements</h2> 85 86 <p>Start off by creating a simple chart to quickly determine how many APKs you need, and what screen 87 size(s) each APK covers. Fortunately, its easy to chart out your requirements quickly and easily, 88 and have a reference for later. Start out with a row of cells representing the various screen sizes 89 available on the Android platform.</p> 90 91 <table cellpadding="10" cellspacing="0" border="1"> 92 <tbody> 93 <tr> 94 <td>small</td> 95 <td>normal</td> 96 <td>large</td> 97 <td>xlarge</td> 98 </tr> 99 </tbody> 100 </table> 101 <p> 102 Now just color in the chart such that each color represents an APK. Heres one example of how you 103 might apply each APK to a certain range of screen sizes.</p> 104 105 <table cellpadding="10" cellspacing="0" border="1"> 106 <tbody> 107 <tr> 108 <td class="blueCell">small</td> 109 <td class="blueCell">normal</td> 110 <td class="greenCell">large</td> 111 <td class="redCell">xlarge</td> 112 </tr> 113 </tbody> 114 </table> 115 <p> 116 Depending on your needs, you could also have two APKs, "small and everything else" or "xlarge and 117 everything else". Coloring in the chart also makes intra-team communication easier—You can 118 now simply refer to each APK as "blue", "green", or "red", no matter how many different screen types 119 it covers.</p> 120 121 <h2 id="CreateLibrary">Put All Common Code and Resources in a Library Project</h2> 122 <p>Whether youre modifying an existing Android application or starting one from scratch, this is 123 the first thing that you should do to the codebase, and by the far the most important. Everything 124 that goes into the library project only needs to be updated once (think language-localized strings, 125 color themes, bugs fixed in shared code), which improves your development time and reduces the 126 likelihood of mistakes that could have been easily avoided.</p> 127 128 <p class="note"><strong>Note:</strong> While the implementation details of how to create and 129 include library projects are beyond the scope of this lesson, you can get up to speed 130 by reading <a 131 href="{@docRoot}studio/projects/android-library.html">Create an Android Library</a>.</p> 132 133 <p>If youre converting an existing application to use multiple APK support, 134 scour your codebase for every localized string file, list of values, theme 135 colors, menu icons and layout that isnt going to change across APKs, and put 136 it all in the library project. Code that isnt going to change much should 137 also go in the library project. Youll likely find yourself extending these 138 classes to add a method or two from APK to APK.</p> 139 140 <p>If, on the other hand, youre creating the application from scratch, try as 141 much as possible to write code in the library project <em>first</em>, then only move it down to an 142 individual APK if necessary. This is much easier to manage in the long run than adding it to one, 143 then another, then another, then months later trying to figure out whether this blob can be moved up 144 to the library section without screwing anything up.</p> 145 146 147 148 <h2 id="CreateAPKs">Create New APK Projects</h2> 149 <p>There should be a separate Android project for each APK youre going to release. For easy 150 organization, place the library project and all related APK projects under the same parent folder. 151 Also remember that each APK needs to have the same package name, although they dont necessarily 152 need to share the package name with the library. If you were to have 3 APKs following the scheme 153 described earlier, your root directory might look like this:</p> 154 155 <pre class="no-pretty-print classic"> 156 alexlucas:~/code/multi-apks-root$ ls 157 foo-blue 158 foo-green 159 foo-lib 160 foo-red 161 </pre> 162 163 <p>Once the projects are created, add the library project as a reference to each APK project. If 164 possible, define your starting Activity in the library project, and extend that Activity in your APK 165 project. Having a starting activity defined in the library project gives you a chance to put all 166 your application initialization in one place, so that each individual APK doesnt have to 167 re-implement "universal" tasks like initializing Analytics, running licensing checks, and any other 168 initialization procedures that dont change much from APK to APK.</p> 169 170 171 <h2 id="AdjustManifests">Adjust the Manifests</h2> 172 <p>When a user downloads an application which uses multiple APKs through Google Play, the correct 173 APK to use is chosen using two simple rules:</p> 174 <ul> 175 <li>The manifest has to show that particular APK is eligible</li> 176 <li>Of the eligible APKs, highest version number wins</li> 177 </ul> 178 179 <p> 180 By way of example, lets take the set of multiple APKs described earlier, and assume that each APK 181 has been set to support all screen sizes larger than its "target" screen size. Taken individually, 182 the possible range of each APK would look like this: 183 </p> 184 <table cellpadding="10" cellspacing="0" border="1"> 185 <tbody> 186 <tr> 187 <td class="blueCell">small</td> 188 <td class="blueCell">normal</td> 189 <td class="blueCell">large</td> 190 <td class="blueCell">xlarge</td> 191 </tr> 192 <tr> 193 <td class="blackCell">small</td> 194 <td class="blackCell">normal</td> 195 <td class="greenCell">large</td> 196 <td class="greenCell">xlarge</td> 197 </tr> 198 <tr> 199 <td class="blackCell">small</td> 200 <td class="blackCell">normal</td> 201 <td class="blackCell">large</td> 202 <td class="redCell">xlarge</td> 203 </tr> 204 </tbody> 205 </table> 206 <p> 207 However, by using the "highest version number wins" rule, if we set the versionCode attribute in 208 each APK such that red ≥ green ≥ blue, the chart effectively collapses down to this:</p> 209 <table cellpadding="10" cellspacing="0" border="1"> 210 <tbody> 211 <tr> 212 <td class="blueCell">small</td> 213 <td class="blueCell">normal</td> 214 <td class="greenCell">large</td> 215 <td class="redCell">xlarge</td> 216 </tr> 217 </tbody> 218 </table> 219 <p> 220 Now, lets further assume that the Red APK has some requirement on it that the other two dont. The 221 <a href="{@docRoot}google/play/filters.html">Filters on Google Play</a> page of the Android 222 Developer guide has a whole list of possible culprits. For the sake of example, lets assume that 223 red requires a front-facing camera. In fact, the entire point of the red APK is to use the extra 224 available screen space to do entertaining things with that front-facing camera. But, it turns out, 225 not all xlarge devices even HAVE front-facing cameras! The horror!</p> 226 227 <p>Fortunately, if a user is browsing Google Play from one such device, Google Play will look at the 228 manifest, see that Red lists the front-facing camera as a requirement, and quietly ignore it, having 229 determined that Red and that device are not a match made in digital heaven. It will then see that 230 Green is not only compatible with xlarge devices, but also doesnt care whether or not theres a 231 front-facing camera! The app can still be downloaded from Google Play by the user, because 232 despite the whole front-camera mishap, there was still an APK that supported that particular screen 233 size.</p> 234 235 <p> In order to keep all your APKs on separate "tracks", its important to have a good version code 236 scheme. The recommended one can be found on the <a 237 href="{@docRoot}google/play/publishing/multiple-apks.html#VersionCodes">Version Codes</a> area of 238 our developer guide. Since the example set of APKs is only dealing with one of 3 possible 239 dimensions, it would be sufficient to separate each APK by 1000 and increment from there. This 240 might look like:</p> 241 242 <p>Blue: 1001, 1002, 1003, 1004...<br /> 243 Green: 2001, 2002, 2003, 2004...<br /> 244 Red:3001, 3002, 3003, 3004...</p> 245 246 <p> Putting this all together, your Android Manifests would likely look something like the 247 following:</p> 248 249 <p>Blue:</p> 250 <pre> 251 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 252 android:versionCode="1001" android:versionName="1.0" package="com.example.foo"> 253 <supports-screens android:smallScreens="true" 254 android:normalScreens="true" 255 android:largeScreens="true" 256 android:xlargeScreens="true" /> 257 ... 258 </pre> 259 260 <p>Green:</p> 261 <pre> 262 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 263 android:versionCode="2001" android:versionName="1.0" package="com.example.foo"> 264 <supports-screens android:smallScreens="false" 265 android:normalScreens="false" 266 android:largeScreens="true" 267 android:xlargeScreens="true" /> 268 ... 269 </pre> 270 271 <p>Red:</p> 272 <pre> 273 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 274 android:versionCode="3001" android:versionName="1.0" package="com.example.foo"> 275 <supports-screens android:smallScreens="false" 276 android:normalScreens="false" 277 android:largeScreens="false" 278 android:xlargeScreens="true" /> 279 ... 280 </pre> 281 <p> 282 Note that technically, multiple APKs will work with either the supports-screens 283 tag, or the compatible-screens tag. Supports-screens is generally preferred, 284 and its generally a really bad idea to use both tags in the same manifest. It 285 makes things needlessly complicated, and increases the opportunity for errors. 286 Also note that instead of taking advantage of the default values (small and 287 normal are always true by default), the manifests explicitly set the value for 288 each screen size. This can save you headaches down the line. For instance, a manifest with a 289 target SDK of < 9 will have xlarge automatically set to false, since that size didnt exist yet. 290 So be explicit! 291 </p> 292 293 <h2 id="PreLaunch">Go Over Pre-launch Checklist</h2> 294 <p> Before uploading to Google Play, double-check the following items. Remember that these are 295 specifically relevant to multiple APKs, and in no way represent a complete checklist for all 296 applications being uploaded to Google Play.</p> 297 <ul> 298 <li>All APKs must have the same package name</li> 299 <li>All APKs must be signed with the same certificate</li> 300 <li>Every screen size you want your APK to support, set to true in the manifest. Every screen size 301 you want it to avoid, set to false</li> 302 <li>Double check your manifest filters for conflicting information (an APK that only supports 303 cupcake on XLARGE screens isnt going to be seen by anybody)</li> 304 <li>Each APK's manifest must be unique across at least one of supported screen, openGL texture, or 305 platform version</li> 306 <li>Try to test each APK on at least one device. Barring that, you have one of the most 307 customizable device emulators in the business sitting on your development machine. Go nuts!</li> 308 </ul> 309 310 <p>Its also worth inspecting the compiled APK before pushing to market, to make sure there arent 311 any surprises that could hide your application on Google Play. This is actually quite simple using the 312 "aapt" tool. Aapt (the Android Asset Packaging Tool) is part of the build process for creating and 313 packaging your Android applications, and is also a very handy tool for inspecting them. </p> 314 315 <pre class="no-pretty-print classic"> 316 >aapt dump badging 317 package: name='com.example.hello' versionCode='1' versionName='1.0' 318 sdkVersion:'11' 319 uses-permission:'android.permission.SEND_SMS' 320 application-label:'Hello' 321 application-icon-120:'res/drawable-ldpi/icon.png' 322 application-icon-160:'res/drawable-mdpi/icon.png' 323 application-icon-240:'res/drawable-hdpi/icon.png' 324 application: label='Hello' icon='res/drawable-mdpi/icon.png' 325 launchable-activity: name='com.example.hello.HelloActivity' label='Hello' icon='' 326 uses-feature:'android.hardware.telephony' 327 uses-feature:'android.hardware.touchscreen' 328 main 329 supports-screens: 'xlarge' 330 supports-any-density: 'true' 331 locales: '--_--' 332 densities: '120' '160' '240' 333 </pre> 334 335 <p>When you examine aapt output, be sure to check that you dont have conflicting values for 336 supports-screens and compatible-screens, and that you dont have unintended "uses-feature" values 337 that were added as a result of permissions you set in the manifest. In the example above, the APK 338 will be invisible to most, if not all devices.</p> 339 <p>Why? By adding the required permission SEND_SMS, the feature requirement of android.hardware.telephony was implicitly added. Since most (if not all) xlarge devices are tablets without telephony hardware in them, Google Play will filter out this APK in these cases, until future devices come along which are both large enough to report as xlarge screen size, and possess telephony hardware. 340 </p> 341 <p>Fortunately this is easily fixed by adding the following to your 342 manifest:</p> 343 <pre> 344 <uses-feature android:name="android.hardware.telephony" android:required="false" /> 345 </pre> 346 <p>The <code>android.hardware.touchscreen</code> requirement is also implicitly added. If you want your APK to be visible on TVs which are non-touchscreen devices you should add the following to your manifest:</p> 347 <pre> 348 <uses-feature android:name="android.hardware.touchscreen" android:required="false" /> 349 </pre> 350 351 <p>Once youve completed the pre-launch checklist, upload your APKs to Google Play. It may take a bit for the application to show up when browsing Google Play, but when it does, perform one last check. Download the application onto any test devices you may have to make sure that the APKs are targeting the intended devices. Congratulations, youre done!</p> 352