1page.title=Using Eclipse
2@jd:body
3
4<!--
5    Copyright 2015 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">Increasing 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<h3 id="create-project">Creating a project</h3>
62<p>Now create a project for Android development:</p>
63<ol>
64<li>
65<p>If Eclipse asks you for a workspace location, choose the default.</p>
66</li>
67<li>
68<p>If you have a "Welcome" screen, close it to reveal the Java perspective.</p>
69</li>
70<li>
71<p>File &gt; New &gt; Java Project</p>
72</li>
73<li>
74<p>Pick a project name, "android" or anything you like.</p>
75</li>
76<li>
77<p>Uncheck use default location, enter the path to your Android root directory, and click Finish.</p>
78</li>
79<li>
80<p>Wait while it sets up the project. (You'll see a subtle progress meter in the lower right corner.)</p>
81</li>
82</ol>
83<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>
84<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>
85<h3 id="when-you-sync">When You Sync</h3>
86<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>
87<ol>
88<li>
89<p>Window &gt; Show View &gt; Navigator</p>
90</li>
91<li>
92<p>In the Navigator, right-click on the project name</p>
93</li>
94<li>
95<p>Click Refresh in the context menu</p>
96</li>
97</ol>
98<h3 id="adding-apps-to-the-build-path">Adding Apps to the Build Path</h3>
99<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>
100<ol>
101<li>
102<p>Project &gt; Properties</p>
103</li>
104<li>
105<p>Select "Java Build Path" from the left-hand menu.</p>
106</li>
107<li>
108<p>Choose the "Source" tab.</p>
109</li>
110<li>
111<p>Click "Add Folder..."</p>
112</li>
113<li>
114<p>Add your app's <code>src</code> directory.</p>
115</li>
116<li>
117<p>Click OK.</p>
118</li>
119</ol>
120<p>When you're done, the "source folder" path in the list should look like </p>
121<pre><code>android/packages/apps/YOURAPP/src
122</code></pre>
123<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>
124<h2 id="eclipse-formatting">Eclipse formatting</h2>
125<p>You can import files in <code>development/ide/eclipse</code> to make Eclipse
126follow the Android style rules.</p>
127<ol>
128<li>
129<p>Select Window &gt; Preferences &gt; Java &gt; Code Style.</p>
130</li>
131<li>
132<p>Use Formatter &gt; Import to import <code>android-formatting.xml</code>.</p>
133</li>
134<li>
135<p>Organize Imports &gt; Import to import <code>android.importorder</code>.</p>
136</li>
137</ol>
138<h2 id="debugging-the-emulator-with-eclipse">Debugging the emulator with Eclipse</h2>
139<p>You can also use eclipse to debug the emulator and step through code. First, start the emulator running:</p>
140<pre><code>cd /path/to/android/root
141. build/envsetup.sh
142lunch 1
143make
144emulator
145</code></pre>
146<p>If the emulator is running, you should see a picture of a phone.</p>
147<p>In another shell, start DDMS (the Dalvik debug manager):</p>
148<pre><code>cd /path/to/android/root
149ddms
150</code></pre>
151<p>You should see a splufty debugging console.</p>
152<p>Now, in eclipse, you can attach to the emulator:</p>
153<ol>
154<li>
155<p>Run &gt; Open Debug Dialog...</p>
156</li>
157<li>
158<p>Right-click "Remote Java Application", select "New".</p>
159</li>
160<li>
161<p>Pick a name, i.e. "android-debug" or anything you like.</p>
162</li>
163<li>
164<p>Set the "Project" to your project name.</p>
165</li>
166<li>
167<p>Keep the Host set to "localhost", but change Port to 8700.</p>
168</li>
169<li>
170<p>Click the "Debug" button and you should be all set.</p>
171</li>
172</ol>
173<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>
174<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>
175<h2 id="bonus-material">Bonus material</h2>
176<p>Replace Ctrl with the Apple key on Mac.</p>
177<table>
178<thead>
179<tr>
180<th>shortcut</th>
181<th>function</th>
182</tr>
183</thead>
184<tbody>
185<tr>
186<td>Ctrl-Shift-o</td>
187<td>Organize imports</td>
188</tr>
189<tr>
190<td>Ctrl-Shift-t</td>
191<td>load class by name</td>
192</tr>
193<tr>
194<td>Ctrl-Shift-r</td>
195<td>load non-class resource by name</td>
196</tr>
197<tr>
198<td>Ctrl-1</td>
199<td>quick fix</td>
200</tr>
201<tr>
202<td>Ctrl-e</td>
203<td>Recently viewed files</td>
204</tr>
205<tr>
206<td>Ctrl-space</td>
207<td>auto complete</td>
208</tr>
209<tr>
210<td>Shift-Alt-r</td>
211<td>refactor:rename</td>
212</tr>
213<tr>
214<td>Shift-Alt-v</td>
215<td>refactor:move</td>
216</tr>
217</tbody>
218</table>
219<h2 id="eclipse-is-not-working-correctly-what-should-i-do">Eclipse is not working correctly, what should I do?</h2>
220<p>Make sure:</p>
221<ul>
222<li>
223<p>You followed the instructions on this page precisely.</p>
224</li>
225<li>
226<p>Your Problems view doesn't show any errors.</p>
227</li>
228<li>
229<p>Your application respects the package/directory structure.</p>
230</li>
231</ul>
232<p>If you're still having problems, please contact one of the <a
233href="{@docRoot}source/community/index.html">Android community mailing lists or
234IRC channels</a>.</p>
235