资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

androidcpp的简单介绍

如何调试android NDK 交叉编译的cpp文件

主要讲一下具体的步骤,具体的ndk指令我就不说了,贴的文章都有:

创新互联专注于企业成都营销网站建设、网站重做改版、剑阁网站定制设计、自适应品牌网站建设、H5开发商城网站制作、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为剑阁等各大城市提供网站开发制作服务。

首先是写一个.java文件,本例中是HprofDumper.java

具体如下:

public class HprofDumper {

public native boolean hprofDumper(String filename, String outname);

}

然后用命令javac HprofDumper.java 生成.class文件

再用javah HprofDumper 生成相应的.h文件

生成的.h文件如下

#include

#ifndef _Included_HprofDumper

#define _Included_HprofDumper

#ifdef __cplusplus

extern "C" {

#endif

JNIEXPORT jboolean JNICALL Java_HprofDumper_hprofDumper

(JNIEnv *, jobject, jstring, jstring);

#ifdef __cplusplus

}

#endif

#endif

然后只需要在对应的.cpp文件完成相应函数即可,核心代码如下:

#include "HprofDumper.h"

#include "hprof.h"

JNIEXPORT jboolean JNICALL Java_HprofDumper_hprofDumper

(JNIEnv *env, jobject obj, jstring in_file, jstring out_file)

{

const char *filename = env-GetStringUTFChars(in_file, 0);

const char *outname = env-GetStringUTFChars(out_file, 0);

return hprof_dump(filename, outname);

}

其中hprof_dump是纯c++代码,引入即可。

有一点需要注意,标红了已经,就是生成的.h文件函数并没具体形参名字,只有形参类型,在.cpp文件中要加入相应的形参名字,本例为env、 obj、 in_file和out_file。

还有一点c和c++的区别,就是env的使用。

本例中C++为env-GetStringUTFChars(in_file, 0);

如果是C就应该改为(env)-GetStringUTFChars(env,in_file, 0);

调用Java类型 : C中调用Java中的String类型为 jstring;

C语言方法名规则 : Java_完整包名类名_方法名(JNIEnv *env, jobject thiz), 注意完整的类名包名中包名的点要用 _ 代替;

参数介绍 : C语言方法中有两个重要的参数, JNIEnv *env, jobject thiz ;

-- JNIEnv参数 : 该参数代表Java环境, 通过这个环境可以调用Java中的方法;

-- jobject参数 : 该参数代表调用jni方法的类,;

调用jni.h中的NewStringUTF方法 : 该方法的作用是在C语言中创建一个Java语言中的String类型对象, jni.h中是这样定义的 jstring (*NewStringUTF)(JNIEnv*, const char*), JNIEnv 结构体中包含了 NewStringUTF 函数指针, 通过 JNIEnv 就可以调用这个方法;

完成代码编写后,在当前目录下完成Android.mk和Application.mk的编写

首先是Android.mk

本例中为:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hprof-dumper

LOCAL_C_INCLUDES += external/stlport/stlport

LOCAL_C_INCLUDES += bionic

LOCAL_C_INCLUDES += bionic/libstdc++/include

LOCAL_SRC_FILES := HprofDumper.cpp \

xx.cpp \

xx.cpp \

xx.cpp \

xx.cpp \

xx.cpp \

xx.cpp \

xxx.cpp

LOCAL_SHARED_LIBRARIES := libstlport

include $(BUILD_SHARED_LIBRARY)

注意标红的是最关键的,LOCAL_C_INCLUDES 顾名思义是需要的头文件的所在的目录,那三个参数主要为了引入STL,最重要!!LOCAL_SHARED_LIBRARIES 我一直生成失败就是没加这个参数,不光要引入头文件,还要引入具体的lib,这就是这个字段的作用。

具体字段的作用:

-- LOCAL_PATH : 代表mk文件所在的目录;

-- include $(CLEAR_VARS) : 编译工具函数, 通过该函数可以进行一些初始化操作;

-- LOCAL_MODULE : 编译后的 .so 后缀文件叫什么名字;

-- LOCAL_SRC_FILES: 指定编译的源文件名称;

-- include $(BUILD_SHARED_LIBRARY) : 告诉编译器需要生成动态库;

Applicaion.mk中就一行

APP_STL = stlport_static

表示使用stl静态库。

注意:我用了STL,大家没有用STL的当然不用引入这些啦~

android用cmake怎么导入cpp文件夹,undifined reference to

ndroid Studio升级到2.2之后,我们可以先配置好NDK开发的一些所需工具,如图,在SDK Tools中勾选安装CMake、LLDB、NDK。

CMake: 外部构建工具。如果你准备只使用 ndk-build 的话,可以不使用它。

LLDB: Android Studio上面调试本地代码的工具。

