blob: af8501572f648794a7e542256b507e667097da63 [file] [log] [blame]
Scott Main50e990c2012-06-21 17:14:39 -07001page.title=Managing Projects from Eclipse with ADT
2parent.title=Managing Projects
3parent.link=index.html
4@jd:body
5
6 <div id="qv-wrapper">
7 <div id="qv">
8 <h2>In this document</h2>
9
10 <ol>
11 <li><a href="#CreatingAProject">Creating an Android 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</a></li>
16 </ol>
17
18 <h2>See also</h2>
19
20 <ol>
21 <li><a href=
22 "{@docRoot}tools/testing/testing_eclipse.html#CreateTestProjectEclipse">Testing
23 from Eclipse with ADT</a></li>
24 </ol>
25 </div>
26 </div>
27
28 <p>Eclipse and the ADT plugin provide GUIs and wizards to create all three types of projects
29 (Android project, Library project, and Test project):
Joe Fernandez025ec882012-11-16 16:10:52 -080030
Scott Main50e990c2012-06-21 17:14:39 -070031 <ul>
32 <li>An Android project contains all of the files and resources that are needed to build a project into
33 an .apk file for installation. You need to create an Android project for any application that you
34 want to eventually install on a device.</li>
35
36 <li>You can also designate an Android project as a library project, which allows it to be shared
37 with other projects that depend on it. Once an Android project is designated as a library
38 project, it cannot be installed onto a device.</li>
39
40 <li>Test projects extend JUnit test functionality to include Android specific functionality. For
41 more information on creating a test project, see <a href=
42 "{@docRoot}tools/testing/testing_eclipse.html">Testing from Eclipse with ADT</a>.</li>
43 </ul>
44
45 <h2 id="CreatingAProject">Creating an Android Project</h2>
46
Joe Fernandez025ec882012-11-16 16:10:52 -080047 <p>The ADT plugin provides a <em>New Project Wizard</em> that you can use to quickly create a new
48 Android project (or a project from existing code). To create a new project:</p>
Scott Main50e990c2012-06-21 17:14:39 -070049
50 <ol>
51 <li>Select <strong>File</strong> &gt; <strong>New</strong> &gt; <strong>Project</strong>.</li>
52
Joe Fernandez025ec882012-11-16 16:10:52 -080053 <li>Select <strong>Android</strong> &gt; <strong>Android Application Project</strong>, and click
Scott Main50e990c2012-06-21 17:14:39 -070054 <strong>Next</strong>.</li>
55
Joe Fernandez025ec882012-11-16 16:10:52 -080056 <li>Enter the basic settings for the project:
Scott Main50e990c2012-06-21 17:14:39 -070057
58 <ul>
Joe Fernandez025ec882012-11-16 16:10:52 -080059 <li>Enter an <strong>Application Name</strong>. This name is used as the title of your
60 application launcher icon when it is installed on a device.</li>
Scott Main50e990c2012-06-21 17:14:39 -070061
Joe Fernandez025ec882012-11-16 16:10:52 -080062 <li>Enter a <strong>Project Name</strong>. This text is used as the name of the folder where
63 your project is created.</li>
Scott Main50e990c2012-06-21 17:14:39 -070064
Joe Fernandez025ec882012-11-16 16:10:52 -080065 <li>Enter a <strong>Package Name</strong>. This class package namespace creates the initial
66 package structure for your applications code files and is added as the
67 <a href="{@docRoot}guide/topics/manifest/manifest-element.html#package">{@code package}</a>
68 attribute in your application's
69 <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">Android manifest file</a>.
70 This manifest value serves as the unique identifier for your application app when you
71 distribute it to users. The package name must follow the same rules as packages in the Java
72 programming language.</li>
Scott Main50e990c2012-06-21 17:14:39 -070073
Joe Fernandez025ec882012-11-16 16:10:52 -080074 <li>Select a <strong>Minimum Required SDK</strong>. This setting indicates the lowest
75 version of the Android platform that your application supports. This value sets the
76 <code>minSdkVersion</code> attribute in the
77 <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">&lt;uses-sdk&gt;</a>
78 element of your manifest file.</li>
Scott Main50e990c2012-06-21 17:14:39 -070079
Joe Fernandez025ec882012-11-16 16:10:52 -080080 <li>Select a <strong>Target SDK</strong>. This setting indicates the highest version of
81 Android with which you have tested with your application and sets the
82 <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code
83 targetSdkVersion}</a> attribute in your application's' manifest file.
84
85 <p class="note"><strong>Note:</strong> You can change the target SDK for your
Scott Main50e990c2012-06-21 17:14:39 -070086 project at any time: Right-click the project in the Package Explorer, select
87 <strong>Properties</strong>, select <strong>Android</strong> and then check the desired
Joe Fernandez025ec882012-11-16 16:10:52 -080088 <strong>Project Build Target</strong>.</p>
Scott Main50e990c2012-06-21 17:14:39 -070089 </li>
90
Joe Fernandez025ec882012-11-16 16:10:52 -080091 <li>Select a <strong>Compile With</strong> API version. This setting specifies what version
92 of the SDK to compile your project against. We strongly recommend using the most recent
93 version of the API.</li>
Scott Main50e990c2012-06-21 17:14:39 -070094
Joe Fernandez025ec882012-11-16 16:10:52 -080095 <li>Select a <strong>Theme</strong>. This setting specifies which standard Android
96 <a href="{@docRoot}design/style/themes.html">visual style</a> is applied to your
97 application.</li>
Scott Main50e990c2012-06-21 17:14:39 -070098
Joe Fernandez025ec882012-11-16 16:10:52 -080099 <li>Click <strong>Next</strong>.</li>
Scott Main50e990c2012-06-21 17:14:39 -0700100 </ul>
101 </li>
102
Joe Fernandez025ec882012-11-16 16:10:52 -0800103 <li>In the <strong>Configure Project</strong> page, select the desired settings and click
104 <strong>Next</strong>. Leave the <strong>Create activity</strong> option checked so you can
105 start your application with some essential components.</li>
106
107 <li>In the <strong>Configure Launcher Icon</strong> page, create an icon and click
108 <strong>Next</strong>.</li>
109
110 <li>In the <strong>Create Activity</strong> page, select activity template and click
111 <strong>Next</strong>. For more information about Android code templates, see
112 <a href="{@docRoot}tools/projects/templates.html">Using Code Templates</a>.
113 </li>
114
115 <li>Click <strong>Finish</strong> and the wizard creates a new project according to the options
116 you have chosen.</li>
Scott Main50e990c2012-06-21 17:14:39 -0700117 </ol>
118
Joe Fernandez025ec882012-11-16 16:10:52 -0800119 <p class="note"><strong>Tip:</strong> You can also start the New Project Wizard by clicking the
120 <strong>New</strong> <img src="/images/tools/eclipse-new.png"
121 style="vertical-align:baseline;margin:0"> icon in the toolbar.</p>
122
Scott Main50e990c2012-06-21 17:14:39 -0700123
124 <h2 id="SettingUpLibraryProject">Setting up a Library Project</h2>
125
126 <p>A library project is a standard Android project, so you can create a new one in the same way
127 as you would a new application project.</p>
128
Joe Fernandez025ec882012-11-16 16:10:52 -0800129 <p>To create a new library project:</p>
Scott Main50e990c2012-06-21 17:14:39 -0700130
Joe Fernandez025ec882012-11-16 16:10:52 -0800131 <ol>
132 <li>Select <strong>File</strong> &gt; <strong>New</strong> &gt; <strong>Project</strong>.</li>
133
134 <li>Select <strong>Android</strong> &gt; <strong>Android Application Project</strong>, and click
135 <strong>Next</strong>.</li>
136
137 <li>Enter the basic settings for the project, including <strong>Application Name</strong>,
138 <strong>Project Name</strong>, <strong>Package Name</strong>, and SDK settings.</li>
139
140 <li>In the <strong>Configure Project</strong> page, select the <strong>Mark this project as a
141 library</strong> option to flag the project as a library.</li>
142
143 <li>Set the other options as desired and click <strong>Next</strong>.</li>
144
145 <li>Follow the instructions to complete the wizard and create a new library project.</li>
146 </ol>
147
148 <p>You can also convert an existing application project into a library. To do so, simply open the
149 Properties for the project and select the <strong>is Library</strong> checkbox, as shown in
150 the figure below.</p>
151
152 <img src= "{@docRoot}images/developing/adt-props-isLib.png">
153 <p class="img-caption"><strong>Figure 1.</strong> Marking a project as an Android library.</p>
154
155 <p>To set the a project's properties to indicate that it is a library project:</p>
Scott Main50e990c2012-06-21 17:14:39 -0700156
157 <ol>
158 <li>In the <strong>Package Explorer</strong>, right-click the library project and select
159 <strong>Properties</strong>.</li>
160
Joe Fernandez025ec882012-11-16 16:10:52 -0800161 <li>In the <strong>Properties</strong> window, select the <strong>Android</strong> properties
162 group in the left pane and locate the <strong>Library</strong> properties in the right pane.</li>
Scott Main50e990c2012-06-21 17:14:39 -0700163
Joe Fernandez025ec882012-11-16 16:10:52 -0800164 <li>Select the <strong>is Library</strong> check box and click <strong>Apply</strong>.</li>
Scott Main50e990c2012-06-21 17:14:39 -0700165
Joe Fernandez025ec882012-11-16 16:10:52 -0800166 <li>Click <strong>OK</strong> to close the <strong>Properties</strong> window.</li>
Scott Main50e990c2012-06-21 17:14:39 -0700167 </ol>
168
Joe Fernandez025ec882012-11-16 16:10:52 -0800169 <p>Once you create a library project or mark an existing project as a library, you can reference
170 the library project in other Android application projects. For more information, see the
171 <a href="#ReferencingLibraryProject">Referencing a library project</a> section.
Scott Main50e990c2012-06-21 17:14:39 -0700172
Scott Main50e990c2012-06-21 17:14:39 -0700173
174 <h3>Creating the manifest file</h3>
175
176 <p>A library project's manifest file must declare all of the shared components that it includes,
177 just as would a standard Android application. For more information, see the documentation for
178 <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p>
179
180 <p>For example, the <a href=
181 "{@docRoot}resources/samples/TicTacToeLib/AndroidManifest.html">TicTacToeLib</a> example library
Joe Fernandez025ec882012-11-16 16:10:52 -0800182 project declares the activity <code>GameActivity</code>:</p>
Scott Main50e990c2012-06-21 17:14:39 -0700183 <pre>
184&lt;manifest&gt;
185 ...
186 &lt;application&gt;
187 ...
188 &lt;activity android:name="GameActivity" /&gt;
189 ...
190 &lt;/application&gt;
191&lt;/manifest&gt;
192</pre>
193
194 <h2 id="ReferencingLibraryProject">Referencing a library project</h2>
195
196 <p>If you are developing an application and want to include the shared code or resources from a
197 library project, you can do so easily by adding a reference to the library project in the
198 application project's Properties.</p>
199
200 <p>To add a reference to a library project, follow these steps:</p>
201
202 <ol>
Joe Fernandez025ec882012-11-16 16:10:52 -0800203 <li>Make sure that both the project library and the application project that depends on it are
204 in your workspace. If one of the projects is missing, import it into your workspace.</li>
205
Scott Main50e990c2012-06-21 17:14:39 -0700206 <li>In the <strong>Package Explorer</strong>, right-click the dependent project and select
207 <strong>Properties</strong>.</li>
208
209 <li>In the <strong>Properties</strong> window, select the "Android" properties group at left
210 and locate the <strong>Library</strong> properties at right.</li>
211
212 <li>Click <strong>Add</strong> to open the <strong>Project Selection</strong> dialog.</li>
213
214 <li>From the list of available library projects, select a project and click
215 <strong>OK</strong>.</li>
216
217 <li>When the dialog closes, click <strong>Apply</strong> in the <strong>Properties</strong>
218 window.</li>
219
220 <li>Click <strong>OK</strong> to close the <strong>Properties</strong> window.</li>
221 </ol>
222
223 <p>As soon as the Properties dialog closes, Eclipse rebuilds the project, including the contents
224 of the library project.</p>
225
226 <p>Figure 2 shows the Properties dialog that lets you add library references and move
227 them up and down in priority.</p><img src="{@docRoot}images/developing/adt-props-libRef.png">
228
229 <p class="img-caption"><strong>Figure 2.</strong> Adding a reference to a
230 library project in the properties of an application project.</p>
231
232 <p>If you are adding references to multiple libraries, note that you can set their relative
233 priority (and merge order) by selecting a library and using the <strong>Up</strong> and
234 <strong>Down</strong> controls. The tools merge the referenced libraries with your application
235 starting from lowest priority (bottom of the list) to highest (top of the list). If more than one
236 library defines the same resource ID, the tools select the resource from the library with higher
237 priority. The application itself has highest priority and its resources are always used in
238 preference to identical resource IDs defined in libraries.</p>
239
240 <h3>Declaring library components in the manifest file</h3>
241
242 <p>In the manifest file of the application project, you must add declarations of all components
243 that the application will use that are imported from a library project. For example, you must
244 declare any <code>&lt;activity&gt;</code>, <code>&lt;service&gt;</code>,
245 <code>&lt;receiver&gt;</code>, <code>&lt;provider&gt;</code>, and so on, as well as
246 <code>&lt;permission&gt;</code>, <code>&lt;uses-library&gt;</code>, and similar elements.</p>
247
248 <p>Declarations should reference the library components by their fully-qualified package names,
249 where appropriate.</p>
250
251 <p>For example, the <a href=
252 "{@docRoot}resources/samples/TicTacToeMain/AndroidManifest.html">TicTacToeMain</a> example
Joe Fernandez025ec882012-11-16 16:10:52 -0800253 application declares the library activity <code>GameActivity</code> like this:</p>
Scott Main50e990c2012-06-21 17:14:39 -0700254 <pre>
255&lt;manifest&gt;
256 ...
257 &lt;application&gt;
258 ...
259 &lt;activity android:name="com.example.android.tictactoe.library.GameActivity" /&gt;
260 ...
261 &lt;/application&gt;
262&lt;/manifest&gt;
263</pre>
264
265 <p>For more information about the manifest file, see the documentation for <a href=
266 "{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a>.</p>
267
268
269
270
271
272
273