`

Android Dev - Using Eclipse

 
阅读更多

Basic setup

First, it's important to make sure the regular Android development system is set up.

cd /path/to/android/root 
make

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.

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.

cd /path/to/android/root 
cp development/ide/eclipse/.classpath .
chmod u+w .classpath

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 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

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 to 512MB, and keep the maximum permanent generation size at the default of 256MB.

Now start Eclipse:

eclipse

Now create a project for Android development:

  1. If Eclipse asks you for a workspace location, choose the default.

  2. If you have a "Welcome" screen, close it to reveal the Java perspective.

  3. File > New > Java Project

  4. Pick a project name, "android" or anything you like.

  5. Select "Create project from existing source", enter the path to your Android root directory, and click Finish.

  6. 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 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.

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:

  1. Window > Show View > Navigator

  2. In the Navigator, right-click on the project name

  3. 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:

  1. Project > Properties

  2. Select "Java Build Path" from the left-hand menu.

  3. Choose the "Source" tab.

  4. Click "Add Folder..."

  5. Add your app's src directory.

  6. Click OK.

When you're done, the "source folder" path in the list should look like

android/packages/apps/YOURAPP/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.

Eclipse formatting

You can import files in development/ide/eclipse to make Eclipse follow the Android style rules.

  1. Select Window > Preferences > Java > Code Style.

  2. Use Formatter > Import to import android-formatting.xml.

  3. Organize Imports > Import to import android.importorder.

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    
make       
emulator

If the emulator is running, you should see a picture of a phone.

In another shell, start DDMS (the Dalvik debug manager):

cd /path/to/android/root 
ddms

You should see a splufty debugging console.

Now, in eclipse, you can attach to the emulator:

  1. Run > Open Debug Dialog...

  2. Right-click "Remote Java Application", select "New".

  3. Pick a name, i.e. "android-debug" or anything you like.

  4. Set the "Project" to your project name.

  5. Keep the Host set to "localhost", but change Port to 8700.

  6. Click the "Debug" button and you should be all set.

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.

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.

Bonus material

Replace Ctrl with the Apple key on Mac.

shortcut function
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

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-android-dev-bookmarks.zip

    Android-android-dev-bookmarks.zip,android开发书签(adb)-android用于cn dev now:https://rocko.xyz/categories/android-dev-bookmarks/;android-dev-bookmarks/;android:https://tinyletter.com/rocko:,安卓...

    Dev-Cpp 5.11 TDM-GCC 4.9.2

    软件:Dev-Cpp 5.11 TDM-GCC 4.9.2 这个软件我相信大部分人都认识 Dev-C++是一款C++开发工具 Dev C++也是一个Windows下的C和C++程序的集成开发环境 但大家应该遇到这个问题 在访问Dev-Cpp下载的网站: ...

    Dev-Cpp-5.11-TDM-GCC-4.9.2-Setup.exe

    标题中的"Dev-Cpp-5.11-TDM-GCC-4.9.2-Setup.exe"和描述中的"Dev-Cpp_5.11_TDM-GCC_4.9.2_Setup.exe"指向的是同一款软件的安装程序,名为Dev-Cpp,这是一个集成开发环境(IDE),专门用于编写C++语言的程序。...

    Dev-Cpp-5.16d.exe

    Dev-C++主页: https://devcpp.gitee.io Dev-C++ (或者叫做 Dev-Cpp)是 Windows 环境下的一个用于 C 语言和 C++ 语言程序设计的轻量级免费集成开发环境。 它集成了 SynEdit 源码编辑器、MinGW-w64 编译器、GDB 调试...

    Dev-Cpp 5.11版本(Dev-c++)安装包

    Dev-Cpp 5.11 是一款经典的C和C++集成开发环境,也被称为Dev-C++。这款轻量级的IDE特别适合初学者和专业开发者使用,它集成了编译器、调试器和其他必要的工具,使得C和C++程序开发更为便捷。在2020年5月发布的这个...

    webpack-使用webpack-dev-server.rar

    `webpack-dev-server` 是 Webpack 提供的一个本地开发服务器,它极大地提高了前端开发效率,因为它提供了实时重载(live reloading)和热模块替换(hot module replacement)等功能。 **实时重载(Live Reloading)...

    Dev-Cpp 5.11 TDM-GCC 4.9.2 Setup.zip 2024年更新

    Dev-Cpp 5.11 TDM-GCC 4.9.2 是一款专为Windows用户设计的强大C/C++集成开发环境(IDE),它在2024年进行了更新,以提供最新的功能和优化。这款软件是编程爱好者和专业开发者进行C++编程的理想选择,因为它集成了...

    Embarcadero 最新 Dev-Cpp 6.3版 包含TDM-GCC_9.2

    Dev-Cpp是由Embarcadero公司提供的一款集成开发环境(IDE),特别针对C++编程语言。这个6.3版本是该软件的最新迭代,旨在为开发者提供一个高效、易用的平台来编写、编译和调试C++代码。Embarcadero在软件行业内有着...

    Dev-Cpp_5.11_TDM-GCC_4.9.2_Setup.exe

    《Dev-C++:一款强大的C++集成开发环境》 Dev-C++是一款经典的开源C++集成开发环境(IDE),尤其适合初学者和专业人士进行C++编程。这个标题"Dev-Cpp_5.11_TDM-GCC_4.9.2_Setup.exe"暗示的是Dev-C++的一个特定版本...

    Dev-cpp 6.0

    Dev-cpp 6.0 Dev-C++是一个C&C++开发工具,它是一款自由软件,遵守GPL协议。它集合了GCC、MinGW等众多自由软件,并且可以取得最新版本的各种工具支持,而这一切工作都是来自全球的狂热者所做的工作,并且你拥有对这...

    dev-sidecar.rar

    标题中的"dev-sidecar.rar"指的是一个压缩文件,其核心是一个名为"dev-sidecar"的程序或工具。根据描述,这个工具被设计为一个“上网助手”,特别针对开发者,能够加速对GitHub的访问,并解决在浏览过程中可能出现的...

    Dev-Cpp v5.11 TDM-GCC 4.9.2 Setup.exe.zip

    《Dev-Cpp v5.11 TDM-GCC 4.9.2:一款轻量级C++集成开发环境》 Dev-Cpp是一款经典且小巧的C++集成开发环境(IDE),尤其适合初学者和对资源占用有严格要求的开发者。这个版本,即Dev-Cpp v5.11,内置了TDM-GCC ...

    Dev-Cpp 5.9.2 TDM-GCC 4.8.1 Setup

    devc 安装包 Dev-Cpp 5.9.2 TDM-GCC 4.8.1 Setup

    Dev-Cpp 5.4.0 MinGW 4.7.2 Setup

    **Dev-Cpp 5.4.0 MinGW 4.7.2 Setup** 是一个针对C和C++编程语言的集成开发环境(IDE),专为初学者和专业开发者设计。这个版本结合了Dev-Cpp IDE与MinGW编译器套件,其中MinGW是Minimalist GNU for Windows的缩写,...

    Dev-Cpp 5.4.0编程软件C++下载C语言免费版

    Dev-Cpp是一款经典的C++集成开发环境(IDE),特别适合初学者和专业开发者进行C/C++编程。这个软件是开源的,因此它是免费的,同时因其用户友好的界面和丰富的功能,深受许多程序员喜爱,特别是在蓝桥杯等编程竞赛中...

    Dev-C++ 5.11最新版本

    相比于其他大型IDE,如Visual Studio或Eclipse,Dev-C++的安装文件小,占用系统资源少,可以在各种配置的计算机上流畅运行,这对于那些资源有限的用户来说是一个巨大的优势。 该软件集成了TDM-GCC编译器,这是GCC...

    Ubuntu dev-sidecar deb安装包

    Ubuntu dev-sidecar deb安装包 DevSidecar-1.7.3.deb

    Dev-Cpp 5.6.0 MinGW 4.8.1 Setup.exe

    Dev-Cpp 5.6.0 MinGW 4.8.1 Setup.exe Dev-Cpp 5.6.0 MinGW 4.8.1 Setup.exe Dev-Cpp 5.6.0 MinGW 4.8.1 Setup.exe Dev-Cpp 5.6.0 MinGW 4.8.1 Setup.exe

    gtk-dev-2.12.9-win32-2

    标题中的“gtk-dev-2.12.9-win32-2”指的是GTK+开发工具包的一个特定版本,适用于Windows 32位操作系统。GTK+(GIMP Toolkit)是一个跨平台的图形用户界面(GUI)工具包,广泛用于创建美观且功能丰富的应用程序,...

    Dev-Cpp-5.16i编译器

    Dev-C++是一个Windows环境下C&C++开发工具,它是一款自由软件,遵守GPL协议。它集合了GCC、MinGW32等众多自由软件,并且可以取得最新版本的各种工具支持。Dev-C++是一个非常实用的编程软件,多款著名软件均由它编写...

Global site tag (gtag.js) - Google Analytics