Android 存档

Android 存档

用Eclipse开发与调试纯粹的Android C++程序,非ndk-build、ndk-gdb

2011 年 09 月 01 日 分类目录 :Android 2

本文欢迎转载,但请注明出处,否则依法追究。源文链接: http://www.gqweb.net/eclipse-dev-an…-build-ndk-gdb_253.html

在做Android本地程序开发时,Google为我们提供了NDK开发环境,如果只是很小的工程,用NDK开发还是很方便的。但是如果是很大的工程,代码文件很多的时候,手工支配置android.mk文件还是很繁锁的。虽然Google不建议我们用NDK开发大规模的工程,但是在很多时候,例如,现有游戏或者其它工程的移植时,还是不可避免的。本文将介绍一种利用eclipse进行传统C++开发的方法。

一、 开发篇

1. 创建工程
打开eclipse新建C++工程–>键入工程名并选择Hello World C++ Project–>Finish

create c++ project

创建C++工程

选择Hello World工程

2. 设置工程属性
右键工程–>选择属性–>C/C++Build–>Tool Chain Editor : [Current toolchain: “Cross GCC”]

选择工具链

注意:如果没有所需的工具链选择,可能是eclipse的版本不对。

3. 设置工具链属性
右键工程–>选择属性–>C/C++Build–>Settings

工具链设置

3.1. [Cross Settings]

Prefex : arm-linux-androideabi-
Path : /home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/

注意:

1. Path是指工具链的根目录,并不是可执行文件的目录,所以不需要加上 “/bin”目录。
2. /home/q/Workspace/Andriod/NDK/是NDK的根目录,请根据自己的情况选择。以下设置相同。

3.2. [Cross GCC Compiler]

Include Paths :
/home/q/Workspace/Andriod/NDK/platforms/android-9/arch-arm/usr/include
/home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/include
/home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/arm-linux-androideabi/include
/home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.4.3/include
/home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.4.3/include-fixed/
/home/q/Workspace/Andriod/NDK/sources/cxx-stl/stlport/stlport/
Miscellaneous:
Other Flags :-c -fmessage-length=0 -DANDROID -fno-exceptions

如果使用stlport,“-DANDROID -fno-exceptions” 选项是必须的否则 stlport中的 #include_next 没法使用,会报如下错误:

/home/q/Workspace/Andriod/NDK/sources/cxx-stl/stlport/stlport/stl/_cstddef.h:29:30: error: cstddef: No such file or directory

钩选:-fPIC

3.3. [Cross G++ Compiler] 同 3.2。

3.4. [Cross G++ Linker]
General: -nostdlib
Libraries:
添加的库:

1c
2m
3stdc++
4stlport

库路径:

1/home/q/Workspace/Andriod/NDK/platforms/android-9/arch-arm/usr/lib
2/home/q/Downloads/android_bak/NDK/sources/cxx-stl/stlport/libs/armeabi-v7a

Libraries设置

注意:
在/home/q/Downloads/android_bak/NDK/sources/cxx-stl/stlport/libs/armeabi-v7a路径下是没有libstlport.so文件的。我们需要从我们自己的设备上把此文件下载下来:

adb shell pull /system/lib/libstlport.so /home/q/Downloads/android_bak/NDK/sources/cxx-stl/stlport/libs/armeabi-v7a

Miscellaneous:
其它选项:
-R/home/q/Workspace/Andriod/NDK/platforms/android-9/arch-arm/usr/lib
其它对象:
/home/q/Workspace/Andriod/NDK/platforms/android-9/arch-arm/usr/lib/crtbegin_dynamic.o
/home/q/Workspace/Andriod/NDK/platforms/android-9/arch-arm/usr/lib/crtend_android.o

链接器选项

这样开发的配置就已经完成了,编译一下试试看吧!

1 adb push ./HelloWorld /data/td/
2adb shell /data/td/HelloWorld

注意:如果将程序放到sdcard上是不能执行的,即使已经root了,chmod也设置不了权限的。
二、 调试篇

虽然在NDK里面有ndk-gdb的工具使我们可以调试ndk-build编译出来的程序,但是它需要android.mk文件,从上一篇的介绍中我们并没有生成此文件。还有其它方法可以用吗?分析ndk-gdb文件我们提取出下面这些有用信息。

首先我们以命令行的方式来模拟ndk-gdb的操作:

1. 在设备上启动gdbserver监听gdb的请求:

