`
bolutes
  • 浏览: 875673 次
文章分类
社区版块
存档分类
最新评论

Android NDK rb5 文档之使用 Android 工具链作为一个独立编译器

 
阅读更多

android中间件开发,本来利用NDK就直接生成了一个SO文件,然后直接打包到APK里,即可运行,但是由于一般真机是不带有root权限的,即使SSH可以解决root权限,当到了java层,root权限又失效了。经常碰到operation not permitted 或 permission delied 之类的错误,但目前还有一个可以运行在真机上的C编译器,因为手机的资源毕竟是很有限的,可喜的是android-NDK给我们提供了这个交叉编译环境,只是生成SO文件的时候,把里面的细节全部省掉了。不过还是保留了许多中间过程的开发HTML文档。下面说说具体的实现过程:

1,下载NDK,并配合NDK环境变量为NDK的安装路径

2,根据NDK里docs文档里的standalone-toolchain.html来抽取交叉编译的环境。

3,配置SYSROOT环境变量: SYSROOT=$NDK/platforms/android-8/arch-arm //android-8是你的android开发版本所定
4,然后运行命令:

$NDK/build/tools/make-standalone-toolchain.sh --platform=android-5 --install-dir=/tmp/my-android-toolchain //默认是arm,x86:--arch=x86

/tmp/my-android-toolchain是你交叉编译环境的复制路径。最好别放在tmp目录里,因为重启机子就立即消失了。这个新生成的文件 夹即是你的交叉编译环境

5,配置PAHT和CC环境变量:

export PATH=/tmp/my-android-toolchain/bin:$PATH
export CC=arm-linux-androideabi-gcc //x86 export CC=i686-android-linux-gcc
如果需要长久有效,在/etc/profile里加上PATH=/..../bin:$PATH

export PATH

其他的类似处理。

6,把你要需要编译的C文件放在以上生成的编译环境的bin目录下,例如:/tmp/my-android-toolchain/bin,进入这个目录里,输入命令: $CC -o hello hello.c (这相当于我们平时的gcc命令,只是这里需要引用arm里的库,而不是X86里的库)

hello.c:

#include<string.h>

#include<stdio.h>
int main()

{

printf("hello,toolchain!/n");

return 0;

}

即生成了可在手机arm里运行的可执行文件hello

7,把这个可执行文件hello放到手机里的/data/目录下,方法有很多,这里介绍两种:

一、把这个文件放在电脑的某个目录下,然后进入DOS的这个目录下,运行如下命令:adb push hello /data/

二、放在SDCARD目录下,然后执行cp /sdcard/hello /data/也可以

8,进入/data/目录下,运行这个hello文件,例如“./hello”,即可以在adb shell里看到打印结果: hello,toolchain!



NDK r6 新的特性

随着r5中对native activity的支持,对ndk的关注比较紧,多么希望对c和c++更多的支持啊。下面是翻译自ndk的CHANGES.html中对r6的描述。

android-ndk-r6

IMPORTANT CHANGES:
- Official support for the x86 ABI.
This release of the Android NDK now provides support for the 'x86' ABI.
This allows you to generate machine code that runs on future x86-based
Android devices.

Note that by default, code is still generated for ARM-based devices.
You can however add 'x86' to your APP_PLATFORM definition in your
Application.mk. For example, the following line instructs ndk-build
to build your code for three distinct ABIs:

APP_ABI := armeabi armeabi-v7a x86

Unless you rely on ARM-based assembly sources, you shouldn't need to touch
your Android.mk files to build x86 machine code.

For all details regarding x86 support, please read the new documentation
file named docs/CPU-X86.html.

Don't hesitate to file NDK bugs related to x86 at http://b.android.com

- You can build a standalone x86 toolchain using the --toolchain=x86-4.4.3
option when calling make-standalone-toolchain.sh. See
docs/STANDALONE-TOOLCHAIN.html for more details.

- The new 'ndk-stack' tool can be used to translate stack traces
(as reported by adb logcat in case of crash in native code) into
something more readable, i.e. containing function / source file /
line number information corresponding to each stack frame.

For more information and usage example, see the new documentation
file docs/NDK-STACK.html

OTHER FIXES & CHANGES:
- The arm-eabi-4.4.0, which had been deprecated since NDK r5, has been
finally removed from the NDK distribution.


大意如下:

重要的变化:

-官方支持x86 ABI。

新发布的Android NDK 版本 现在提供了对x86 ABI的支持。
这允许你生成的机器码在未来的基于x86架构的Android设备上运行。

注意,代码仍然默认的生成基于ARM的设备。然而你可以在Application.mk中的APP_PLATFORM定义中添加‘x86’。
例如,下面一行指示ndk-build来构建你的代码向三种不同的ABI:
APP_ABI := armeabi armeabi-v7a x86

除非你依赖基于ARM的汇编源,否则你不需要管Android.mk来构建x86机器码。

关于对x86支持的所有细节,请阅读新文档docs目录下面的CPU-X86.html。

有关x86的bugs,请不用迟疑的归档在http://b.android.com
- 你可以构建一个单独的x86工具链,当调用make-standalone-toolchain.sh时使用 --toolchain=x86-4.4.3选项。
具体细节请看docs/STANDALONE-TOOLCHAIN.html。


- 新的“ndk栈”工具可以翻译栈的痕迹(在native代码crash时,adb logcat报告的)更多一些可读信息。
即,包含函数/源文件/行数信息相应的每个栈的帧。

更多信息和用法示例,请阅读新的文档docs/NDK-STACK.html。

其它 FIXES & CHANGES:
- arm-eabi-4.4.0, 从NDK r5 已经过时(deprecated),已经从NDK分布上永远的移除了。



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics