| Robert Ly | 6181036 | 2010-12-29 16:17:52 -0800 | [diff] [blame] | 1 | page.title=Creating and Managing Projects on the Command Line |
| Robert Ly | bf6c338 | 2010-12-22 11:05:10 -0800 | [diff] [blame] | 2 | @jd:body |
| 3 | |
| 4 | <div id="qv-wrapper"> |
| 5 | <div id="qv"> |
| 6 | <h2>In this document</h2> |
| 7 | |
| 8 | <ol> |
| 9 | <li><a href="#CreatingAProject">Creating an Android Project</a></li> |
| 10 | |
| 11 | <li><a href="#UpdatingAProject">Updating a Project</a></li> |
| 12 | |
| 13 | <li><a href="#SettingUpLibraryProject">Setting up a Library Project</a></li> |
| 14 | |
| 15 | <li><a href="#ReferencingLibraryProject">Referencing a Library Project from an |
| 16 | Application</a></li> |
| 17 | </ol> |
| 18 | |
| 19 | <h2>See also</h2> |
| 20 | |
| 21 | <ol> |
| 22 | <li><a href= |
| 23 | "{@docRoot}guide/developing/testing/testing_otheride.html#CreateTestProjectCommand">Testing |
| 24 | in Other IDEs</a></li> |
| 25 | </ol> |
| 26 | </div> |
| 27 | </div> |
| 28 | |
| 29 | <p>The <code>android</code> tool provides you with commands to create all three types of |
| 30 | projects. An Android project contains all of the files and resources that are needed to build a |
| 31 | project into an .apk file for installation. |
| 32 | |
| 33 | <ul> |
| 34 | <li>An Android project contains all of the files and resources that are needed to build a project into |
| 35 | an .apk file for installation. You need to create an Android project for any application that you |
| 36 | want to eventually install on a device.</li> |
| 37 | |
| 38 | <li>You can also designate an Android project as a library project, which allows it to be shared |
| 39 | with other projects that depend on it. Once an Android project is designated as a library |
| 40 | project, it cannot be installed onto a device.</li> |
| 41 | |
| 42 | <li>Test projects extend JUnit test functionality to include Android specific functionality. For |
| 43 | more information on creating a test project, see <a href= |
| 44 | "{@docRoot}guide/developing/testing/testing_otheride.html">Testing in other IDEs</a>.</li> |
| 45 | </ul> |
| 46 | |
| 47 | |
| 48 | <h2 id="CreatingAProject">Creating an Android Project</h2> |
| 49 | |
| 50 | <p>To create an Android project, you must use the <code>android</code> tool. When you create a |
| 51 | new project with <code>android</code>, it will generate a project directory with some default |
| 52 | application files, stub files, configuration files and a build file.</p> |
| 53 | |
| 54 | <p>To create a new Android project, open a command-line, navigate to the <code>tools/</code> |
| 55 | directory of your SDK and run:</p> |
| 56 | <pre> |
| 57 | android create project \ |
| 58 | --target <target_ID> \ |
| 59 | --name <your_project_name> \ |
| 60 | --path path/to/your/project \ |
| 61 | --activity <your_activity_name> \ |
| 62 | --package <your_package_namespace> |
| 63 | </pre> |
| 64 | |
| 65 | <ul> |
| 66 | <li><code>target</code> is the "build target" for your application. It corresponds to an |
| 67 | Android platform library (including any add-ons, such as Google APIs) that you would like to |
| 68 | build your project against. To see a list of available targets and their corresponding IDs, |
| 69 | execute: <code>android list targets</code>.</li> |
| 70 | |
| 71 | <li><code>name</code> is the name for your project. This is optional. If provided, this name |
| 72 | will be used for your .apk filename when you build your application.</li> |
| 73 | |
| 74 | <li><code>path</code> is the location of your project directory. If the directory does not |
| 75 | exist, it will be created for you.</li> |
| 76 | |
| 77 | <li><code>activity</code> is the name for your default {@link android.app.Activity} class. This |
| 78 | class file will be created for you inside |
| 79 | <code><em><path_to_your_project></em>/src/<em><your_package_namespace_path></em>/</code> |
| 80 | . This will also be used for your .apk filename unless you provide a <code>name</code>.</li> |
| 81 | |
| 82 | <li><code>package</code> is the package namespace for your project, following the same rules as |
| 83 | for packages in the Java programming language.</li> |
| 84 | </ul> |
| 85 | |
| 86 | <p>Here's an example:</p> |
| 87 | <pre> |
| 88 | android create project \ |
| 89 | --target 1 \ |
| 90 | --name MyAndroidApp \ |
| 91 | --path ./MyAndroidAppProject \ |
| 92 | --activity MyAndroidAppActivity \ |
| 93 | --package com.example.myandroid |
| 94 | </pre> |
| 95 | |
| 96 | <p>Once you've created your project, you're ready to begin development. You can move your project |
| 97 | folder wherever you want for development, but keep in mind that you must use the <a href= |
| 98 | "{@docRoot}guide/developing/tools/adb.html">Android Debug Bridge</a> (adb) — located in the |
| 99 | SDK <code>platform-tools/</code> directory — to send your application to the emulator (discussed |
| 100 | later). So you need access between your project solution and the <code>platform-tools/</code> folder.</p> |
| 101 | |
| 102 | <p class="note"><strong>Tip:</strong> Add the <code>platform-tools/</code> as well as the <code>tools/</code> directory |
| 103 | to your <code>PATH</code> environment variable.</p> |
| 104 | |
| 105 | <p class="caution"><strong>Caution:</strong> You should refrain from moving the location of the |
| 106 | SDK directory, because this will break the build scripts. (They will need to be manually updated |
| 107 | to reflect the new SDK location before they will work again.)</p> |
| 108 | |
| 109 | <h2 id="UpdatingAProject">Updating a project</h2> |
| 110 | |
| 111 | <p>If you're upgrading a project from an older version of the Android SDK or want to create a new |
| 112 | project from existing code, use the <code>android update project</code> command to update the |
| 113 | project to the new development environment. You can also use this command to revise the build |
| 114 | target of an existing project (with the <code>--target</code> option) and the project name (with |
| 115 | the <code>--name</code> option). The <code>android</code> tool will generate any files and |
| 116 | folders (listed in the previous section) that are either missing or need to be updated, as needed |
| 117 | for the Android project.</p> |
| 118 | |
| 119 | <p>To update an existing Android project, open a command-line and navigate to the |
| 120 | <code>tools/</code> directory of your SDK. Now run:</p> |
| 121 | <pre> |
| 122 | android update project --name <project_name> --target <target_ID> |
| 123 | --path <path_to_your_project> |
| 124 | </pre> |
| 125 | |
| 126 | <ul> |
| 127 | <li><code>target</code> is the "build target" for your application. It corresponds to an |
| 128 | Android platform library (including any add-ons, such as Google APIs) that you would like to |
| 129 | build your project against. To see a list of available targets and their corresponding IDs, |
| 130 | execute: <code>android list targets</code>.</li> |
| 131 | |
| 132 | <li><code>path</code> is the location of your project directory.</li> |
| 133 | |
| 134 | <li><code>name</code> is the name for the project. This is optional—if you're not |
| 135 | changing the project name, you don't need this.</li> |
| 136 | </ul> |
| 137 | |
| 138 | <p>Here's an example:</p> |
| 139 | <pre> |
| 140 | android update project --name MyApp --target 2 --path ./MyAppProject |
| 141 | </pre> |
| 142 | |
| 143 | <h2 id="SettingUpLibraryProject">Setting up a library project</h2> |
| 144 | |
| 145 | <p>A library project is a standard Android project, so you can create a new one in the same way |
| 146 | as you would a new application project. Specifically, you can use the <code>android</code> tool |
| 147 | to generate a new library project with all of the necessary files and folders.</p> |
| 148 | |
| 149 | <p>To create a new library project, navigate to the <code><sdk>/tools/</code> directory and |
| 150 | use this command:</p> |
| 151 | <pre class="no-pretty-print"> |
| 152 | android create lib-project --name <your_project_name> \ |
| 153 | --target <target_ID> \ |
| 154 | --path path/to/your/project \ |
| 155 | --package <your_library_package_namespace> |
| 156 | </pre> |
| 157 | |
| 158 | <p>The <code>create lib-project</code> command creates a standard project structure that includes |
| 159 | preset property that indicates to the build system that the project is a library. It does this by |
| 160 | adding this line to the project's <code>default.properties</code> file:</p> |
| 161 | <pre class="no-pretty-print"> |
| 162 | android.library=true |
| 163 | </pre> |
| 164 | |
| 165 | <p>Once the command completes, the library project is created and you can begin moving source |
| 166 | code and resources into it, as described in the sections below.</p> |
| 167 | |
| 168 | <p>If you want to convert an existing application project to a library project, so that other |
| 169 | applications can use it, you can do so by adding a the <code>android.library=true</code> property |
| 170 | to the application's <code>default.properties</code> file.</p> |
| 171 | |
| 172 | <h4>Creating the manifest file</h4> |
| 173 | |
| 174 | <p>A library project's manifest file must declare all of the shared components that it includes, |
| 175 | just as would a standard Android application. For more information, see the documentation for |
| 176 | <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p> |
| 177 | |
| 178 | <p>For example, the <a href= |
| 179 | "{@docRoot}resources/samples/TicTacToeLib/AndroidManifest.html">TicTacToeLib</a> example library |
| 180 | project declares the Activity <code>GameActivity</code>:</p> |
| 181 | <pre> |
| 182 | <manifest> |
| 183 | ... |
| 184 | <application> |
| 185 | ... |
| 186 | <activity android:name="GameActivity" /> |
| 187 | ... |
| 188 | </application> |
| 189 | </manifest> |
| 190 | </pre> |
| 191 | |
| 192 | <h4>Updating a library project</h4> |
| 193 | |
| 194 | <p>If you want to update the build properties (build target, location) of the library project, |
| 195 | use this command:</p> |
| 196 | <pre> |
| 197 | android update lib-project \ |
| 198 | --target <em><target_ID></em> \ |
| 199 | --path <em>path/to/your/project</em> |
| 200 | </pre> |
| 201 | |
| 202 | <h2 id="ReferencingLibraryProject">Referencing a Library Project</h2> |
| 203 | |
| 204 | <p>If you are developing an application and want to include the shared code or resources from a |
| 205 | library project, you can do so easily by adding a reference to the library project in the |
| 206 | application project's build properties.</p> |
| 207 | |
| 208 | <p>To add a reference to a library project, navigate to the <code><sdk>/tools/</code> |
| 209 | directory and use this command:</p> |
| 210 | <pre> |
| 211 | android update lib-project \ |
| 212 | --target <em><target_ID></em> \ |
| 213 | --path <em>path/to/your/project</em> |
| 214 | --library <em>path/to/library_projectA</em> |
| 215 | </pre> |
| 216 | |
| 217 | <p>This command updates the application project's build properties to include a reference to the |
| 218 | library project. Specifically, it adds an <code>android.library.reference.<em>n</em></code> |
| 219 | property to the project's <code>default.properties</code> file. For example:</p> |
| 220 | <pre class="no-pretty-print"> |
| 221 | android.library.reference.1=path/to/library_projectA |
| 222 | </pre> |
| 223 | |
| 224 | <p>If you are adding references to multiple libraries, note that you can set their relative |
| 225 | priority (and merge order) by manually editing the <code>default.properties</code> file and |
| 226 | adjusting the each reference's <code>.<em>n</em></code> index as appropriate. For example, assume |
| 227 | these references:</p> |
| 228 | <pre class="no-pretty-print"> |
| 229 | android.library.reference.1=path/to/library_projectA |
| 230 | android.library.reference.2=path/to/library_projectB |
| 231 | android.library.reference.3=path/to/library_projectC |
| 232 | </pre> |
| 233 | |
| 234 | <p>You can reorder the references to give highest priority to <code>library_projectC</code> in |
| 235 | this way:</p> |
| 236 | <pre class="no-pretty-print"> |
| 237 | android.library.reference.2=path/to/library_projectA |
| 238 | android.library.reference.3=path/to/library_projectB |
| 239 | android.library.reference.1=path/to/library_projectC |
| 240 | </pre> |
| 241 | |
| 242 | <p>Note that the <code>.<em>n</em></code> index in the references must begin at "1" and increase |
| 243 | uniformly without "holes". References appearing in the index after a hole are ignored.</p> |
| 244 | |
| 245 | <p>At build time, the libraries are merged with the application one at a time, starting from the |
| 246 | lowest priority to the highest. Note that a library cannot itself reference another library and |
| 247 | that, at build time, libraries are not merged with each other before being merged with the |
| 248 | application.</p> |
| 249 | |
| 250 | <h3>Declaring library components in the the manifest file</h3> |
| 251 | |
| 252 | <p>In the manifest file of the application project, you must add declarations of all components |
| 253 | that the application will use that are imported from a library project. For example, you must |
| 254 | declare any <code><activity></code>, <code><service></code>, |
| 255 | <code><receiver></code>, <code><provider></code>, and so on, as well as |
| 256 | <code><permission></code>, <code><uses-library></code>, and similar elements.</p> |
| 257 | |
| 258 | <p>Declarations should reference the library components by their fully-qualified package names, |
| 259 | where appropriate.</p> |
| 260 | |
| 261 | <p>For example, the <a href= |
| 262 | "{@docRoot}resources/samples/TicTacToeMain/AndroidManifest.html">TicTacToeMain</a> example |
| 263 | application declares the library Activity <code>GameActivity</code> like this:</p> |
| 264 | <pre> |
| 265 | <manifest> |
| 266 | ... |
| 267 | <application> |
| 268 | ... |
| 269 | <activity android:name="com.example.android.tictactoe.library.GameActivity" /> |
| 270 | ... |
| 271 | </application> |
| 272 | </manifest> |
| 273 | </pre> |
| 274 | |
| 275 | <p>For more information about the manifest file, see the documentation for |
| 276 | <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p> |
| 277 | |
| 278 | <h3 id="depAppBuild">Building a dependent application</h3> |
| 279 | |
| 280 | <p>To build an application project that depends on one or more library projects, you can use the |
| 281 | standard Ant build commands and compile modes, as described in <a href= |
| 282 | "{@docRoot}guide/developing/building/index.html">Building Your Application</a>, earlier in this |
| 283 | document. The tools compile and merge all libraries referenced by the application as part of |
| 284 | compiling the dependent application project. No additional commands or steps are necessary.</p> |
| 285 | |