Abstract
本文介绍如何在现有Android studio工程中添加C/C++代码以及相关native方法,并编译成.so库。开发环境Android Studio 3.x,请确认已正确安装NDK,LLDB,CMake(Tools-SDK Manager-SDK Tools)。ps:如果是新建项目,new project时直接勾选Include c++ support即可。
Step1 添加C/C++代码
project视图下,定位到app/src/main,右键main - New - Directory,命名为cpp,在cpp目录下新建include目录,并添加以下c/c++代码(注意目录结构)。如图
c_test.h
#ifndef NDKDEMO_C_TEST_H
#define NDKDEMO_C_TEST_H
const char * cFunction();
#endif
c_test.c
#include "stdio.h"
const char * cFunction(){
const char * str = "String from C !";
return str;
}
cpp_test.h
#ifndef NDKDEMO_CPP_TEST_H
#define NDKDEMO_CPP_TEST_H
const char * cppFunction();
#endif
cpp_test.cpp
#include <iostream>
using namespace std;
const char * cppFunction(){
const char * str = "String from C++ !";
return str;
}
Step2 添加CMakeLists.txt文件
右键app/ - New - File,命名为CMakeLists.txt,添加内容如下(参见注释)
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
#设置生成的so动态库最后输出的路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI})
#.h文件目录
include_directories(
${CMAKE_SOURCE_DIR}/src/main/cpp/include #h文件目录
)
add_library( # Sets the name of the library.
ndkdemo-lib #c/cpp代码将要编译成为so库的名称,java代码加载库文件要用这个名称
SHARED #动态库
src/main/cpp/c_test.c #cpp代码文件路径 可以在下面接着随意添加c、c++文件
src/main/cpp/cpp_test.cpp
src/main/jni/ndkdemo-lib.c #参见step 5
src/main/jni/ndkdemo-lib.cpp #参见step 5
)
find_library( #调用系统库
log-lib #打印用
log )
target_link_libraries( # Specifies the target library.
ndkdemo-lib
${log-lib} )
Step3 Link C++ Project with Gradle
切换到Android视图,右键app,选择" Link C++ Project with Gradle"
设置step2中新建的CMakeLists.txt的路径
Step4 添加java代码
在MainActivity中,System.loadLibrary()加载so库,并添加相关native方法,代码如下:
public class MainActivity extends AppCompatActivity {
private TextView ctxt;
private TextView cpptxt;
static {
System.loadLibrary("ndkdemo-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctxt = findViewById(R.id.ctxt);
ctxt.setText(stringFromC());
cpptxt = findViewById(R.id.cpptxt);
cpptxt.setText(stringFromCpp());
}
public static native String stringFromC();
public static native String stringFromCpp();
}
布局文件(activity_main.xml)如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/ctxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/cpptxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Step5 添加JNI层代码
光标定位到MainActivity中的public static native String stringFromC(),Alt+Enter后AS会自动新建ndkdemo-lib.c文件和相关函数头,在ndkdemo-lib.c添加以下代码:
#include <jni.h>
#include "c_test.h"
JNIEXPORT jstring JNICALL
Java_com_example_chaoyc_ndkdemo_MainActivity_stringFromC(JNIEnv *env, jclass type)
{
const char * cStr = cFunction();
return (*env)->NewStringUTF(env, cStr);
}
注:C++代码调用的不同
在step5路径(即app/src/main/jni/)新建ndkdemo-lib.cpp文件,添加代码如下:
#include <jni.h>
#include "cpp_test.h"
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_chaoyc_ndkdemo_MainActivity_stringFromCpp(
JNIEnv *env,
jclass type)
{
const char * cppStr = cppFunction();
return env->NewStringUTF(cppStr);
}
Step6 查看运行结果和编译生成的.so库
运行结果如下:
根据CMakeLists.txt中指定的路径,生成的.so库如下:
Ref
https://developer.android.google.cn/studio/projects/add-native-code
注
笔者水平有限,欢迎留言交流!
网友评论