使用Android Studio在新的项目添加CMake支持,操作十分简单,只需要勾选“Include C/C++ support”,Android Studio就会自动为新项目配置好CMake环境。
但对于已创建的项目,Android Studio似乎并没有提供自动配置的功能。
要在已创建的项目中配置CMake,需要进行以下步骤:
首先,在项目的build.gradle文件下,修改配置如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
建议使用Gradle 3.2.1版本,使用其他版本编译,可能会出现配置错误的问题。由于Gradle 3.2.1版本对应的zip为gradle-4.6-all.zip,因此还要修改gradle-wrapper.properties文件:
#Sun Jun 16 13:00:26 CST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
在app文件夹下build.gradle文件中,分别在android和defaultConfig配置中添加externalNativeBuild:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "cn.axen.demo"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// defaultConfig中添加externalNativeBuild
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// android中添加externalNativeBuild
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
在app文件夹下添加CMakeLists.txt,在CMakeLists.txt添加如下内容:
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
jni_lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
jni_lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
点击项目右键,创建JNI文件夹:
新建C/C++ Source File,这里命名为jni_hello.cpp:
jni_hello.cpp代码如下:
#include<jni.h>
#include <string>
extern "C"JNIEXPORT
jstring JNICALL Java_cn_axen_demo_MainActivity_stringFromJNI(JNIEnv *env, jclass thiz)
{
std::string hello = "Hello,I from C++";
return env->NewStringUTF(hello.c_str());
}
将jni_hello.cpp添加到CMakeLists.txt中:
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
jni_lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/jni/jni_hello.cpp)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
jni_lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
大功告成,rebuild项目之后,就可以使用jni_hello里面的函数啦:
package cn.axen.demo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= findViewById(R.id.tv);
textView.setText(stringFromJNI);
}
static{
System.loadLibrary( "jni_lib");
}
private static native String stringFromJNI();