1 root@g:~# adb devices
2List of devices attached
3 353046FDC1EE00EC device
4
5 root@g:~# adb push /home/q/Workspace/cmu_test_remote/HelloWorld/Debug/HelloWorld /data/td/
6 1002 KB/s (136536 bytes in 0.132s)
7 root@g:~# adb push /home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/gdbserver /system/xbin/
8 893 KB/s (125208 bytes in 0.136s)
9 root@g:~# adb shell gdbserver :5039 /data/td/HelloWorld
10Process /data/td/HelloWorld created; pid = 21867
11Listening on port 5039

2. 进行端口重定向:

1adb forward tcp:5039 tcp:5039

因为gdb需要通过TCP或者串口进行连接,但大多数情况下我们是用USB线进行连接的,所以需要进行端口重定向。

3. Gdb 打开程序,进行调试:

1 root@g:~# /home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gdb /home/q/Workspace/cmu_test_remote/HelloWorld/Debug/HelloWorld
2GNU gdb 6.6
3Copyright (C) 2006 Free Software Foundation, Inc.
4 GDB is free software, covered by the GNU General Public License, and you are
5welcome to change it and/or distribute copies of it under certain conditions.
6 Type "show copying" to see the conditions.
7 There is absolutely no warranty for GDB. Type "show warranty" for details.
8 This GDB was configured as "--host=x86_64-linux-gnu --target=arm-elf-linux"...
9(gdb)

这里的gdb的版本一定要正确。这时已经进入了gdb调试模式,还需要进行与远程的gdbserver进行连接,在gdb模式下输入:

