Android Using Eclipse
来源:互联网 发布:模拟人生4mac怎么放mod 编辑:程序博客网 时间:2024/05/21 14:59
Using Eclipse (Android Open Source Project)
Using Eclipse
About this Document
This document will help you set up the Eclipse IDE for Android platform development.
Enter eclipse
Basic setup
First, it's important to make sure the regular Android development systemis set up.
cd /path/to/android/root
make # and wait a while, if you haven't done this
Important: You will still be using "make" to buildthe files you will actually run (in the emulator or on a device). You willbe using Eclipse to edit files and verify that they compile, but when you wantto run something you will need to make sure files are saved in Eclipse andrun "make" in a shell. The Eclipse build is just for error checking.
Eclipse needs a list of directories to search for Java files. This iscalled the "Java Build Path" and can be set with the .classpath file.We have a sample version to start you off.
cd /path/to/android/root
cp development/ide/eclipse/.classpath .
chmod u+w .classpath # Make the copy writable
Now edit that copy of .classpath, if necessary.
Increase Eclipse's Memory Settings
The Android project is large enough that Eclipse's Java VM sometimes runs out of memory whilecompiling it. Avoid this problem by editing the the eclipse.ini
file. On Apple OSX theeclipse.ini
file is located at/Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini
Memory-related defaults (as of Eclipse 3.4)
-Xms40m
-Xmx256m
-XX:MaxPermSize=256m
Recommended settings for Android development
-Xms128m
-Xmx512m
-XX:MaxPermSize=256m
These settings set Eclipse's minimum Java heap size to 128MB, set the maximum Java heap size to512MB, and keep the maximum permanent generation size at the default of 256MB.
Now start Eclipse:
eclipse # or you can click some clicky thing instead, if you prefer
Now create a project for Android development:
- If Eclipse asks you for a workspace location, choose the default.
- If you have a "Welcome" screen, close it to reveal the Java perspective.
- File > New > Java Project
- Pick a project name, "android" or anything you like.
- Select "Create project from existing source", enter the path to your Android root directory, and click Finish.
- Wait while it sets up the project. (You'll see a subtle progress meter in the lower right corner.)
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.
Note: Eclipse sometimes likes to addan "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 willcause your make to break. Look out for these erroneous import statements anddelete them.
When you sync
Every time you repo sync, or otherwise change files outside of Eclipse(especially the .classpath), you need to refresh Eclipse's view of things:
- Window > Show View > Navigator
- In the "Navigator", right-click on the project ("android-java" or whatever you named it)
- Click Refresh in the context menu
Adding apps to the build path
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:
- Project > Properties
- Select "Java Build Path" from the left-hand menu.
- Choose the "Source" tab.
- Click "Add Folder...".
- Add your app's "src" directory.
- Click OK.
When you're done, the "source folder" path in the list should look likeandroid/packages/apps/YourAppName/src. Depending on which app(s)you include, you may also need to include other src/main/java directories under android/dalvik/libcore. Do this if you findyou cannot build with the default set.
Eclipse setup to work on developer tools
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."
Once the project is created, you need to set up the Java Build Path:
- Select the project you just created.
- Project > Properties
- Select "Java Build Path" from the left-hand menu.
- Choose the "Source" tab.
- Expand the single toolname/src entry.
- Double click the "Excluded: (none)" item.
- Add to the excluded (bottom) list: "MakeFile" and "resources/".
- Close the dialog.
- Back in the "Source" tab, click "Add Folder...", and addtoolname/src/resources.
- Click OK.
Eclipse setup to work on DDMS
For DDMS, you will need to make a project for
- development/tools/ddms/libs/ddmlib
- development/tools/ddms/libs/ddmuilib
- development/tools/ddms/app
Each project will need to reference the ones before it ("ddmuilib" references "ddmlib", and "app" references both of those). To do this:
- Make sure you have all 3 projects defined.
- Right click on a project, "Build Path" > "Configure Build Path..."
- Choose the "Project" tab.
- Click "Add..." and check the required projects.
Eclipse formatting
You can use a preference file in Eclipse to automatically set the formatter to the Android style guide. Import the files android-formatting.xml and android.importorder, which live under development/ide/eclipse.
Debugging the emulator with Eclipse
You can also use eclipse to debug the emulator and step through code.First, start the emulator running:
cd /path/to/android/root
. build/envsetup.sh
lunch 1 # to build the emulator
make # if you didn't already do this
emulator # you should see a GUI picture of a phone
In another shell, start DDMS (the Dalvik debug manager):
cd /path/to/android/root
ddms # you should get a splufty debugging console
Now, in eclipse, you can attach to the emulator:
- Run > Open Debug Dialog...
- Right-click "Remote Java Application", select "New".
- Pick a name, "android-debug" or anything you like.
- Set the "Project" to your project ("android-java" or whatever).
- Keep the "Host" set to "localhost", but change "Port" to 8700.
- Click the "Debug" button and you should be all set.
Note that port 8700 is attached to whatever process is currently selectedin the DDMS console, so you need to sure that DDMS has selected the process you want to debug.
You may need to open the Debug perspective (next to the "Java" perspectiveicon 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 oneand break it (by clicking the "pause" icon), it should show the stack trace,source file, and line where execution is at. Breakpoints and whatnot shouldall work.
Bonus material
Replace Ctrl with the Apple key on Mac.
Ctrl-Shift-o = Organize imports
Ctrl-Shift-t = load class by name
Ctrl-Shift-r = load non-class resource by name
Ctrl-1 = quick fix
Ctrl-e = Recently viewed files
Ctrl-space = auto complete
Shift-Alt-r = refactor:rename
Shift-Alt-v = refactor:move
Useful Plugins
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:
- AnyEdit - automatically fix whitespace issueswhen saving files. Can convert tabs to spaces, strip blanks at end-of-line, andensure the last line of the file has an end-of-line character.
"Eclipse is not working correctly, what should I do?"
Make sure:
- You followed the instructions on this page precisely.
- Your Problems view doesn't show any errors.
- Your application respects the package/directory structure.
If you're still having problems, please contact one of the Android mailing lists or IRC channels.
- Android Using Eclipse
- Using Eclipse to compile Android source code
- Using Android monkeyrunner from Eclipse in Windows
- Using Eclipse to read Android Framework code.
- Debugging Android Java framework services using Eclipse
- Using Eclipse
- "invalid command-line parameter: Files\android" when using eclipse ADT
- [Android+Development+Build+Environment+Step+3]Using Eclipse
- Using Android monkeyrunner from Eclipse, both in Windows and Linux!
- Using Android monkeyrunner from Eclipse, both in Windows and Linux!
- Using Subversion in Eclipse
- Java Preprocessor Using Eclipse
- tips of using eclipse
- Using Eclipse With CloudStack
- Developing SWT applications using Eclipse
- Using Eclipse with wxWidgets libraries
- Developing SWT applications using Eclipse
- Using Eclipse Metrics plug-in
- 设置窗口标题栏左上角图标
- MySQL不能启动服务1067错误
- 制作任何应用都可以安装的扩展(火狐,雷鸟...自己写的也可以)
- 模拟鼠标点击
- 中国佛学66句震撼世界的禅语
- Android Using Eclipse
- 关于 Web Service 的一些总结,一些理解
- 列主元消元法
- 美国论坛这样评价中国人,大家认为呢
- 更改mozilla安装扩展的倒计时时间
- 让thunderbird显示flash配置
- Perl 生成随机字符串
- 只允许启动一个相同的窗口
- C++中delete, new以及new [], delete[]操作符内幕