1page.title=Using Eclipse
2@jd:body
3
4<!--
5    Copyright 2013 The Android Open Source Project
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18-->
19<div id="qv-wrapper">
20  <div id="qv">
21    <h2>In this document</h2>
22    <ol id="auto-toc">
23    </ol>
24  </div>
25</div>
26<p>This document will help you set up the Eclipse IDE for Android platform development.</p>
27<p><em>Note: if you are looking for information on how to use
28Eclipse to develop applications that run on Android, this is not the right
29page for you. You probably would find <a href="https://developer.android.com/sdk/eclipse-adt.html">the Eclipse page on
30developer.android.com</a> more useful.</em></p>
31<h2 id="basic-setup">Basic setup</h2>
32<p>First, it's important to make sure the regular Android development system is set up.</p>
33<pre><code>cd /path/to/android/root
34make
35</code></pre>
36<p><strong>Important</strong>: You will still be using <code>make</code> to build the files you will actually run (in the emulator or on a device). You will be using Eclipse to edit files and verify that they compile, but when you want to run something you will need to make sure files are saved in Eclipse and run <code>make</code> in a shell. The Eclipse build is just for error checking.</p>
37<p>Eclipse needs a list of directories to search for Java files. This is called the "Java Build Path" and can be set with the <code>.classpath</code> file. We have a sample version to start you off.</p>
38<pre><code>cd /path/to/android/root
39cp development/ide/eclipse/.classpath .
40chmod u+w .classpath
41</code></pre>
42<p>Now edit that copy of <code>.classpath</code>, if necessary.</p>
43<h3 id="increase-eclipses-memory-settings">Increase Eclipse's Memory Settings</h3>
44<p>The Android project is large enough that Eclipse's Java VM sometimes runs out of memory while compiling it. Avoid this problem by editing the <code>eclipse.ini</code> file. On Apple OSX the eclipse.ini file is located at</p>
45<pre><code>/Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini
46</code></pre>
47<p>Memory-related defaults (as of Eclipse 3.4):</p>
48<pre><code>-Xms40m
49-Xmx256m
50-XX:MaxPermSize=256m
51</code></pre>
52<p>Recommended settings for Android development:</p>
53<pre><code>-Xms128m
54-Xmx512m
55-XX:MaxPermSize=256m
56</code></pre>
57<p>These settings set Eclipse's minimum Java heap size to 128MB, set the maximum Java heap size to 512MB, and keep the maximum permanent generation size at the default of 256MB.</p>
58<p>Now start Eclipse:</p>
59<pre><code>eclipse
60</code></pre>
61<p>Now create a project for Android development:</p>
62<ol>
63<li>
64<p>If Eclipse asks you for a workspace location, choose the default.</p>
65</li>
66<li>
67<p>If you have a "Welcome" screen, close it to reveal the Java perspective.</p>
68</li>
69<li>
70<p>File &gt; New &gt; Java Project</p>
71</li>
72<li>
73<p>Pick a project name, "android" or anything you like.</p>
74</li>
75<li>
76<p>Select "Create project from existing source", enter the path to your Android root directory, and click Finish.</p>
77</li>
78<li>
79<p>Wait while it sets up the project. (You'll see a subtle progress meter in the lower right corner.)</p>
80</li>
81</ol>
82<p>Once the project workspace is created, Eclipse should start building. In theory, it should build with no errors and you should be set to go. If necessary, uncheck and re-check Project Build Automatically to force a rebuild.</p>
83<p><em>Note:</em> Eclipse sometimes likes to add an <code>import android.R</code> statement at the top of your files that use resources, especially when you ask eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.</p>
84<h3 id="when-you-sync">When You Sync</h3>
85<p>Every time you repo sync, or otherwise change files outside of Eclipse (especially the .classpath), you need to refresh Eclipse's view of things:</p>
86<ol>
87<li>
88<p>Window &gt; Show View &gt; Navigator</p>
89</li>
90<li>
91<p>In the Navigator, right-click on the project name</p>
92</li>
93<li>
94<p>Click Refresh in the context menu</p>
95</li>
96</ol>
97<h3 id="adding-apps-to-the-build-path">Adding Apps to the Build Path</h3>
98<p>The default <code>.classpath</code> includes the source to the core system and a sample set of apps, but might not include the particular app you may want to work on. To add an app, you must add the app's source directory. To do this inside Eclipse:</p>
99<ol>
100<li>
101<p>Project &gt; Properties</p>
102</li>
103<li>
104<p>Select "Java Build Path" from the left-hand menu.</p>
105</li>
106<li>
107<p>Choose the "Source" tab.</p>
108</li>
109<li>
110<p>Click "Add Folder..."</p>
111</li>
112<li>
113<p>Add your app's <code>src</code> directory.</p>
114</li>
115<li>
116<p>Click OK.</p>
117</li>
118</ol>
119<p>When you're done, the "source folder" path in the list should look like </p>
120<pre><code>android/packages/apps/YOURAPP/src
121</code></pre>
122<p>Depending on which app(s) you include, you may also need to include <code>othersrc/main/java</code> directories under <code>android/dalvik/libcore</code>. Do this if you find you cannot build with the default set.</p>
123<h2 id="eclipse-formatting">Eclipse formatting</h2>
124<p>You can import files in <code>development/ide/eclipse</code> to make Eclipse
125follow the Android style rules.  </p>
126<ol>
127<li>
128<p>Select Window &gt; Preferences &gt; Java &gt; Code Style.</p>
129</li>
130<li>
131<p>Use Formatter &gt; Import to import <code>android-formatting.xml</code>.</p>
132</li>
133<li>
134<p>Organize Imports &gt; Import to import <code>android.importorder</code>.</p>
135</li>
136</ol>
137<h2 id="debugging-the-emulator-with-eclipse">Debugging the emulator with Eclipse</h2>
138<p>You can also use eclipse to debug the emulator and step through code. First, start the emulator running:</p>
139<pre><code>cd /path/to/android/root
140. build/envsetup.sh
141lunch 1
142make
143emulator
144</code></pre>
145<p>If the emulator is running, you should see a picture of a phone.</p>
146<p>In another shell, start DDMS (the Dalvik debug manager):</p>
147<pre><code>cd /path/to/android/root
148ddms
149</code></pre>
150<p>You should see a splufty debugging console.</p>
151<p>Now, in eclipse, you can attach to the emulator:</p>
152<ol>
153<li>
154<p>Run &gt; Open Debug Dialog...</p>
155</li>
156<li>
157<p>Right-click "Remote Java Application", select "New".</p>
158</li>
159<li>
160<p>Pick a name, i.e. "android-debug" or anything you like.</p>
161</li>
162<li>
163<p>Set the "Project" to your project name.</p>
164</li>
165<li>
166<p>Keep the Host set to "localhost", but change Port to 8700.</p>
167</li>
168<li>
169<p>Click the "Debug" button and you should be all set.</p>
170</li>
171</ol>
172<p>Note that port 8700 is attached to whatever process is currently selected in the DDMS console, so you need to sure that DDMS has selected the process you want to debug.</p>
173<p>You may need to open the Debug perspective (next to the "Java" perspective icon in the upper-right, click the small "Open Perspective" icon and select "Debug"). Once you do, you should see a list of threads; if you select one and break it (by clicking the "pause" icon), it should show the stack trace, source file, and line where execution is at. Breakpoints and whatnot should all work.</p>
174<h2 id="bonus-material">Bonus material</h2>
175<p>Replace Ctrl with the Apple key on Mac.</p>
176<table>
177<thead>
178<tr>
179<th>shortcut</th>
180<th>function</th>
181</tr>
182</thead>
183<tbody>
184<tr>
185<td>Ctrl-Shift-o</td>
186<td>Organize imports</td>
187</tr>
188<tr>
189<td>Ctrl-Shift-t</td>
190<td>load class by name</td>
191</tr>
192<tr>
193<td>Ctrl-Shift-r</td>
194<td>load non-class resource by name</td>
195</tr>
196<tr>
197<td>Ctrl-1</td>
198<td>quick fix</td>
199</tr>
200<tr>
201<td>Ctrl-e</td>
202<td>Recently viewed files</td>
203</tr>
204<tr>
205<td>Ctrl-space</td>
206<td>auto complete</td>
207</tr>
208<tr>
209<td>Shift-Alt-r</td>
210<td>refactor:rename</td>
211</tr>
212<tr>
213<td>Shift-Alt-v</td>
214<td>refactor:move</td>
215</tr>
216</tbody>
217</table>
218<h2 id="eclipse-is-not-working-correctly-what-should-i-do">Eclipse is not working correctly, what should I do?</h2>
219<p>Make sure:</p>
220<ul>
221<li>
222<p>You followed the instructions on this page precisely.</p>
223</li>
224<li>
225<p>Your Problems view doesn't show any errors.</p>
226</li>
227<li>
228<p>Your application respects the package/directory structure.</p>
229</li>
230</ul>
231<p>If you're still having problems, please contact one of the Android mailing lists or IRC channels.</p>
232