blob: 56df7137794052375b8e0dd06f217755cf8bc268 [file] [log] [blame]
page.title=Using Eclipse
doc.type=source
@jd:body
<div>
<p>This document will help you set up the Eclipse IDE for Android platform development.</p>
<p><i>Note: if you are looking for information on how to use
Eclipse to develop applications that run on Android, this is not the right
page for you. You probably would find <a
href="http://developer.android.com/sdk/eclipse-adt.html">the Eclipse page on
developer.android.com</a> more useful.</i></p>
<h2><a>Enter eclipse</a>
</h2>
<h4>
Basic setup
</h4>
<p>First, it's important to make sure the regular Android development system is set up.
</p>
<pre>cd /path/to/android/root <br>make # and wait a while, if you haven't done this <br></pre>
<p><b>Important</b>
: You will still be using "make" 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 "make" in a shell. The Eclipse build is just for error checking.
</p>
<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 .classpath file. We have a sample version to start you off.
</p>
<pre>cd /path/to/android/root <br>cp development/ide/eclipse/.classpath .<br>chmod u+w .classpath # Make the copy writable <br></pre>
<p>Now edit that copy of .classpath, if necessary.
</p>
<h5>
Increase Eclipse's Memory Settings
</h5>
<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 the eclipse.ini file. On Apple OSX the eclipse.ini file is located at /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini
</p>
<p>Memory-related defaults (as of Eclipse 3.4)
</p>
<pre>-Xms40m <br>-Xmx256m <br>-XX:MaxPermSize=256m <br></pre>
<p>Recommended settings for Android development
</p>
<pre>-Xms128m <br>-Xmx512m <br>-XX:MaxPermSize=256m <br></pre>
<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>
<p>Now start Eclipse:
</p>
<pre>eclipse # or you can click some clicky thing instead, if you prefer <br></pre>
<p>Now create a project for Android development:
</p>
<ol><li>If Eclipse asks you for a workspace location, choose the default.
</li>
<li>If you have a "Welcome" screen, close it to reveal the Java perspective.
</li>
<li>File New Java Project
</li>
<li>Pick a project name, "android" or anything you like.
</li>
<li>Select "Create project from existing source", enter the path to your Android root directory, and click Finish.
</li>
<li>Wait while it sets up the project. (You'll see a subtle progress meter in the lower right corner.)
</li>
</ol>
<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>
<p><i><b>Note:</b>
</i>
Eclipse sometimes likes to add an "import android.R" 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>
<h4>
When you sync
</h4>
<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>
<ol><li>Window Show View Navigator
</li>
<li>In the "Navigator", right-click on the project ("android-java" or whatever you named it)
</li>
<li>Click Refresh in the context menu
</li>
</ol>
<h4>
Adding apps to the build path
</h4>
<p>The default .classpath 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>
<ol><li>Project Properties
</li>
<li>Select "Java Build Path" from the left-hand menu.
</li>
<li>Choose the "Source" tab.
</li>
<li>Click "Add Folder...".
</li>
<li>Add your app's "src" directory.
</li>
<li>Click OK.
</li>
</ol>
<p>When you're done, the "source folder" path in the list should look like android/packages/apps/<i>YourAppName</i>
/src. Depending on which app(s) you include, you may also need to include othersrc/main/java directories under android/dalvik/libcore. Do this if you find you cannot build with the default set.
</p>
<h4>
Eclipse setup to work on developer tools
</h4>
<p>To work on Java developer tools, the principle is the same, except you specify /path/to/tool when using the option "Create project from existing source."
</p>
<p>Once the project is created, you need to set up the Java Build Path:
</p>
<ol><li>Select the project you just created.
</li>
<li>Project Properties
</li>
<li>Select "Java Build Path" from the left-hand menu.
</li>
<li>Choose the "Source" tab.
</li>
<li>Expand the single <i>toolname</i>
/src entry.
</li>
<li>Double click the "Excluded: (none)" item.
</li>
<li>Add to the excluded (bottom) list: "MakeFile" and "resources/".
</li>
<li>Close the dialog.
</li>
<li>Back in the "Source" tab, click "Add Folder...", and add <i>toolname</i>
/src/resources.
</li>
<li>Click OK.
</li>
</ol>
<h4>
Eclipse setup to work on DDMS <br></h4>
<p>For DDMS, you will need to make a project for
</p>
<ol><li>development/tools/ddms/libs/ddmlib
</li>
<li>development/tools/ddms/libs/ddmuilib
</li>
<li>development/tools/ddms/app
</li>
</ol>
<p>Each project will need to reference the ones before it ("ddmuilib" references "ddmlib", and "app" references both of those). To do this:
</p>
<ol><li>Make sure you have all 3 projects defined.
</li>
<li>Right click on a project, "Build Path" "Configure Build Path..."
</li>
<li>Choose the "Project" tab.
</li>
<li>Click "Add..." and check the required projects.
</li>
</ol>
<h2><a>Eclipse formatting</a>
</h2>
<p>You can import files in development/ide/eclipse to make Eclipse
follow the Android style rules. Select
"Window &rsaquo; Preferences &rsaquo; Java &rsaquo; Code Style".
Use "Formatter &rsaquo; Import" to import android-formatting.xml
and "Organize Imports &rsaquo; Import" to import android.importorder.
</p>
<h2><a>Debugging the emulator with Eclipse</a>
</h2>
<p>You can also use eclipse to debug the emulator and step through code. First, start the emulator running:
</p>
<pre>cd /path/to/android/root <br>. build/envsetup.sh <br>lunch 1 # to build the emulator <br>make # if you didn't already do this <br>emulator # you should see a GUI picture of a phone <br></pre>
<p>In another shell, start DDMS (the Dalvik debug manager):
</p>
<pre>cd /path/to/android/root <br>ddms # you should get a splufty debugging console <br></pre>
<p>Now, in eclipse, you can attach to the emulator:
</p>
<ol><li>Run Open Debug Dialog...
</li>
<li>Right-click "Remote Java Application", select "New".
</li>
<li>Pick a name, "android-debug" or anything you like.
</li>
<li>Set the "Project" to your project ("android-java" or whatever).
</li>
<li>Keep the "Host" set to "localhost", but change "Port" to 8700.
</li>
<li>Click the "Debug" button and you should be all set.
</li>
</ol>
<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>
<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>
<h2><a>Bonus material</a>
</h2>
<p>Replace Ctrl with the Apple key on Mac.
</p>
<pre>Ctrl-Shift-o = Organize imports <br>Ctrl-Shift-t = load class by name <br>Ctrl-Shift-r = load non-class resource by name <br>Ctrl-1 = quick fix <br>Ctrl-e = Recently viewed files <br>Ctrl-space = auto complete <br>Shift-Alt-r = refactor:rename <br>Shift-Alt-v = refactor:move <br></pre>
<h2><a>Useful Plugins</a>
</h2>
<p>Eclipse has a plugin architecture that enables third parties to extend the IDE. Here are some plugins that make Eclipse easier to use for writing Android software:
</p>
<ul><li><a href="http://andrei.gmxhome.de/anyedit/">AnyEdit</a>
- automatically fix whitespace issues when saving files. Can convert tabs to spaces, strip blanks at end-of-line, and ensure the last line of the file has an end-of-line character.
</li>
</ul>
<h2><a>"Eclipse is not working correctly, what should I do?"</a>
</h2>
<p>Make sure:
</p>
<ul><li>You followed the instructions on this page precisely.
</li>
<li>Your Problems view doesn't show any errors.
</li>
<li>Your application respects the package/directory structure.
</li>
</ul>
<p>If you're still having problems, please contact one of the Android mailing lists or IRC channels.
</p>
</div>