1(gdb) target remote :5039
2 Remote debugging using :5039
3 warning: Unable to find dynamic linker breakpoint function.
4GDB will be unable to debug shared library initializers
5and track explicitly loaded dynamic code.
6 0xb0001000 in ?? ()
7(gdb) l
84 // Version :
95 // Copyright : Your copyright notice
10 6 // Description : Hello World in C++, Ansi-style
117 //============================================================================
128
13 9 #include <iostream>
14 10 using namespace std;
1511
1612 int main() {
17 13 cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
18(gdb) b main
19 Breakpoint 1 at 0x84e8: file ../src/HelloWorld.cpp, line 13.
20(gdb) c
21Continuing.
22 Error while mapping shared library sections:
23 /system/bin/linker: No such file or directory.
24 Error while mapping shared library sections:
25libc.so: Success.
26 Error while mapping shared library sections:
27libm.so: Success.
28 Error while mapping shared library sections:
29libstdc++.so: Success.
30 Error while mapping shared library sections:
31libstlport.so: Success.
32
33Breakpoint 1, main () at ../src/HelloWorld.cpp:13
34 13 cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
35(gdb) c
36Continuing.
37
38Program exited normally.
39(gdb)

至此已经完成了整个程序的调试过程。

4. 与Eclipse集成

虽然通过命令行已经可以对程序进行调试,但毕竟没有图形界面的方便,下面将介绍如何将gdb调试与Eclipse进行集成。

4.1. 到Eclipse中选中需调试的工程 ,打开Debug Configurations…

打开Debug Configurations

4.2. 在打开的Debug Configurations中新建C/C++ Remote Application

在main页面中设置:
Connection: local
Remote Absolute File Path : /data/td/HelloWorld
Commands to execute before application:

/home/q/Workspace/Andriod/SDK/platform-tools/adb shell gdbserver :5039 /data/td/HelloWorld
/home/q/Workspace/Andriod/SDK/platform-tools/adb forward tcp:5039 tcp:5039

注意,这里的adb路径一定要完整路径。最后还要选中 “Skip download to target path”

HelloWorld Debug设置

转到Debugger页片,填入gdb的完整路径,并将GDB command file置空。

Debug gdb路径设置

至此配置工作全部结束,直接点击调试吧。

参考:

1. Android Native Development Using the Android Open Source Project[OL]. http://www.aton.com/android-native-development-using-the-android-open-source-project/
2. Debugging with GDB[OL]. http://www.kandroid.org/online-pdk/guide/debugging_gdb.html
3. GDB 命令详细解释 [OL]. http://blog.youkuaiyun.com/wei801004/article/details/4253911
4. How C/C++ Debugging Works onAndroid[OL]. http://mhandroid.wordpress.com/2011/01/25/how-cc-debugging-works-on-android/
5. ndk-gdb[OL]. http://csipsimple.googlecode.com/svn/trunk/pjsip_android/ndk-gdb

android, Eclipse, native code, ndk

显示 Android NDK 编译和链接详情的方法

2011 年 08 月 27 日 分类目录 :Android 2

显示Android NDK 编译和链接详情有两种方法:

方法一:

如果有source,可以用source的mmm工具编译:

1 q@g:~$ cd /home/q/Workspace/Andriod/source/
2 q@g:~/Workspace/Andriod/source$ source build/envsetup.sh
3including device/htc/passion/vendorsetup.sh
4including device/samsung/crespo4g/vendorsetup.sh
5including device/samsung/crespo/vendorsetup.sh
6 including sdk/bash_completion/adb.bash
7
8 q@g:~/Workspace/Andriod/source$ help
9 Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
10 - croot: Changes directory to the top of the tree.
11 - m: Makes from the top of the tree.
12 - mm: Builds all of the modules in the current directory.
13 - mmm: Builds all of the modules in the supplied directories.
14 - cgrep: Greps on all local C/C++ files.
15 - jgrep: Greps on all local Java files.
16 - resgrep: Greps on all local res/*.xml files.
17 - godir: Go to the directory containing a file.
18
19 Look at the source to view more functions. The complete list is:
20addcompletions add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct choosetype choosevariant cproj croot findmakefile gdbclient get_abs_build_var getbugreports get_build_var getprebuilt gettop godir help isviewserverstarted jgrep lunch m mm mmm pid printconfig print_lunch_menu resgrep runhat runtest set_java_home setpaths set_sequence_number set_stuff_for_environment settitle smoketest startviewserver stopviewserver systemstack tapas tracedmdump
21
22 q@g:~/Workspace/Andriod/source$ mmm /home/q/Workspace/Andriod/NDK/samples/hello-jni/jni showcommands
23============================================
24 PLATFORM_VERSION_CODENAME=AOSP
25 PLATFORM_VERSION=3.1.4.1.5.9.2.6.5
26TARGET_PRODUCT=full
27TARGET_BUILD_VARIANT=eng
28TARGET_SIMULATOR=
29TARGET_BUILD_TYPE=release
30TARGET_BUILD_APPS=
31TARGET_ARCH=arm
32TARGET_ARCH_VARIANT=armv7-a
33HOST_ARCH=x86
34HOST_OS=linux
35HOST_BUILD_TYPE=release
36BUILD_ID=OPENMASTER
37============================================
38 make:进入目录'/home/q/Workspace/Andriod/source'
39build/core/base_rules.mk:78: *** Module name: hello-jni
40build/core/base_rules.mk:79: *** Makefile location: /home/q/Workspace/Andriod/NDK/samples/hello-jni/jni
41 build/core/base_rules.mk:80: *
42 build/core/base_rules.mk:81: * Each module must use a LOCAL_MODULE_TAGS in its
43build/core/base_rules.mk:82: * Android.mk. Possible tags declared by a module:
44 build/core/base_rules.mk:83: *
45build/core/base_rules.mk:84: * optional, debug, eng, tests, samples
46 build/core/base_rules.mk:85: *
47 build/core/base_rules.mk:86: * If the module is expected to be in all builds
48 build/core/base_rules.mk:87: * of a product, then it should use the
49 build/core/base_rules.mk:88: * "optional" tag:
50 build/core/base_rules.mk:89: *
51 build/core/base_rules.mk:90: * Add "LOCAL_MODULE_TAGS := optional" in the
52 build/core/base_rules.mk:91: * Android.mk for the affected module, and add
53 build/core/base_rules.mk:92: * the LOCAL_MODULE value for that component
54build/core/base_rules.mk:93: * into the PRODUCT_PACKAGES section of product
55 build/core/base_rules.mk:94: * makefile(s) where it's necessary, if
56build/core/base_rules.mk:95: * appropriate.
57 build/core/base_rules.mk:96: *
58 build/core/base_rules.mk:97: * If the component should be in EVERY build of ALL
59 build/core/base_rules.mk:98: * products, then add its LOCAL_MODULE value to the
60build/core/base_rules.mk:99: * PRODUCT_PACKAGES section of
61build/core/base_rules.mk:100: * build/target/product/core.mk
62 build/core/base_rules.mk:101: *
63build/core/base_rules.mk:102: *** user tag detected on new module - user tags are only supported on legacy modules。 停止。
64 make:离开目录“/home/q/Workspace/Andriod/source
65
66 q@g:~/Workspace/Andriod/source$ mmm /home/q/Workspace/Andriod/NDK/samples/hello-jni/jni showcommands
67============================================
68 PLATFORM_VERSION_CODENAME=AOSP
69 PLATFORM_VERSION=3.1.4.1.5.9.2.6.5
70TARGET_PRODUCT=full
71TARGET_BUILD_VARIANT=eng
72TARGET_SIMULATOR=
73TARGET_BUILD_TYPE=release
74TARGET_BUILD_APPS=
75TARGET_ARCH=arm
76TARGET_ARCH_VARIANT=armv7-a
77HOST_ARCH=x86
78HOST_OS=linux
79HOST_BUILD_TYPE=release
80BUILD_ID=OPENMASTER
81============================================
82 make:进入目录'/home/q/Workspace/Andriod/source'
83target thumb C: hello-jni <= /home/q/Workspace/Andriod/NDK/samples/hello-jni/jni/hello-jni.c
84 prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gcc -I /home/q/Workspace/Andriod/NDK/samples/hello-jni/jni -I out/target/product/generic/obj/SHARED_LIBRARIES/hello-jni_intermediates -I dalvik/libnativehelper/include/nativehelper -I system/core/include -I hardware/libhardware/include -I hardware/libhardware_legacy/include -I hardware/ril/include -I dalvik/libnativehelper/include -I frameworks/base/include -I frameworks/base/opengl/include -I frameworks/base/native/include -I external/skia/include -I out/target/product/generic/obj/include -I bionic/libc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -I bionic/libm/include -I bionic/libm/include/arch/arm -I bionic/libthread_db/include -c -fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections -funwind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security -fno-short-enums -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -include system/core/include/arch/linux-arm/AndroidConfig.h -I system/core/include/arch/linux-arm/ -Wno-psabi -mthumb-interwork -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -DNDEBUG -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-registers -DNDEBUG -UDEBUG -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -MD -o out/target/product/generic/obj/SHARED_LIBRARIES/hello-jni_intermediates/hello-jni.o /home/q/Workspace/Andriod/NDK/samples/hello-jni/jni/hello-jni.c
85target SharedLib: hello-jni (out/target/product/generic/obj/SHARED_LIBRARIES/hello-jni_intermediates/LINKED/hello-jni.so)
86prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-g++ -nostdlib -Wl,-soname,hello-jni.so -Wl,-T,build/core/armelf.xsc -Wl,--gc-sections -Wl,-shared,-Bsymbolic -Lout/target/product/generic/obj/lib out/target/product/generic/obj/SHARED_LIBRARIES/hello-jni_intermediates/hello-jni.o out/target/product/generic/obj/lib/crtbegin_so.o -Wl,--whole-archive -Wl,--no-whole-archive -lc -lstdc++ -lm -o out/target/product/generic/obj/SHARED_LIBRARIES/hello-jni_intermediates/LINKED/hello-jni.so -Wl,-z,noexecstack -Wl,--fix-cortex-a8 -Wl,--no-undefined prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/../lib/gcc/arm-eabi/4.4.3/libgcc.a out/target/product/generic/obj/lib/crtend_so.o
87target Prelink: hello-jni (out/target/product/generic/symbols/system/lib/hello-jni.so)
88out/host/linux-x86/bin/apriori --prelinkmap build/core/prelink-linux-arm.map --locals-only --quiet out/target/product/generic/obj/SHARED_LIBRARIES/hello-jni_intermediates/LINKED/hello-jni.so --output out/target/product/generic/symbols/system/lib/hello-jni.so
89 build/tools/apriori/prelinkmap.c(168): library 'hello-jni.so' not in prelink map
90 make: *** [out/target/product/generic/symbols/system/lib/hello-jni.so] 错误 1
91 make:离开目录“/home/q/Workspace/Andriod/source
方法二

直接用NDK的 ndk-build,不过需要加上参数 -B V=1

1 q@g:~/Workspace/Andriod/source$ ndk-build -B V=1 -C/home/q/Workspace/Andriod/NDK/samples/hello-jni/jni/
2 make:进入目录'/home/q/Workspace/Andriod/NDK/samples/hello-jni/jni'
3 rm -f /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi/lib*.so /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi-v7a/lib*.so /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/x86/lib*.so
4 rm -f /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi/gdbserver /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi-v7a/gdbserver /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/x86/gdbserver
5 rm -f /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi/gdb.setup /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi-v7a/gdb.setup /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/x86/gdb.setup
6Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
7 mkdir -p /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi
8 install -p /home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/gdbserver /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi/gdbserver
9Gdbsetup : libs/armeabi/gdb.setup
10 mkdir -p /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi
11 echo "set solib-search-path /home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi" > /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi/gdb.setup
12 echo "directory /home/q/Workspace/Andriod/NDK/platforms/android-8/arch-arm/usr/include /home/q/Workspace/Andriod/NDK/samples/hello-jni/jni /home/q/Workspace/Andriod/NDK/sources/cxx-stl/system" >> /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi/gdb.setup
13Compile thumb : hello-jni <= hello-jni.c
14 /home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -MMD -MP -MF /home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi/objs-debug/hello-jni/hello-jni.o.d.org -fpic -ffunction-sections -funwind-tables -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -I/home/q/Workspace/Andriod/NDK/samples/hello-jni/jni -DANDROID -Wa,--noexecstack -O0 -g -I/home/q/Workspace/Andriod/NDK/platforms/android-8/arch-arm/usr/include -c /home/q/Workspace/Andriod/NDK/samples/hello-jni/jni/hello-jni.c -o /home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi/objs-debug/hello-jni/hello-jni.o && ( if [ -f "/home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi/objs-debug/hello-jni/hello-jni.o.d.org" ]; then rm -f /home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi/objs-debug/hello-jni/hello-jni.o.d && mv /home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi/objs-debug/hello-jni/hello-jni.o.d.org /home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi/objs-debug/hello-jni/hello-jni.o.d; fi )
15 SharedLibrary : libhello-jni.so
16 /home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-g++ -Wl,-soname,libhello-jni.so -shared --sysroot=/home/q/Workspace/Andriod/NDK/platforms/android-8/arch-arm /home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi/objs-debug/hello-jni/hello-jni.o -Wl,--no-undefined -Wl,-z,noexecstack -lc -lm -lsupc++ -o /home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi/libhello-jni.so
17Install : libhello-jni.so => libs/armeabi/libhello-jni.so
18 mkdir -p /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi
19 install -p /home/q/Workspace/Andriod/NDK/samples/hello-jni/obj/local/armeabi/libhello-jni.so /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi/libhello-jni.so
20/home/q/Workspace/Andriod/NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-strip --strip-unneeded /home/q/Workspace/Andriod/NDK/samples/hello-jni/libs/armeabi/libhello-jni.so
21 make:离开目录“/home/q/Workspace/Andriod/NDK/samples/hello-jni/jni”

Debug NDK by Eclipse

2011 年 08 月 11 日 分类目录 :Android 2

MartinH 已经在他的博客里面介绍的很详细,这里不再缀述,请参考:

http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-debugging/

http://mhandroid.wordpress.com/2011/01/23/using-cgdb-with-ndk-debug-and-cgdb-tutorial/

error: .repo/manifests/: contains uncommitted changes 解决方法

2011 年 08 月 10 日 分类目录 :Android 2

今天在同步Android Source时出现

1 error: .repo/manifests/: contains uncommitted changes

google查找一阵子,无果,虽然也有人遇到但却没有给出解决方法。最后试了一下以下方法,得以解决。

  1. 删除Andriod/source/.repo/目录下除了projects目录以外的所有目录。
  2. 重新执行命令
1 repo init -u git://android.git.kernel.org/platform/manifest.git
2repo sync

此时因为projects目录文件没有删除,所以重新同步不会重新下载所有文件,所以不会花费很长时间。

android

Android ndk 编译方法

2011 年 08 月 10 日 分类目录 :Android 1

1. 用NDK命令编译:

cd $(ndk-root)
./ndk-build -C/home/q/Workspace/Andriod/NDK/samples/test-libstdc++/jni

其中“-C”是指定需要编译的路径,其中必须有Android.mk。注意“-C”与路径之间没有空格。

详细使用方法请参见:

1 q@g:~/Workspace/Andriod/NDK$ ./ndk-build --help
2用法:make [选项] [目标] ...
3选项:
4 -b, -m 忽略兼容性。
5 -B, --always-make Unconditionally make all targets.
6 -C 目录, --directory=目录
7 在所有操作前切换到“目录”。
8 -d 打印大量调试信息。
9 --debug[=FLAGS] 打印各种调试信息
10 -e, --environment-overrides
11 指定替代makefile中默认设置的环境变量
12 -f FILE, --file=FILE, --makefile=FILE
13 读取 FILE 作为一个 makefile.
14 -h, --help 打印该消息并退出。
15 -i, --ignore-errors Ignore errors from commands.
16 -I DIRECTORY, --include-dir=DIRECTORY
17 搜索 DIRECTORY 为包含的 makefiles.
18 -j [N], --jobs[=N] 同时允许 N 个任务;无参数表明允许无限个任务。
19 -k, --keep-going 当某些目标无法创建时仍然继续。
20 -l [N], --load-average[=N], --max-load[=N]
21 不开始多线程工作除非系统负载低于N
22 -L, --check-symlink-times Use the latest mtime between symlinks and target.
23 -n, --just-print, --dry-run, --recon
24 不要实际运行任何命令;仅仅输出他们
25 -o FILE, --old-file=FILE, --assume-old=FILE
26 FILE认作非常老,不要重新make它.
27 -p, --print-data-base 打印 make 的内部数据库。
28 -q, --question 不运行任何命令;退出状态说明是否已全部更新。
29 -r, --no-builtin-rules 禁用内置隐含规则。
30 -R, --no-builtin-variables 禁用内置变量设置。
31 -s, --silent, --quiet 不显示命令。
32 -S, --no-keep-going, --stop
33 关闭 -k.
34 -t, --touch touch 目标而不是重新创建它们
35 -v, --version 打印 make 的版本号并退出。
36 -w, --print-directory 打印当前目录。
37 --no-print-directory 即使 -w 隐式开启,也要关闭 -w。
38 -W FILE, --what-if=FILE, --new-file=FILE, --assume-new=FILE
39 FILE认作无限新.
40 --warn-undefined-variables Warn when an undefined variable is referenced.
41
42 这个程序创建为 x86_64-pc-linux-gnu
43Report bugs to

2. 使用源码自带Toolchain工具

如果你已经得到了Android源码,你还可通过Android源码中提供的Make工具编译,这种方法的好处是可以查看更多的编译细节。

1. 初始化,使用命令

1 $ source build/envsetup.sh

或者

1 $ . build/envsetup.sh

2. 直接输入命令,可以查看编译的命令

1 q@g:~/Workspace/Andriod/source$ help
2 Invoke ". build/envsetup.sh" from your shell to add the following functions
3to your environment:
4 - croot: Changes directory to the top of the tree.
5 - m: Makes from the top of the tree.
6 - mm: Builds all of the modules in the current directory.
7 - mmm: Builds all of the modules in the supplied directories.
8 - cgrep: Greps on all local C/C++ files.
9 - jgrep: Greps on all local Java files.
10 - resgrep: Greps on all local res/*.xml files.
11 - godir: Go to the directory containing a file.
12
13 Look at the source to view more functions. The complete list is:
14add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct
15choosetype choosevariant cproj croot findmakefile gdbclient
16 get_abs_build_var getbugreports get_build_var getprebuilt gettop godir help
17isviewserverstarted jgrep lunch m mm mmm pid printconfig print_lunch_menu
18resgrep runhat runtest set_java_home setpaths set_sequence_number
19set_stuff_for_environment settitle smoketest startviewserver stopviewserver
20systemstack tapas tracedmdump

3. 使用mmm命令编译NDK下面的例子

1 q@g:~/Workspace/Andriod/source$ mmm /home/q/Workspace/Andriod/NDK/samples/test-libstdc++/jni/
2 ============================================
3 PLATFORM_VERSION_CODENAME=AOSP
4 PLATFORM_VERSION=AOSP
5 TARGET_PRODUCT=full
6 TARGET_BUILD_VARIANT=eng
7 TARGET_SIMULATOR=
8 TARGET_BUILD_TYPE=release
9 TARGET_BUILD_APPS=
10 TARGET_ARCH=arm
11 TARGET_ARCH_VARIANT=armv5te
12 HOST_ARCH=x86
13 HOST_OS=linux
14 HOST_BUILD_TYPE=release
15 BUILD_ID=OPENMASTER
16 ============================================
17 make:进入目录'/home/q/Workspace/Andriod/source'
18 build/core/base_rules.mk:78: *** Module name: test-libstl
19 build/core/base_rules.mk:79: *** Makefile location: /home/q/Workspace/Andriod/NDK/samples/test-libstdc++/jni
20 build/core/base_rules.mk:80: *
21 build/core/base_rules.mk:81: * Each module must use a LOCAL_MODULE_TAGS in its
22 build/core/base_rules.mk:82: * Android.mk. Possible tags declared by a module:
23 build/core/base_rules.mk:83: *
24 build/core/base_rules.mk:84: * optional, debug, eng, tests, samples
25 build/core/base_rules.mk:85: *
26 build/core/base_rules.mk:86: * If the module is expected to be in all builds
27 build/core/base_rules.mk:87: * of a product, then it should use the
28 build/core/base_rules.mk:88: * "optional" tag:
29 build/core/base_rules.mk:89: *
30 build/core/base_rules.mk:90: * Add "LOCAL_MODULE_TAGS := optional" in the
31 build/core/base_rules.mk:91: * Android.mk for the affected module, and add
32 build/core/base_rules.mk:92: * the LOCAL_MODULE value for that component
33 build/core/base_rules.mk:93: * into the PRODUCT_PACKAGES section of product
34 build/core/base_rules.mk:94: * makefile(s) where it's necessary, if
35 build/core/base_rules.mk:95: * appropriate.
36 build/core/base_rules.mk:96: *
37 build/core/base_rules.mk:97: * If the component should be in EVERY build of ALL
38 build/core/base_rules.mk:98: * products, then add its LOCAL_MODULE value to the
39 build/core/base_rules.mk:99: * PRODUCT_PACKAGES section of
40 build/core/base_rules.mk:100: * build/target/product/core.mk
41 build/core/base_rules.mk:101: *
42 build/core/base_rules.mk:102: *** user tag detected on new module - user tags
43are only supported on legacy modules。 停止。
44 make:离开目录“/home/q/Workspace/Andriod/source”

4. 根据上面的提示编译出错,并根据提示在Android.mk里面添加 “LOCAL_MODULE_TAGS := optional”

1# A simple test for the minimal standard C++ library
2#
3
4 LOCAL_PATH := $(call my-dir)
5
6include $(CLEAR_VARS)
7 LOCAL_MODULE_TAGS := optional #add to here
8 LOCAL_MODULE := test-libstl
9 LOCAL_SRC_FILES := test-libstl.cpp
10include $(BUILD_EXECUTABLE)

再次运行上面的命令编译。

3. 使用源码自带Toolchain

将上面的方法中m、mm、mmm替换成make

1 q@g:~/Workspace/Andriod/source$ make libsimplejni showcommands
2 ============================================
3 PLATFORM_VERSION_CODENAME=AOSP
4 PLATFORM_VERSION=AOSP
5 TARGET_PRODUCT=full
6 TARGET_BUILD_VARIANT=eng
7 TARGET_SIMULATOR=
8 TARGET_BUILD_TYPE=release
9 TARGET_BUILD_APPS=
10 TARGET_ARCH=arm
11 TARGET_ARCH_VARIANT=armv5te
12 HOST_ARCH=x86
13 HOST_OS=linux
14 HOST_BUILD_TYPE=release
15 BUILD_ID=OPENMASTER
16 ============================================
17 find: `frameworks/base/frameworks/base/docs/html': 没有那个文件或目录
18 find: `out/target/common/docs/gen': 没有那个文件或目录
19 find: `frameworks/base/frameworks/base/docs/html': 没有那个文件或目录
20 find: `out/target/common/docs/gen': 没有那个文件或目录
21 find: `frameworks/base/frameworks/base/docs/html': 没有那个文件或目录
22 find: `out/target/common/docs/gen': 没有那个文件或目录
23 find: `frameworks/base/frameworks/base/docs/html': 没有那个文件或目录
24 find: `out/target/common/docs/gen': 没有那个文件或目录
25 find: `frameworks/base/frameworks/base/docs/html': 没有那个文件或目录
26 find: `out/target/common/docs/gen': 没有那个文件或目录
27 target thumb C++: libsimplejni <= development/samples/SimpleJNI/jni/native.cpp
28 prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-g++ -I dalvik/libnat
29 ivehelper/include/nativehelper -I development/samples/SimpleJNI/jni -I out
30 /target/product/generic/obj/SHARED_LIBRARIES/libsimplejni_intermediates -I o
31 ut/target/product/generic/obj/STATIC_LIBRARIES/libwebcore_intermediates -I d
32 alvik/libnativehelper/include/nativehelper -I system/core/include -I hardw
33 are/libhardware/include -I hardware/libhardware_legacy/include -I hardware
34 /ril/include -I dalvik/libnativehelper/include -I frameworks/base/include
35 -I frameworks/base/opengl/include -I frameworks/base/native/include -I e
36 xternal/skia/include -I out/target/product/generic/obj/include -I bionic/l
37 ibc/arch-arm/include -I bionic/libc/include -I bionic/libstdc++/include
38 -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -I bionic/libm
39 /include -I bionic/libm/include/arch/arm -I bionic/libthread_db/include -
40 c -fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections -funw
41 ind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security -fno-sh
42 ort-enums -march=armv5te -mtune=xscale -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__
43 ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -include system/core/include/arch/linux-arm/A
44 ndroidConfig.h -I system/core/include/arch/linux-arm/ -Wno-psabi -mthumb-inter
45 work -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-a
46 rith -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequ
47 ence-point -DNDEBUG -g -Wstrict-aliasing=2 -finline-functions -fno-inline-func
48 tions-called-once -fgcse-after-reload -frerun-cse-after-loop -frename-register
49 s -DNDEBUG -UDEBUG -fvisibility-inlines-hidden -DANDROID -fmessage-length=0 -W
50 -Wall -Wno-unused -Winit-self -Wpointer-arith -Wsign-promo -Werror=return-typ
51 e -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -DNDEBUG -UD
52 EBUG -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64
53 -fno-rtti -MD -o out/target/product/generic/obj/SHARED_LIBRARIES/libsimp
54 lejni_intermediates/native.o development/samples/SimpleJNI/jni/native.cpp
55 target SharedLib: libsimplejni (out/target/product/generic/obj/SHARED_LIBRARIE
56 S/libsimplejni_intermediates/LINKED/libsimplejni.so)
57 prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-g++ -nostdlib -Wl,-so
58 name,libsimplejni.so -Wl,-T,build/core/armelf.xsc -Wl,--gc-sections -Wl,-share
59 d,-Bsymbolic -Lout/target/product/generic/obj/lib out/target/product/generi
60 c/obj/SHARED_LIBRARIES/libsimplejni_intermediates/native.o out/targ
61 et/product/generic/obj/lib/crtbegin_so.o -Wl,--whole-archive -Wl,--no-whole-
62 archive -llog -lutils -lc -lstdc++ -lm -o out/target/product/generic/obj/SH
63 ARED_LIBRARIES/libsimplejni_intermediates/LINKED/libsimplejni.so -Wl,-z,noexe
64 cstack -Wl,--no-undefined prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/
65 ../lib/gcc/arm-eabi/4.4.3/libgcc.a out/target/product/generic/obj/lib/crtend_s
66o.o
67 target Non-prelinked: libsimplejni (out/target/product/generic/symbols/system/
68 lib/libsimplejni.so)
69 out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/SHARED_LIBRARIE
70 S/libsimplejni_intermediates/LINKED/libsimplejni.so out/target/product/generic
71 /symbols/system/lib/libsimplejni.so
72 target Strip: libsimplejni (out/target/product/generic/obj/lib/libsimplejni.so)
73 out/host/linux-x86/bin/soslim --strip --shady --quiet out/target/product/gener
74 ic/symbols/system/lib/libsimplejni.so --outfile out/target/product/generic/obj
75 /lib/libsimplejni.so
76 Install: out/target/product/generic/system/lib/libsimplejni.so
77 out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/lib/libsimplejn
78 i.so out/target/product/generic/system/lib/libsimplejni.so

通过参数中的showcommand可以得到编译的具体命令。在输出中可以看到

1 find: `frameworks/base/frameworks/base/docs/html': 没有那个文件或目录
2 find: `out/target/common/docs/gen': 没有那个文件或目录

的错误,此错误可以忽略,不会影响代码的编译。如果实在觉得影响美观可以重新下载下载源码编译。如果觉得下载时间太长可以按如下步骤解决:
1. 删除源码下除了“.repo”文件夹外的所有文件夹

1 cd /android/source
2 rm -rf *

2. 重新同步服务器

1repo sync

因为本地仓库中已经有了源文件,所以不会花费很长时间。

参考:

Building the System[OL], source.android.com/source/building.html

在Android系统上使用busybox——最简单的方法

2011 年 07 月 22 日 分类目录 :Android 1

Android是基于Linux系统的,在学习和使用Android系统时,不可避免的要和Terminal打交道。但是Android自带的Terminal emulator实在是简陋,连一些基本的命令都不具备。有没有什么解决方法呢?有的,这便是被称为“嵌入式Linux中的瑞士军刀”的Busybox。

一、BusyBox
BusyBox 是标准 Linux 工具的一个单个可执行实现。BusyBox 包含了一些简单的工具,例如 cat 和 echo,还包含了一些更大、更复杂的工具,例如 grep、find、mount 以及 telnet。有些人将 BusyBox 称为 Linux 工具里的瑞士军刀。简单的说BusyBox就好像是个大工具箱,它集成压缩了 Linux 的许多工具和命令。

二、安装

  1. 取得设备的Root权限(网上查找)。
  2. 下载Busybox(下载)编译安装,也可以直接下载binary文件(下载)。
  3. 将Busybox binary文件拷贝到Android系统的/system/xbin目录下。

三、使用

1. 在安装完后便可以使用了。使用方法:busybox command,例如,busybox ls
busybox ls
看颜色是不是变了。

2. 因为系统里面也有ls命令,如果直接调用ls将会调用系统的ls,这样要想使用busybox的命令就必须加上busybox字符。这样很不方便,有没有办法可以省去这样麻烦呢?

方法一: 使用命令 alies,例如:
# alias ls=’busybox ls’
这个方法的缺点是重启终端,又会恢复原样。

方法二: 删除或者重命名 /system/bin/toolbox

参考文献:

  1. 为Android安装BusyBox —— 完整的bash shell[OL].http://www.cnblogs.com/xiaowenji/archive/2011/03/12/1982309.html
  2. Android自带的toolbox分析及扩展[OL].http://blog.youkuaiyun.com/a345017062/article/details/6250619

android, busybox

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值