1 page.title=Using Eclipse 2 doc.type=source 3 @jd:body 4 <div> 5 <p>This document will help you set up the Eclipse IDE for Android platform development.</p> 6 <p><i>Note: if you are looking for information on how to use 7 Eclipse to develop applications that run on Android, this is not the right 8 page for you. You probably would find <a 9 href="http://developer.android.com/sdk/eclipse-adt.html">the Eclipse page on 10 developer.android.com</a> more useful.</i></p> 11 <h2><a>Enter eclipse</a> 12 </h2> 13 <h4> 14 Basic setup 15 </h4> 16 <p>First, it's important to make sure the regular Android development system is set up. 17 </p> 18 <pre>cd /path/to/android/root <br>make # and wait a while, if you haven't done this <br></pre> 19 <p><b>Important</b> 20 : 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. 21 </p> 22 <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. 23 </p> 24 <pre>cd /path/to/android/root <br>cp development/ide/eclipse/.classpath .<br>chmod u+w .classpath # Make the copy writable <br></pre> 25 <p>Now edit that copy of .classpath, if necessary. 26 </p> 27 <h5> 28 Increase Eclipse's Memory Settings 29 </h5> 30 <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 31 </p> 32 <p>Memory-related defaults (as of Eclipse 3.4) 33 </p> 34 35 <pre>-Xms40m <br>-Xmx256m <br>-XX:MaxPermSize=256m <br></pre> 36 <p>Recommended settings for Android development 37 </p> 38 <pre>-Xms128m <br>-Xmx512m <br>-XX:MaxPermSize=256m <br></pre> 39 <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. 40 </p> 41 <p>Now start Eclipse: 42 </p> 43 <pre>eclipse # or you can click some clicky thing instead, if you prefer <br></pre> 44 <p>Now create a project for Android development: 45 </p> 46 <ol><li>If Eclipse asks you for a workspace location, choose the default. 47 </li> 48 <li>If you have a "Welcome" screen, close it to reveal the Java perspective. 49 </li> 50 <li>File New Java Project 51 </li> 52 <li>Pick a project name, "android" or anything you like. 53 </li> 54 <li>Select "Create project from existing source", enter the path to your Android root directory, and click Finish. 55 </li> 56 <li>Wait while it sets up the project. (You'll see a subtle progress meter in the lower right corner.) 57 </li> 58 </ol> 59 <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. 60 </p> 61 <p><i><b>Note:</b> 62 </i> 63 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. 64 </p> 65 <h4> 66 When you sync 67 </h4> 68 <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: 69 </p> 70 <ol><li>Window Show View Navigator 71 </li> 72 <li>In the "Navigator", right-click on the project ("android-java" or whatever you named it) 73 </li> 74 <li>Click Refresh in the context menu 75 </li> 76 </ol> 77 <h4> 78 Adding apps to the build path 79 </h4> 80 <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: 81 </p> 82 <ol><li>Project Properties 83 </li> 84 <li>Select "Java Build Path" from the left-hand menu. 85 </li> 86 <li>Choose the "Source" tab. 87 </li> 88 <li>Click "Add Folder...". 89 </li> 90 <li>Add your app's "src" directory. 91 </li> 92 <li>Click OK. 93 </li> 94 </ol> 95 <p>When you're done, the "source folder" path in the list should look like android/packages/apps/<i>YourAppName</i> 96 /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. 97 </p> 98 <h2><a>Eclipse formatting</a> 99 </h2> 100 <p>You can import files in development/ide/eclipse to make Eclipse 101 follow the Android style rules. Select 102 "Window › Preferences › Java › Code Style". 103 Use "Formatter › Import" to import android-formatting.xml 104 and "Organize Imports › Import" to import android.importorder. 105 </p> 106 <h2><a>Debugging the emulator with Eclipse</a> 107 </h2> 108 <p>You can also use eclipse to debug the emulator and step through code. First, start the emulator running: 109 </p> 110 <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> 111 <p>In another shell, start DDMS (the Dalvik debug manager): 112 </p> 113 <pre>cd /path/to/android/root <br>ddms # you should get a splufty debugging console <br></pre> 114 <p>Now, in eclipse, you can attach to the emulator: 115 </p> 116 <ol><li>Run Open Debug Dialog... 117 </li> 118 <li>Right-click "Remote Java Application", select "New". 119 </li> 120 <li>Pick a name, "android-debug" or anything you like. 121 </li> 122 <li>Set the "Project" to your project ("android-java" or whatever). 123 </li> 124 <li>Keep the "Host" set to "localhost", but change "Port" to 8700. 125 </li> 126 <li>Click the "Debug" button and you should be all set. 127 </li> 128 </ol> 129 <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. 130 </p> 131 <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. 132 </p> 133 <h2><a>Bonus material</a> 134 </h2> 135 <p>Replace Ctrl with the Apple key on Mac. 136 </p> 137 138 <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> 139 140 <h2><a>"Eclipse is not working correctly, what should I do?"</a> 141 </h2> 142 <p>Make sure: 143 </p> 144 <ul><li>You followed the instructions on this page precisely. 145 </li> 146 <li>Your Problems view doesn't show any errors. 147 </li> 148 <li>Your application respects the package/directory structure. 149 </li> 150 </ul> 151 <p>If you're still having problems, please contact one of the Android mailing lists or IRC channels. 152 </p> 153 </div> 154