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

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

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


项目结构如下图所示

利用adb工具android真机环境运行cpp(无需root)_第1张图片

项目文件为

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    := helloandroid
LOCAL_SRC_FILES := helloandroid.cpp
LOCAL_STATIC_LIBRARIES := libc

include $(BUILD_EXECUTABLE)
* 注意最后一行为BUILD_EXECUTABLE


运行脚本build.sh为

#!/bin/bash
export MODULE_NAME="helloandroid"
ndk-build
adb push libs/armeabi/${MODULE_NAME} /data/local
echo ""
echo "------ run ${MODULE_NAME} -------"
echo ""
adb shell /data/local/${MODULE_NAME}
rm -r libs
rm -r obj
echo ""


注意需要设置环境变量

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

~/.bash_profile

export ANDROID_SDK_ROOT=/programes/android-sdk-macosx
export ANDROID_NDK_ROOT=/programes/android-ndk-r9d
export COCOS2DX_ROOT=//files/cocos2d-2.0-x-2.0.4
export NDK_ROOT=/programes/android-ndk-r8c    
export ADB_ROOT=/Applications/Android\ Studio.app/sdk/platform-tools

export PATH=$PATH:$ANDROID_SDK_ROOT
export PATH=$PATH:$ANDROID_NDK_ROOT
export 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

利用adb工具android真机环境运行cpp(无需root)_第2张图片



总结下,

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

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

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


你可能感兴趣的:(android,小细节)