利用adb工具android真机环境运行cpp(无需root)
16lz
2021-01-23
想学习下android下的epoll,可惜macos是基于unix的,无epoll库支持
那就直接写cpp到真机上运行吧
项目结构如下图所示
项目文件为
android_helloandroid
|-------jni
|-------|-------helloandroid.cpp
|-------|-------Android.mk
|-------build.sh
helloandroid.cpp文件内容为
#include #include 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命令执行此文件
更多相关文章
- 史上最全谷歌Android开发工具Android Studio下载地址汇总
- android studio添加忽略文件
- Android手机凡客项目介绍
- android读取properties配置文件
- Android调用WebAPI之传参、文件
- 重装或者升级Android Studio后,打开旧的Android项目,同步编译时遇
- Android assets文件夹之位置放置和作用( AS 和eclipse中的 )
- Android中关于FTP的文件上传和下载