blob: 8b889ab8715069c4ffa0712bd5d0f0d887674bdc [file] [log] [blame]
Robert Ly61810362010-12-29 16:17:52 -08001page.title=Creating and Managing Projects on the Command Line
Robert Lybf6c3382010-12-22 11:05:10 -08002@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>
57android create project \
58--target &lt;target_ID&gt; \
59--name &lt;your_project_name&gt; \
60--path path/to/your/project \
61--activity &lt;your_activity_name&gt; \
62--package &lt;your_package_namespace&gt;
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>&lt;path_to_your_project&gt;</em>/src/<em>&lt;your_package_namespace_path&gt;</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>
88android 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) &mdash; located in the
99 SDK <code>platform-tools/</code> directory &mdash; 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>
122android update project --name &lt;project_name&gt; --target &lt;target_ID&gt;
123--path &lt;path_to_your_project&gt;
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&mdash;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>
140android 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>&lt;sdk&gt;/tools/</code> directory and
150 use this command:</p>
151 <pre class="no-pretty-print">
152android create lib-project --name &lt;your_project_name&gt; \
153--target &lt;target_ID&gt; \
154--path path/to/your/project \
155--package &lt;your_library_package_namespace&gt;
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">
162android.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&lt;manifest&gt;
183 ...
184 &lt;application&gt;
185 ...
186 &lt;activity android:name="GameActivity" /&gt;
187 ...
188 &lt;/application&gt;
189&lt;/manifest&gt;
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>
197android update lib-project \
198--target <em>&lt;target_ID&gt;</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>&lt;sdk&gt;/tools/</code>
209 directory and use this command:</p>
210 <pre>
211android update lib-project \
212--target <em>&lt;target_ID&gt;</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">
221android.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">
229android.library.reference.1=path/to/library_projectA
230android.library.reference.2=path/to/library_projectB
231android.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">
237android.library.reference.2=path/to/library_projectA
238android.library.reference.3=path/to/library_projectB
239android.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>&lt;activity&gt;</code>, <code>&lt;service&gt;</code>,
255 <code>&lt;receiver&gt;</code>, <code>&lt;provider&gt;</code>, and so on, as well as
256 <code>&lt;permission&gt;</code>, <code>&lt;uses-library&gt;</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&lt;manifest&gt;
266 ...
267 &lt;application&gt;
268 ...
269 &lt;activity android:name="com.example.android.tictactoe.library.GameActivity" /&gt;
270 ...
271 &lt;/application&gt;
272&lt;/manifest&gt;
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