1<!-- 2 Copyright 2010 The Android Open Source Project 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15--> 16 17# Using Eclipse # 18 19This document will help you set up the Eclipse IDE for Android platform development. 20 21*Note: if you are looking for information on how to use 22Eclipse to develop applications that run on Android, this is not the right 23page for you. You probably would find [the Eclipse page on 24developer.android.com](http://developer.android.com/sdk/eclipse-adt.html) more useful.* 25 26## Basic setup ## 27 28First, it's important to make sure the regular Android development system is set up. 29 30 cd /path/to/android/root 31 make 32 33**Important**: 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. 34 35Eclipse 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. 36 37 cd /path/to/android/root 38 cp development/ide/eclipse/.classpath . 39 chmod u+w .classpath 40 41Now edit that copy of `.classpath`, if necessary. 42 43### Increase Eclipse's Memory Settings ### 44 45The 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 46 47 /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini 48 49Memory-related defaults (as of Eclipse 3.4): 50 51 -Xms40m 52 -Xmx256m 53 -XX:MaxPermSize=256m 54 55Recommended settings for Android development: 56 57 -Xms128m 58 -Xmx512m 59 -XX:MaxPermSize=256m 60 61These 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. 62 63Now start Eclipse: 64 65 eclipse 66 67Now create a project for Android development: 68 691. If Eclipse asks you for a workspace location, choose the default. 70 712. If you have a "Welcome" screen, close it to reveal the Java perspective. 72 733. File > New > Java Project 74 754. Pick a project name, "android" or anything you like. 76 775. Select "Create project from existing source", enter the path to your Android root directory, and click Finish. 78 796. Wait while it sets up the project. (You'll see a subtle progress meter in the lower right corner.) 80 81Once 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. 82 83*Note:* 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. 84 85### When You Sync ### 86 87Every time you repo sync, or otherwise change files outside of Eclipse (especially the .classpath), you need to refresh Eclipse's view of things: 88 891. Window > Show View > Navigator 90 911. In the Navigator, right-click on the project name 92 931. Click Refresh in the context menu 94 95### Adding Apps to the Build Path ### 96 97The 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: 98 991. Project > Properties 100 1011. Select "Java Build Path" from the left-hand menu. 102 1031. Choose the "Source" tab. 104 1051. Click "Add Folder..." 106 1071. Add your app's `src` directory. 108 1091. Click OK. 110 111When you're done, the "source folder" path in the list should look like 112 113 android/packages/apps/YOURAPP/src 114 115Depending 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. 116 117## Eclipse formatting ## 118 119You can import files in `development/ide/eclipse` to make Eclipse 120follow the Android style rules. 121 1221. Select Window > Preferences > Java > Code Style. 123 1241. Use Formatter > Import to import `android-formatting.xml`. 125 1261. Organize Imports > Import to import `android.importorder`. 127 128## Debugging the emulator with Eclipse ## 129 130You can also use eclipse to debug the emulator and step through code. First, start the emulator running: 131 132 cd /path/to/android/root 133 . build/envsetup.sh 134 lunch 1 135 make 136 emulator 137 138If the emulator is running, you should see a picture of a phone. 139 140In another shell, start DDMS (the Dalvik debug manager): 141 142 cd /path/to/android/root 143 ddms 144 145You should see a splufty debugging console. 146 147Now, in eclipse, you can attach to the emulator: 148 1491. Run > Open Debug Dialog... 150 1511. Right-click "Remote Java Application", select "New". 152 1531. Pick a name, i.e. "android-debug" or anything you like. 154 1551. Set the "Project" to your project name. 156 1571. Keep the Host set to "localhost", but change Port to 8700. 158 1591. Click the "Debug" button and you should be all set. 160 161Note 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. 162 163You 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. 164 165## Bonus material ## 166 167Replace Ctrl with the Apple key on Mac. 168 169shortcut | function 170-------------|----------------- 171Ctrl-Shift-o | Organize imports 172Ctrl-Shift-t | load class by name 173Ctrl-Shift-r | load non-class resource by name 174Ctrl-1 | quick fix 175Ctrl-e | Recently viewed files 176Ctrl-space | auto complete 177Shift-Alt-r | refactor:rename 178Shift-Alt-v | refactor:move 179 180## Eclipse is not working correctly, what should I do? ## 181 182Make sure: 183 184- You followed the instructions on this page precisely. 185 186- Your Problems view doesn't show any errors. 187 188- Your application respects the package/directory structure. 189 190If you're still having problems, please contact one of the Android mailing lists or IRC channels. 191 192