Android Studio自带DEMO了解CMAKE

Android Studio升级到2.2版本之后,在创建新的project时,界面上多了一个Include C++ Support的选项。勾选它之后将会创建一个默认的C++与JAVA混编的Demo程序。就让我们先来看看这个官方标准Demo吧。

开始之前最好先下载好NDK,见NDK开发 从入门到放弃(一:基本流程入门了解),即在Project Structure界面Android NDK location处下载或选择正确的路径。或者使用上方提供的工具安装方法来进行下载。否则,创建的新project也会报错,需要配置好后clean。

File - New - New Project,在如下界面中勾选Include C++ Support,然后一路 Next,直到 Finish 为止即可。

项目打开后我们查看目录结构,与常规项目不同的是多了.externalNativeBuild文件夹、cpp文件夹、CMakeLists.txt文件,如下图:

这三个东西都是NDK部分:

1. .externalNativeBuild文件夹:cmake编译好的文件, 显示支持的各种硬件等信息。系统生成。

2. cpp文件夹:存放C/C++代码文件,native-lib.cpp文件是该Demo中自带的,可更改。需要自己编写。

3. CMakeLists.txt文件:CMake脚本配置的文件。需要自己配置编写。

Gradle中也有两处不同:

java代码:

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Example of a call to a native method

TextView tv = (TextView) findViewById(R.id.sample_text);

tv.setText(stringFromJNI());

}

/**

* A native method that is implemented by the 'native-lib' native library,

* which is packaged with this application.

*/

public native String stringFromJNI();

// Used to load the 'native-lib' library on application startup.

static {

System.loadLibrary("native-lib");

}

}12345678910111213141516171819202122231234567891011121314151617181920212223

从native-lib.cpp的代码中我们能看到它使用的是静态注册的方式,动态注册的方式代码同传统JNI。

#include jni.h

#include string

extern "C"

jstring

Java_com_example_person_myapplication_MainActivity_stringFromJNI(

JNIEnv* env,

jobject /* this */) {

std::string hello = "Hello from C++";

return env-NewStringUTF(hello.c_str());

}12345678910111234567891011

CMakeLists.txt文件中我们需要注意的是下面这三个地方。两个library的名字(需一致)以及一个cpp文件的路径,彼此需要对应一致,当我们自己定义library以及自己创建cpp文件时需要对应修改。

如何在Android上运行cpp-tests

Download Cocos2d-x

Download cocos2d-x and unzip it. We could simply unzip it on the root directory of your home folder. After unzipping, double click the cocos2d-xfolder and you should have a structure like this:

Download JDK, SDK and NDK

For developing Android games Java is a must have toolkit.

check your Java version.

java -version

Hopefully you see results similar to: (although your version may be slightly different.)

java version "1.8.0_05"

