Android Studio已創(chuàng)建項(xiàng)目下配置CMake

使用Android Studio在新的項(xiàng)目添加CMake支持,操作十分簡單,只需要勾選“Include C/C++ support”,Android Studio就會(huì)自動(dòng)為新項(xiàng)目配置好CMake環(huán)境。
但對(duì)于已創(chuàng)建的項(xiàng)目,Android Studio似乎并沒有提供自動(dòng)配置的功能。
要在已創(chuàng)建的項(xiàng)目中配置CMake,需要進(jìn)行以下步驟:
首先,在項(xiàng)目的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版本,使用其他版本編譯,可能會(huì)出現(xiàn)配置錯(cuò)誤的問題。由于Gradle 3.2.1版本對(duì)應(yīng)的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添加如下內(nèi)容:

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} )

點(diǎn)擊項(xiàng)目右鍵,創(chuàng)建JNI文件夾:

創(chuàng)建jni文件夾.png

新建C/C++ Source File,這里命名為jni_hello.cpp:
新建C/C++ Source File.png

新建C/C++ Source File.png

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項(xiàng)目之后,就可以使用jni_hello里面的函數(shù)啦:

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();

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。