blob: 21f56189f781aeb8904ce270f1f0eb03f3df1d73 [file] [log] [blame] [view]
Skyler Kaufman991ae4d2011-04-07 12:30:41 -07001# Using Eclipse #
2
3This document will help you set up the Eclipse IDE for Android platform development.
4
5*Note: if you are looking for information on how to use
6Eclipse to develop applications that run on Android, this is not the right
7page for you. You probably would find [the Eclipse page on
8developer.android.com](http://developer.android.com/sdk/eclipse-adt.html) more useful.*
9
10## Basic setup ##
11
12First, it's important to make sure the regular Android development system is set up.
13
14 cd /path/to/android/root
15 make
16
17**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.
18
19Eclipse 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.
20
21 cd /path/to/android/root
22 cp development/ide/eclipse/.classpath .
23 chmod u+w .classpath
24
25Now edit that copy of `.classpath`, if necessary.
26
27### Increase Eclipse's Memory Settings ###
28
29The 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
30
31 /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini
32
33Memory-related defaults (as of Eclipse 3.4):
34
35 -Xms40m
36 -Xmx256m
37 -XX:MaxPermSize=256m
38
39Recommended settings for Android development:
40
41 -Xms128m
42 -Xmx512m
43 -XX:MaxPermSize=256m
44
45These 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.
46
47Now start Eclipse:
48
49 eclipse
50
51Now create a project for Android development:
52
531. If Eclipse asks you for a workspace location, choose the default.
54
552. If you have a "Welcome" screen, close it to reveal the Java perspective.
56
573. File > New > Java Project
58
594. Pick a project name, "android" or anything you like.
60
615. Select "Create project from existing source", enter the path to your Android root directory, and click Finish.
62
636. Wait while it sets up the project. (You'll see a subtle progress meter in the lower right corner.)
64
65Once 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.
66
67*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.
68
69### When You Sync ###
70
71Every time you repo sync, or otherwise change files outside of Eclipse (especially the .classpath), you need to refresh Eclipse's view of things:
72
731. Window > Show View > Navigator
74
751. In the Navigator, right-click on the project name
76
771. Click Refresh in the context menu
78
79### Adding Apps to the Build Path ###
80
81The 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:
82
831. Project > Properties
84
851. Select "Java Build Path" from the left-hand menu.
86
871. Choose the "Source" tab.
88
891. Click "Add Folder..."
90
911. Add your app's `src` directory.
92
931. Click OK.
94
95When you're done, the "source folder" path in the list should look like
96
97 android/packages/apps/YOURAPP/src
98
99Depending 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.
100
101## Eclipse formatting ##
102
103You can import files in `development/ide/eclipse` to make Eclipse
104follow the Android style rules.
105
1061. Select Window > Preferences > Java > Code Style.
107
1081. Use Formatter > Import to import `android-formatting.xml`.
109
1101. Organize Imports > Import to import `android.importorder`.
111
112## Debugging the emulator with Eclipse ##
113
114You can also use eclipse to debug the emulator and step through code. First, start the emulator running:
115
116 cd /path/to/android/root
117 . build/envsetup.sh
118 lunch 1
119 make
120 emulator
121
122If the emulator is running, you should see a picture of a phone.
123
124In another shell, start DDMS (the Dalvik debug manager):
125
126 cd /path/to/android/root
127 ddms
128
129You should see a splufty debugging console.
130
131Now, in eclipse, you can attach to the emulator:
132
1331. Run > Open Debug Dialog...
134
1351. Right-click "Remote Java Application", select "New".
136
1371. Pick a name, i.e. "android-debug" or anything you like.
138
1391. Set the "Project" to your project name.
140
1411. Keep the Host set to "localhost", but change Port to 8700.
142
1431. Click the "Debug" button and you should be all set.
144
145Note 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.
146
147You 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.
148
149## Bonus material ##
150
151Replace Ctrl with the Apple key on Mac.
152
153shortcut | function
154-------------|-----------------
155Ctrl-Shift-o | Organize imports
156Ctrl-Shift-t | load class by name
157Ctrl-Shift-r | load non-class resource by name
158Ctrl-1 | quick fix
159Ctrl-e | Recently viewed files
160Ctrl-space | auto complete
161Shift-Alt-r | refactor:rename
162Shift-Alt-v | refactor:move
163
164## Eclipse is not working correctly, what should I do? ##
165
166Make sure:
167
168- You followed the instructions on this page precisely.
169
170- Your Problems view doesn't show any errors.
171
172- Your application respects the package/directory structure.
173
174If you're still having problems, please contact one of the Android mailing lists or IRC channels.
175