AS中ndk-build方式cpp问题集锦

想用c++代码在Java中调用,之前也一直OK调用.so库和jni接口,但不知道为什么在AS3.0上一直报标准库找不到;

还有一个变动就是AS2.2以后ndk编译默认采用cmake了,这就导致以前的Android.mk和Application.mk文件写法不行了;但为了方便还是使用了ndk-build方式(这块官方文档也是用的cmake,都没有ndk-build教程了,哎~)。没办法,之前对这块没经验,所以开始了填坑之旅。

目前还只有一个问题,以后填了新坑再补充。

问题:找不到类似string和std等标准库函数

首先要使用ndk-build形式进行编译cpp代码,需要修改一下build.gradle文件,现在新建c++项目都是用cmake了,所以我们如果不用cmake要用mdk-build的话,需要改build.gradle文件;直接贴代码:

改动一:build.gradle

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.zhc.jnidemo"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            ndkBuild {
                // Sets optional flags for the C compiler.
                cFlags "-D_EXAMPLE_C_FLAG1", "-D_EXAMPLE_C_FLAG2"

                // Sets a flag to enable format macro constants for the C++ compiler.
                cppFlags "-D__STDC_FORMAT_MACROS"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        ndkBuild {
            path "src/main/jni/Android.mk"
        }
    }
}

改动二:Android.mk、Application.mk

这两个makefile文件都是在jni目录下的
image.png

Android.mk如下:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := native
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_LDLIBS    := -llog
include $(BUILD_SHARED_LIBRARY)

Application.mk如下

#APP_OPTIM := release
APP_PLATFORM := android-15
APP_ABI := armeabi-v7a
NDK_TOOLCHAIN_VERSION=4.9
APP_PIE := false
# 重点是这句话:
APP_STL := stlport_static
APP_CFLAGS := -O3 -Wall -pipe \
    -ffast-math \
    -fstrict-aliasing -Werror=strict-aliasing \
    -Wno-psabi -Wa,--noexecstack \
    -DANDROID -DNDEBUG

现在再clean项目,重新编译项目,就发现类似#include <string>找不到的问题没有了,希望大家也能解决相同问题。

问题:找不到C++ 11标准库shared_ptr

Android.mk文件中添加APP_CFLAGS := -std=c++11即可支持c++ 11函数,然后在APP_STL := 中指定gnustl_static可以支持最多的函数库。可以查看官方文档:C++ 库支持

#APP_OPTIM := release
APP_PLATFORM := android-15
APP_ABI := armeabi-v7a
NDK_TOOLCHAIN_VERSION=4.9
APP_PIE := false
APP_STL := gnustl_static
APP_CFLAGS := -O3 -Wall -pipe \
    -ffast-math \
    -fstrict-aliasing -Werror=strict-aliasing \
    -Wno-psabi -Wa,--noexecstack \
    -DANDROID -DNDEBUG \
    -std=c++11

问题:must be enabled with the -std=c++11 or -std=gnu++11 compiler options

解决办法是在Android.mk文件中加入支持c++11的flag

LOCAL_CFLAGS += -std=c++11

问题:More than one file was found with OS independent path

Error:Execution failed for task ':app:transformNativeLibsWithMergeJniLibsForDebug'.
More than one file was found with OS independent path 'lib/armeabi-v7a/libnative-lib.so'

删除build.gradle下面的:

    // 删除一下代码
    sourceSets.main {
        jniLibs.srcDir('src/main/libs')
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android Studio配置NDK的步骤如下: 1.下载NDK:打开Android Studio,依次点击File -> Project Structure -> SDK Location,在SDK Location窗口找到Android NDK location,点击Download按钮下载NDK。 2.配置NDK路径:下载完成后,回到SDK Location窗口,将下载好的NDK路径复制到Android NDK location。 3.配置build.gradle文件:在app目录下的build.gradle文件添加以下代码: ```gradle android { ... defaultConfig { ... externalNativeBuild { cmake { cppFlags "" abiFilters "" } } } ... externalNativeBuild { cmake { path "CMakeLists.txt" } } } ``` 4.配置CMakeLists.txt文件:在项目根目录下创建CMakeLists.txt文件,并添加以下代码: ```cmake 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 ) # Searches for a specified prebuilt library and stores the path as a # variable. Because system libraries are included in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. 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 ) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in the # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} ) ``` 5.编写C++代码:在app/src/main/cpp目录下创建native-lib.cpp文件,并添加以下代码: ```c++ #include <jni.h> #include <string> extern "C" JNIEXPORT jstring JNICALL Java_com_example_ndkdemo_MainActivity_stringFromJNI( JNIEnv* env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); } ``` 6.运行程序:点击Android Studio的Run按钮,即可在模拟器或真机上运行程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值