【问题标题】:How to properly include C++ support to Android Studio project?如何正确地将 C++ 支持包含到 Android Studio 项目中?
【发布时间】:2019-01-23 17:09:33
【问题描述】:

我有一个 C++ 项目,我想在我的 android 应用程序中使用它。

根据Google's instructions创建新应用:

  1. 在向导的选择您的项目部分,选择 Native C++ 项目类型。
  2. 点击下一步。
  3. 完成向导下一部分中的所有其他字段。
  4. 点击下一步。
  5. 在向导的自定义 C++ 支持部分,您可以使用 C++ 标准字段自定义项目。使用下拉列表选择要使用的 C++ 标准化。选择 Toolchain Default 会使用默认的 CMake 设置。
  6. 点击完成。

这是我最终完成的项目: (我没有改变任何东西,所以它应该是通用的)

native-lib.cpp:

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL 
Java_e_viktor_testinggitapplication_MainActivity_stringFromJNI(JNIEnv *env,

jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

MainActivity.java:

package e.viktor.testinggitapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = 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();
}

这里我只加了cmake的版本

分级:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "e.viktor.testinggitapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"         

testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                version "3.10.2"
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
        }
    }
    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'
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/cpp/native-lib.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.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

结果 - MainActivity.java 中的“无法解析相应的 JNI 函数”

所有#include 的“找不到字段”和cpp 文件中所有类型的“无法解析类型”。

有趣的是,gradle 构建成功。

编辑:

添加了 CMakeLists.txt

【问题讨论】:

  • 您是否设置了 CMake 配置?如果您愿意,请在 externalNativeBuild 中发布您正在调用的 CMakeLists.txt 文件
  • @dr_g 我什么都没做,正如谷歌的指令所说的那样。我添加了 CMakeLists.txt

标签: java android cmake android-ndk


【解决方案1】:

我想问题出在缺少 NDK 的部分,因为在将 studio 更新到 3.3 后一切都解决了。

对于任何有类似情况的人:
即使我在工具部分手动安装了 NDK 并且仅使用 Android Studio 项目自动生成,我也遇到了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多