4

I am using Android Studio 2.1.2 and I am not using Experimental Plugin for NDK.

I need to compile C++11 source into Android application.

I am done with all NDK setup done with sample JNI and NJI communication.

Now I need to try new keywords or features added in C++11 with Android.

Please check following files, and error I am getting.

I fixed the issue. I am editing files with working code. And the question is not duplicate.

HelloJni.cpp

char *pc = nullptr;     // OK
int  *pi = nullptr;     // OK Checking C++11

Application.mk

    APP_ABI := all
    APP_STL    := gnustl_static
    APP_CFLAGS += -std=gnu++11
    APP_OPTIM  := debug
    LOCAL_CPP_FEATURES += exceptions
    NDK_TOOLCHAIN_VERSION := 4.9

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := ndksampleapp
LOCAL_SRC_FILES := HelloJni.cpp

include $(BUILD_SHARED_LIBRARY)

build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.example.m1035325.ndksampleapp"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets.main {
        //jniLibs.srcDirs = [ 'src/main/libs' ]
        jniLibs.srcDir 'src/main/libs'
        jni.srcDirs = []
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

I am getting following error

Warning:Native C/C++ source code is found, but it seems that NDK option is not configured.  Note that if you have an Android.mk, it is not used for compilation.  The recommended workaround is to remove the default jni source code directory by adding:
 android {
    sourceSets {
        main {
            jni.srcDirs = []
        }
    }
}
to build.gradle, manually compile the code with ndk-build, and then place the resulting shared object in src/main/jniLibs.
 char *pc = nullptr;     // OK
            ^
C:\Users\AndroidStudioProjects\NDKSampleApp\app\src\main\jni\HelloJni.cpp
Error:(10, 12) error: 'nullptr' was not declared in this scope
Error:(11, 12) error: 'nullptr' was not declared in this scope
 int  *pi = nullptr;     // OK Checking C++11
            ^
make: *** [C:\Users\AndroidStudioProjects\NDKSampleApp\app\build\intermediates\ndk\debug\obj/local/arm64-v8a/objs/app/C_\Users\AndroidStudioProjects\NDKSampleApp\app\src\main\jni\HelloJni.o] Error 1
:app:compileDebugNdk FAILED
Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Users\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd'' finished with non-zero exit value 2
Information:BUILD FAILED

Error is fixed. Now I am able to compile C++11 cpp code with Android NDK. Please refer the edited files above.

AdiAtAnd
  • 119
  • 1
  • 13
  • With Android Studio 2.2 there should be new possibilities (unfortunately, as you are with 2.1... then again, 2.2 is already near release candidate stage, I'm using it daily, although not with NDK): http://tools.android.com/tech-docs/external-c-builds – Ped7g Aug 23 '16 at 13:44
  • IMHO that linked "duplicate" doesn't relate to the OP problem, as he already has `-std=gnu++11` in Android.mk. OP should search for "how to use custom Android.mk with gradle". – Ped7g Aug 23 '16 at 13:55
  • Thanks Ped7g for support. That's what my opinion is. I have already gone through that question for which my question is marked as duplicate. That is not what I am looking for. I already surpassed that error, before I was getting that error. I have one query in that question's answer : APP_CFLAGS += -std=c++11 what is " += " it should be " := " if I am not wrong. – AdiAtAnd Aug 23 '16 at 13:59
  • `+=` is to append text to current value in `APP_CFLAGS`. – Ped7g Aug 23 '16 at 14:02
  • Check this question and answer, not sure if it is applicable for 2.1.2 and non-experimental gradle plugin (as the development of AS+gradle is so fast, that I'm not sure what does apply to what): http://stackoverflow.com/a/27840210/4271923 – Ped7g Aug 23 '16 at 14:05
  • Hey Ped7g I fixed the issue. Check the edited files above. You can upvote the question if you are feeling like its not duplicate. Thanks :) – AdiAtAnd Aug 25 '16 at 18:12

0 Answers0