title: Android NDK - Android studio gradle undefined reference to __android_log_write
body: <p>I am trying to debug a JNI C function by inserting log messages, but I can't get it to work. To start with I am just trying to modify the hello-jni example that comes with Android Studio.  This is the modified code:</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;jni.h&gt;
  #include &lt;android/log.h&gt;
  
  jstring
  Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                jobject thiz )
  {
  #if defined(__arm__)
      #if defined(__ARM_ARCH_7A__)
      #if defined(__ARM_NEON__)
        #if defined(__ARM_PCS_VFP)
          #define ABI "armeabi-v7a/NEON (hard-float)"
        #else
          #define ABI "armeabi-v7a/NEON"
        #endif
      #else
        #if defined(__ARM_PCS_VFP)
          #define ABI "armeabi-v7a (hard-float)"
        #else
          #define ABI "armeabi-v7a"
        #endif
      #endif
    #else
     #define ABI "armeabi"
    #endif
  #elif defined(__i386__)
     #define ABI "x86"
  #elif defined(__x86_64__)
     #define ABI "x86_64"
  #elif defined(__mips64)  /* mips64el-* toolchain defines __mips__ too */
     #define ABI "mips64"
  #elif defined(__mips__)
     #define ABI "mips"
  #elif defined(__aarch64__)
     #define ABI "arm64-v8a"
  #else
     #define ABI "unknown"
  #endif
  
      __android_log_write(ANDROID_LOG_ERROR, "TEST_TAG", "Error here");
  
      return (*env)-&gt;NewStringUTF(env, "Hello from JNI !  Compiled with ABI " ABI ".");
  }
  </code></pre>
  
  <p>And this is my Android.mk file:</p>
  
  <pre><code>LOCAL_PATH := $(call my-dir)
  
  include $(CLEAR_VARS)
  
  LOCAL_MODULE    := hello-jni
  LOCAL_SRC_FILES := hello-jni.c
  LOCAL_LDFLAGS := -llog
  
  include $(BUILD_SHARED_LIBRARY)
  </code></pre>
  
  <p>When I use the <code>ndk-build</code> script the libhello-jni.so files get built no problem. When I try to build the project in Android Studio I get the following gradle error message</p>
  
  <pre><code>Information:Gradle tasks [:app:assembleDebug]
  :app:preBuild
  :app:compileDebugNdk
  C:\Android\projects\hello-jni\app\build\intermediates\ndk\debug\obj/local/arm64-v8a/objs/hello-jni/C_\Android\projects\hello-jni\app\src\main\jni\hello-jni.o: In function `Java_com_example_hellojni_HelloJni_stringFromJNI':
  hello-jni.c:(.text.Java_com_example_hellojni_HelloJni_stringFromJNI+0x24): undefined reference to `__android_log_write'
  Error:error: ld returned 1 exit status
  make.exe: *** [C:\Android\projects\hello-jni\app\build\intermediates\ndk\debug\obj/local/arm64-v8a/libhello-jni.so] Error 1
  Error:Execution failed for task ':app:compileDebugNdk'.
  &gt; com.android.ide.common.internal.LoggedErrorException: Failed to run command:
  C:\Android\android-ndk-r10c\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Android\projects\hello-jni\app\build\intermediates\ndk\debug\Android.mk APP_PLATFORM=android-19 NDK_OUT=C:\Android\projects\hello-jni\app\build\intermediates\ndk\debug\obj NDK_LIBS_OUT=C:\Android\projects\hello-jni\app\build\intermediates\ndk\debug\lib APP_ABI=all
  Error Code:
  2
  Output:
  C:\Android\projects\hello-jni\app\build\intermediates\ndk\debug\obj/local/arm64-v8a/objs/hello-jni/C_\Android\projects\hello-jni\app\src\main\jni\hello-jni.o: In function `Java_com_example_hellojni_HelloJni_stringFromJNI':
  hello-jni.c:(.text.Java_com_example_hellojni_HelloJni_stringFromJNI+0x24): undefined reference to `__android_log_write'
  collect2.exe: error: ld returned 1 exit status
  make.exe: *** [C:\Android\projects\hello-jni\app\build\intermediates\ndk\debug\obj/local/arm64-v8a/libhello-jni.so] Error 1
  Information:BUILD FAILED
  Information:Total time: 3.297 secs
  Information:2 errors
  Information:0 warnings
  Information:See complete output in console
  </code></pre>
  
  <p>I've tried the suggestions given in this question, but I still get the same error: <a href="https://stackoverflow.com/questions/5465941/what-is-the-log-api-to-call-from-an-android-jni-program">What is the Log API to call from an Android JNI program?</a></p>
  
  <p>What am I doing wrong?</p>
  
  <p>I'm using Android Studio 0.8.9 and NDK r10c.</p>