Java(TM) SE Runtime Environment (build 1.8.0_05-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

If not, you need to download and install Java before proceeding JDK

Download the Android SDK.

ADT Bundle For Mac

ADT Bundle For Windows (32-bit)

ADT Bundle For Windows (64-bit)

ADT Bundle For Linux (32-bit)

ADT Bundle For Linux (64-bit)

The bundle includes the newest Android SDK version plus an Eclipse version with Android Development Tool included. This is all you need.

After downloading it, unzip the package at ~/AndroidDev directory. The folder contains two folders: *sdk* and *eclipse*.

You can launch Eclipse and install other SDK versions, if needed. Here is an example:

Download NDK. Always use the latest version. This article version is r9d.

After downloading it, unzip the package at the same location of the Android SDK. In our case, it is under the *~/AndroidDev* directory.

Verify Your Environment

verify that *Python 2.7* is installed and it is accessible:

python --version in your Terminal (or cmd on Win32) and it will give you the following result:

$ python --version

Python 2.7.5

If not, you need to download and install Python before proceeding.

Hombrew brew install python

Python.org

Install and verify Apache Ant

Hombrew brew install ant

Apache.org

verify that Ant is found, simply execute:

$ ant

Buildfile: build.xml does not exist!

Build failed

Setup Environment Variables

Run setup.py to configure your Android development environment. This will set the necessary environment variables needed. If you haven't configured this environment before, you will be prompted to enter paths for variables that are not found.

Caution: You must not use the ~ sign. Use the full path to your home directory. Otherwise, the scripts will fail due to error path value.

In your cocos2d-x directory runpython setup.py and you will get the following results:

*COCOS2D_CONSOLE_ROOT* environment variable to point to the bin directory under ~/cocos2d-x/tools/cocos2d-console directory.

*NDK_ROOT* environment variable to point to the location of where you put the NDK. (i.e android-ndk-r9d/)

*ANDROID_SDK_ROOT* environment variable to point to the location of where you put the adt-bundle. Example /Users/guanghui/AndroidDev/adt-bundle-mac-x86_64-20130522/sdk/. The adt-bundle-mac-x86_64-xxxx, the xxxx number maybe different. So please note this non-trival difference.

*ANT_ROOT* environment variable to point to the location of where you put apache-ant-x.x.x. The apache-ant-x.x.x, the x.x.x number maybe different. So please note this non-trival difference.

When all environment variables are correctly configured, you should let them take effect.

On *nix systems, you could issue the following commands:

source ~/.bash_profile

on win32 system, you can just close the command line windows and restart it.

Use android-build.py to build cocos2d-x samples

Change your directory to the where the android-build.py script is located. (usually cocos2d-x/build)

cd build

and then

android list targets

Notice the id in this next screen shot.

Now I can execute:

python android-build.py -p 19 cpp-tests

That's it! The script will handle everything else that you need. When finished you should get the following message:

How to deploy it on your Android phone via command line

Enable USB Debugging on your phone and then connect your phone via USB.

Change your directory to the the bin directory of testcpp android project:

cd ~/cocos2d-x/tests/cpp-tests/proj.android/bin

(Note:If your current directory is build, you could use some relative path like thiscd ../tests/cpp-tests/proj.android/bin)

Then you could use adb to install the apk to your android phone:

adb install TestsDemo-debug.apk

If it prompts you that adb is not a command, you must re-equery your ~/.bashrc file. Ensure it contains the 4 environment variables set above. You could also manually execute:

export PATH=$PATH:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools

You should see the following screenshot! You are done.

我有个 安卓手机 下载了个文件查看器,程序显示有查看cpp文件的功能,但是我却打不开cpp文件.

cpp是c++源代码文件,只不过扩展名不同而已,内部文件格式与txt一样。

其实最笨也是最好用的办法就是:将.cpp文件扩展名改为.txt,在手机中进行编辑整理后,可以再将扩展名改回来。

这应该是对付这种文件最好的办法。

Android | 内存指标与分析方法

这篇文章的内容会涉及以下前置 / 相关知识,贴心的我都帮你准备好了,请享用~

这篇文章偏底层,难免有写错的地方还请你多多斧正哦~

Android 系统包括三种不同类型的内存:RAM、zRAM 和 ROM:

对于内核来说,无论是内核进程还是用户进程,说到底都是 task_struct 结构体的一个实例。task_struct 也叫进程描述符(process descriptor),里面记录了进程相关的所有信息。

在 task_struct 中有一个 mm_struct 的数据结构,也叫内存描述符(memory descriptor),里面记录了 Linux 进程内存管理的所有信息。mm_struct 定义在 linux/mm_types.h 头文件中,其中有一个页(page)的数据结构:

—— 图片引用自网络

页(Page)是 Linux 内核进行内存管理的基本单位,通常一个页的大小为 4 KB 。根据页面是否使用分为 “可用页” 和 “已使用页” 两种,其中已使用页可以分为以下类别:

缓存页是指有存储器中的文件支持的内存,分为两种: 私有页 共享页 :

匿名页是没有存储器中的文件支持的内存(例如由设置了 MAP_ANONYMOUS 标志的 mmap() 进行分配)

为了避免应用滥用内存,Android 系统会限制应用可以申请的最大堆内存,超过此限制就会抛出 OOM 异常。Android 设备出厂后,最大堆内存就已经确定,相关的配置位于系统根目录 /system/build.prop 文件中,我们可以通过命令查看:

在 App 虚拟机启动时,会读取 /system/build.prop 文件的配置,源码位于: AndroidRuntime.cpp

需要注意的是,配置 dalvik.vm.heapgrowthlimit 限制的仅仅是 Java 堆内存,本地内存不受其限制的。换句话说,应用可以使用的最大内存其实是可以大于最大堆内存的。

在确定进程占用了多少内存时,必须考虑多个进程共享页的情况。在 Linux 里,一个进程占用的内存有四种指标,分别是:

一般来说内存占用大小有如下规律:VSS = RSS = PSS = USS。

—— 图片引用自 Android Developers

—— 图片引用自 —— sunsky303 著

关于输出信息的具体分析,建议直接看 Gityuan 的这篇文章: 《Android 内存分析命令》 ,已经写得非常详细了。

如何让工程调用的是android的.cpp而不是调用的cocos2dx的.cpp

cocos2d-x在win32上开发出来的代码还需要交叉编译后才能生成android可以使用的包,具体操作见这个文档 另:使用cocos2d-x引擎的优势在于便于移植性。其开发出的C++代码只要在各上只要稍加改动就可以使用。


网站题目:androidcpp的简单介绍
浏览路径:http://cdkjz.cn/article/dsigjec.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220