加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Android系统中用C语言来编写应用程序

发布时间:2020-12-15 04:47:50 所属栏目:百科 来源:网络整理
导读:以前我时常在想,怎么能在Android系统中用C语言来编写应用程序呢?Android系统上的应用程序不都是Java应用程序吗?其实是可以的,读者不妨用adb shell命令连上Android模拟器,在/system/bin目录下可以看到很多C可执行程序,如cat命令。今天,我们就来学习一

以前我时常在想,怎么能在Android系统中用C语言来编写应用程序呢?Android系统上的应用程序不都是Java应用程序吗?其实是可以的,读者不妨用adb shell命令连上Android模拟器,在/system/bin目录下可以看到很多C可执行程序,如cat命令。今天,我们就来学习一下怎么在Android系统中添加用C语言编写的可执行程序吧。

还是以hello world来讲吧。毕竟大家对这个比较熟。

进入到Android源代码工程的external目录,创建hello目录,

caizd@blsx:~/mt6580_androidL$ cd external/


caizd@blsx:~/mt6580_androidL/external$ mkdir hello

在hello目录中新建hello.c文件,并且添加代码如下:

#include

int main(int argc,char** argv)


{


printf(“Hello World!n”);


return 0;


}

这个程序的作用是打印出Hello World!

然后在hello目录中新建Android.mk文件,添加代码:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE := hello

LOCAL_SRC_FILES := $(call all-subdir-c-files)

include $(BUILD_EXECUTABLE)

注意,BUILD_EXECUTABLE表示我们要编译的是可执行程序

使用mmm命令进行编译:

caizd@blsx:~/mt6580_androidL/external$ mmm ./external/hello

编译成功后,就可以在out/target/product/inwatch_portal/system/bin/目录下,看到可执行文件hello了。

然后通过adb工具把hello push到机器的system/bin/,给755权限即可。然后执行adb 进入机器执行./hello,即可看到打印Hello World!,说明操作成功。具体操作命令如下:

C:Usersasus>adb root


adbd is already running as root

C:Usersasus>adb remount


remount succeeded

C:Usersasus>adb push Y:mt6580_androidLouttargetproductinwatch_portalsystembinhello system/bin


55 KB/s (5412 bytes in 0.094s)

C:Usersasus>adb shell


root@inwatch_portal:/ # cd system/bin


cd system/bin


root@inwatch_portal:/system/bin # chmod 755 hello


chmod 755 hello


root@inwatch_portal:/system/bin # ./hello


./hello


Hello World!


root@inwatch_portal:/system/bin #

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读