【Android源码 栏目提醒】:网学会员鉴于大家对Android源码 十分关注,论文会员在此为大家搜集整理了“使用eclipse来开发android源码 - 电子设计”一文,供大家参考学习
一、怎样使用eclipse来开发
android源码 ............................................................................... 1 1. android的文件系统结构是怎样的我们安装的程序放在那里 .............................. 1 2. android SDK和
android源码能为我们提供什么工具 ................................................ 1 3. 我们在开发时应该使用那些工具和android文件系统呢......................................... 2 4. 怎样修改、编译、调试
android源码呢Google又是用什么工具来开发android的 2 二、Android开发环境建立以及开发工具的使用 ......................................................................... 3 1. android SDK和eclipse的安装以及android开发环境建立 .......................................... 3 2. 使用eclipse来开发
android源码 ................................................................................... 3 3. 具体方法 ........................................................................................................................... 3 1 建立基本的android开发环境 ................................................................................ 3 2 编译
android源码 .................................................................................................... 3 3 把eclipse工程配置文件复制到
android源码根目录下 ....................................... 3 4 修改eclipse程序的配置 .......................................................................................... 4 5 把
android源码作为一个工程导入eclipse ............................................................ 4 6 eclipse上调试android里的程序。
........................................................................ 5 7 编译
android源码 .................................................................................................... 5 8 如何开发自己的工程 ............................................................................................... 6 三、 set up the Eclipse IDE for Android platform development ...................................................... 8 一、怎样使用eclipse来开发
android源码 android提供的工具链和开发工具比较完善因此它的开发环境的搭建比较简单相信许多朋友都已经搭建好环境并编写了HelloActivity入门程序了。
这里先看几个问题 1. android的文件系统结构是怎样的我们安装的程序放在那里 编译
android源码之后在out/target/product/generic一些文件 ramdisk.img、system.img、userdata.img、 system、 data、root 其中 system.img是由 system打包压缩得到的 userdata.img是由 data打包压缩得到的。
ramdisk.img是模拟器的文件系统把ramdisk.img解压出来可知道ramdisk.img里的文件跟root文件夹的文件基本一样。
模拟器装载ramdisk.img并解压到内存接着分别把system.img和userdata.img挂载到 ramdisk下的system和data目录。
我们编译出来的应用程序就是放在system/app下的。
用户安装的程序则是放在data/app下。
2. android SDK和
android源码能为我们提供什么工具 android SDK提供有很多工具如adbddmsemulatoraapt等并提供kernel-qemu、ramdisk.img、 system.img、userdata.img。
因此只要有android SDK我们就可以在模拟器上把android跑起来。
android源码可以编译出android SDK、adb等工具、android文件系统以及ADT插件也就是说我们可以从
android源码编译出所有android相关的东西。
3. 我们在开发时应该使用那些工具和android文件系统呢 把
android源码“make”之后会生成许多工具和android文件系统system.img等我们又可以使用“make sdk”来生成android SDKandroid SDK也包括有工具和android文件系统system.img等而原来安装的时候我们也安装了android SDK那么我们在开发时应该使用那些工具和android文件系统呢 这个问题在后面回答。
4. 怎样修改、编译、调试
android源码呢Google又是用什么工具来开发android的 官方推荐我们使用 eclipseadt进入开发应用程序我们的HelloActivity程序也是这里开发的。
当我们把
android源码/packages /apps/下的工程导入eclipse时一般都会出现找不到包的错误。
那么我们怎样修改、编译、调试
android源码呢Google又是用什么工具来开发android的 这个问题在后面回答。
二、Android开发环境建立以及开发工具的使用 1. android SDK和eclipse的安装以及android开发环境建立 android工具链比较完善需要外部的工具比较少。
具体的安装过程可参考官方文档或ltltandroid模拟器在ubuntu8.10的安装gtgt和ltltandroid源码的编译gtgt。
这里需要注意的是用“make”来编译
android源码时我们可以使用JDK5或JDK6用“make sdk”来编译时会用到javadoc来生成文档javadoc就必须使用JDK5的javadoc否则编译是通不过的。
因此我们可以把JDK5 和JDK6都装上然后只把javadoc和javadoc.1.gz指向JDK5相应的工具其它工具还是用JDK6的。
当然我们可以只安装JDK5 或只用JDK5的工具。
具体操作可以参考ltltandroid源码的编译gtgt 2. 使用eclipse来开发
android源码 这里主要参考官方文档 https://sites.google.com/a/android.com/opensource/using-eclipse 详见第三部分 3. 具体方法 下面从官方文档总结出具体怎样用eclipse来开发
android源码在linuxUbuntu系统下 1 建立基本的android开发环境 请参考官方文档或ltltandroid模拟器在ubuntu8.10的安装gtgt 2 编译
android源码 android源码根目录下通过“make”进行编译请注意一些配置具体可参考ltltandroid源码的编译gtgt 3 把eclipse工程配置文件复制到
android源码根目录下 进入
android源码根目录 cp development/ide/eclipse/.classpath ./ chmod uw .classpath Make the copy writable .classpath文件是导入包的路径 4 修改eclipse程序的配置 1 增大eclipse内存设置 把eclipse.ini在eclipse软件的安装根目录下的3个值改为下面的值 -Xms128m -Xmx512m -XX:MaxPermSize256m 2 把android-formatting.xml和android.importorder导入eclipse可选 android-formatting.xml、.classpath和android.importorder都放在development/ide/eclipse/下。
android-formatting.xml用来配置eclipse编辑器的代码风格 android.importorder用来配置eclipse的import的顺序和结构。
具体导入eclipse方法 在window-gtpreferences-gtjava-gtCode style-gtFormatter中导入android-formatting.xml 在window-gtpreferences-gtjava-gtCode style-gtOrganize Imports中导入android.importorder 3 安装anyedit插件可选 在http://andrei.gmxhome.de/anyedit/下载并导入eclipse中 5 把
android源码作为一个工程导入eclipse 导入前先检查.classpath里的文件在
android源码中是否有相应的文件文件夹否则也会破坏
android源码一般是多添加文件/文件夹.classpath里多余的路径可删除。
新建Java Project不是android project否则会破坏
android源码工程名任意选择从已存在的工程导入即选择quotCreate project from existing sourcequotenter the path to your Android root directory点击完成。
导入时eclipse要build工程比较慢。
导完后一般都没有错误。
If necessary uncheck and re-check Project gt Build Automatically to force a rebuild. 这里也就回答了第4个问题。
官方文档为 Now create a project for Android development: 1. If Eclipse asks you for a workspace location choose the default. 2. If you have a quotWelcomequot screen close it to reveal the Java perspective. 3. File gt New gt Java Project 4. Pick a project name quotandroidquot or anything you like. 5. Select quotCreate project from existing sourcequot enter the path to your Android root directory and click Finish. 6. Wait while it sets up the project. Youll see a subtle progress meter in the lower right corner. 6 eclipse上调试android里的程序。
为了不让其它版本的android工具和android文件系统影响下面的编译和调试需要从环境变量中去除android工具和android文件系统的路径 vim /.bashrc export PATHPATH:/usr/local/arm-2008q3/bin/ export JAVA_HOME/usr/lib/jvm/java-5-sun export ANDROID_JAVA_HOMEJAVA_HOME export PATHHOME/android/out/host/linux-x86/bin:PATH export ANDROID_PRODUCT_OUTHOME/android/out/target/product/generic 看看有没有在PATH变量中加入android工具和android文件系统的路径如果加有则注释它。
通过下面的方法我们是不需要在.bashrc中添加android工具和android文件系统的路径的。
执行 1. cd ltandroid源码目录gt 2. . build/envsetup.sh 设了环境变量之后会多出mmm等命令可以通过输入help来查看 3. lunch 1 把emulator等工具和ramdisk.img等文件的路径对应起来就可以直接调用emulator等工具也解决了第3个问题 4. emulator amp 5. ddms amp 注意先启动ddms再启动eclipse这样eclipse中就不会说端口冲突。
然后在eclipse中配置调试类型和端口 在Run-gtDebug Configurations-gtRemote java application上双击然后“Host:”设为localhost“Port:”设为8800“Connection Type”为StandardSocket Attach然后“Apply”。
注意上面设置的端口要与DDMS中设置的端口一致ADT插件使用了8700端口因此上面设置的端口是8800。
如果出现连不到VM的错误时请注意要先在DDMS中选中某一进程对应某一应用程序才能在eclipse执行 Debug。
在eclipse调试时可以设断点、单步调试。
估计google团队也是这样开发、调试android应用程序的。
需要安装ADT插件吗 需要安装Android SDK吗 7 编译
android源码 执行 cd ltandroid源码目录gt . build/envsetup.sh 那么就会多出mm/mmm等命令mm/mmm用来编译模块包括C、C、JAVA程序。
我们也可以直接在
android源码根目录下执行“make 模块名”来编译模块模块名可以在.mk文件中找到。
模块编译后会在out/target/product/generic/system/app下生成对应的.apk包。
但是用mm/mmm来编译生成的.apk并不会打包到system.img中需要我们手动通过make snod把 system文件夹打包为system.img不过这就得重新运行模拟器了这也是很麻烦了。
对于我们开发者来说我们可以这样做 1把需要修改、调试的模块比如AlarmClock.apk从out/target/product/generic/system/app下移除然后make snod这样system.img就没有AlarmClock.apk了。
2运行模拟器就看不到AlarmClock了 3修改AlarmClock源码并用mm/mmm来编译在out/target/product/generic/system/app下生成AlarmClock.apk 4通过adb把AlarmClock.apk安装到android文件系统中安装方法有两个 A、通过adb install xxx/AlarmClock.apk B、通过adb push xxx/AlarmClock.apk /data/app 两种方法都可以把 AlarmClock安装到/data/app下android会自动把它显示在主菜单中只要AlarmClock.apk中有一Activity包含android.intent.category.LAUNCHER属性不过A方法在/data/app生成 com.android.alarmclock.apkB方法则是 AlarmClock.apk。
用A方法时如果原来已经安装了 AlarmClock你还得先adb uninstall 它而B方法则不用。
推荐使用B方法。
同样卸载可以通过adb uninstall或adb shell rm xxx/xxx.apk来也推荐用删除的方法来卸载 编译指定模块命令 1、 cd packages/apps/Contacts //进入需要编译的模块目录 mm //编译当前模块 2、 mmm packages/apps/Contacts //编译指定的模块 8 如何开发自己的工程 前面主要是讲如何在eclipse上开发android原有的工程。
对于自己的工程我们可以这样做 1新建一个android工程。
建android工程的好处就是可以充分使用ADT的功能。
2导入需要的包 3编译、运行、调试 4加入到
android源码相应的目录下应用程序一般放在packages/apps下 我们观察packages/apps原有的工程就会发现它们的代码是很“干净”的没有ADT自动生成的assets、bin等文件夹和R.java当然也没有.classpath和.project 5编写Makefile文件xxx.mk 用
android源码提供的专用Makefile文件xxx.mk它的格式比较简单 6把刚加入的工程添加到eclipse的android工程中 可以在eclipse中添加也可以在.classpath中直接加入相应路径。
如在.classpath中添加 ltclasspathentry kindquotsrcquot pathquotpackages/apps/HelloWorld/srcquot/gt R.java 中编译时自动生成的其实所有工程用到“资源”的都会用到R.java这些R.java是放在out/target/common/R下。
我们在源码根目录下make全部代码时才会对每个模块生成R.java在make时已经编译过并生成有.apk文件的模块是不会被编译的。
因此如果新加入的工程已经用mm/mmm编译过的话我们先对该工程的“资源”改动一下必须是改动“资源”因为R.java是由“资源”生成的再make就在会out/target/common/R对应的包路径下看到你的工程的R.java。
刷新在eclipse的out/target/common/R 子工程再在你用到R类的地方加入它的包如 import com.android.example.test.inside.helloworld.R这样就不会出现找不到R定义的错误。
其实这个错误对我们是没有任何影响的因为我们是在shell中编译。
7在
android源码目录下编译刚加入的工程 可以用mm/mmm或make 模块名 8用版本控件工具svn或git或其它把该工程上传到服务器 从官方文档和实践可以总结出几点 1、可以使用eclipse来编辑JAVA程序、检查错误主要是类库包含和语法方面但是不能在eclipse上编译运行
android源码还是得在shell中make或mm或mmm 2、
android源码文件夹里提供有一些eclipse配置文件 .claapatheclipse工程的配置文件方便我们直接把
android源码相应的文件和JAVA包导入工程。
android-formatting.xml和android.importorder这个很重要主要是用来规范我们的编码风格更容易使我们的代码风格一致 3、把
android源码作为一个工程导入eclipse时必须注意两点 1、新建的工程必须是java project不能是android project否则会破坏
android源码一般是多添加文件/文件夹 2、导入前最好检查.classpath里的文件在
android源码中是否有相应的文件文件夹否则也会破坏
android源码一般是多添加文件/文件夹 总的来说 1、用eclipse来编辑代码、检查错误 2、不在eclipse上编译、运行
android源码程序只能在命令行通过make或mm或mmm编译
android源码 3、可以在eclipse上调试
android源码程序原理eclipse通过ddms服务器在emulator上进行调试并可以单步调试、断点调试。
4、需要调试的程序把它从/system/app/移除安装到data/app下这样更方便 5、安装、卸载程序通过adb push 和adb shell rm更方便 三、 set up the Eclipse IDE for Android platform development https://sites.google.com/a/android.com/opensource/using-eclipse Using Eclipse About this Document This document will help you set up the Eclipse IDE for Android platform development. Enter eclipse Basic setup First its important to make sure the regular Android development system is set up. cd /path/to/android/root make and wait a while if you havent done this Important: You will still be using quotmakequot 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 quotmakequot 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 quotJava Build Pathquot 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 uw .classpath Make the copy writable Now edit that copy of .classpath if necessary. Increase Eclipses Memory Settings The Android project is large enough that Eclipses 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:MaxPermSize256m Recommended settings for Android development -Xms128m -Xmx512m -XX:MaxPermSize256m These settings set Eclipses 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 or you can click some clicky thing instead if you prefer Now create a project for Android development: 1. If Eclipse asks you for a workspace location choose the default. 2. If you have a quotWelcomequot screen close it to reveal the Java perspective. 3. File gt New gt Java Project 4. Pick a project name quotandroidquot or anything you like. 5. Select quotCreate project from existing sourcequot enter the path to your Android root directory and click Finish. 6. Wait while it sets up the project. Youll 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 gt Build Automatically to force a rebuild. Note: Eclipse sometimes likes to add an quotimport android.Rquot 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 Eclipses view of things: 1. Window gt Show View gt Navigator 2. In the quotNavigatorquot right-click on the project quotandroid-javaquot or whatever you named it 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 apps source directory. To do this inside Eclipse: 1. Project gt Properties 2. Select quotJava Build Pathquot from the left-hand menu. 3. Choose the quotSourcequot tab. 4. Click quotAdd Folder...quot. 5. Add your apps quotsrcquot directory. 6. Click OK. When youre done the quotsource folderquot path in the list should look like android/packages/apps/YourAppName/src. Depending on which apps you include you may also need to include other src/main/java directories under android/dalvik/libcore. Do this if you find you cannot build with the default set. Eclipse setup to work on dev.