利用adb工具android真机环境运行cpp(无需root)

来源:互联网 发布:蓝牙串口调试助手源码 编辑:程序博客网 时间:2024/04/30 16:17

想学习下android下的epoll,可惜macos是基于unix的,无epoll库支持

那就直接写cpp到真机上运行吧


项目结构如下图所示


项目文件为

android_helloandroid

|-------jni

|-------|-------helloandroid.cpp

|-------|-------Android.mk

|-------build.sh


helloandroid.cpp文件内容为

#include <stdlib.h>#include <stdio.h>int main(int argc, char const *argv[]){printf("%s\n","halo" );return 0;}



Android.mk文件内容为

# Copyright (C) 2009 The Android Open Source Project## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at##      http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := helloandroidLOCAL_SRC_FILES := helloandroid.cppLOCAL_STATIC_LIBRARIES := libcinclude $(BUILD_EXECUTABLE)
* 注意最后一行为BUILD_EXECUTABLE


运行脚本build.sh为

#!/bin/bashexport MODULE_NAME="helloandroid"ndk-buildadb push libs/armeabi/${MODULE_NAME} /data/localecho ""echo "------ run ${MODULE_NAME} -------"echo ""adb shell /data/local/${MODULE_NAME}rm -r libsrm -r objecho ""


注意需要设置环境变量

我设置在了mac的用户文件里

~/.bash_profile

export ANDROID_SDK_ROOT=/programes/android-sdk-macosxexport ANDROID_NDK_ROOT=/programes/android-ndk-r9dexport COCOS2DX_ROOT=//files/cocos2d-2.0-x-2.0.4export NDK_ROOT=/programes/android-ndk-r8c    export ADB_ROOT=/Applications/Android\ Studio.app/sdk/platform-toolsexport PATH=$PATH:$ANDROID_SDK_ROOTexport PATH=$PATH:$ANDROID_NDK_ROOTexport PATH=$PATH:$ADB_ROOT### Your previous /Users/ashqal/.bash_profile file was backed up as /Users/ashqal/.bash_profile.macports-saved_2014-01-14_at_13:20:36### MacPorts Installer addition on 2014-01-14_at_13:20:36: adding an appropriate PATH variable for use with MacPorts.export PATH=/opt/local/bin:/opt/local/sbin:$PATH# Finished adapting your PATH environment variable for use with MacPorts.
这样就可以直接找到adb、ndk-build命令了


最后是打开真机调试模式,插上真机,打开终端,在项目根目录下运行此build.sh



总结下,

思路就是用ndk-bulid工具的BUILD_EXECUTABLE脚本自动生成android可识别的可运行文件,

然后用adb工具把文件上传到真机上,

然后用adb shell命令执行此文件


0 0
原创粉丝点击