android-studio - 无法使用 Android Studio 从 JNI 打印日志消息

标签 android-studio android-ndk java-native-interface android-gradle-plugin

我得到的错误:

error: undefined reference to '__android_log_print'



我已经将此行添加到我的 .cpp 文件中:
#include <android/log.h>

我都试过: __android_log_print __android_log_write

我还在我的 Android.mk 文件(我手动编译)中链接了日志库。

LOCAL_LDLIBS := -llog

我还尝试了几种我发现的替代方法:
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 

这是我的 Android.mk:
LOCAL_PATH := $(call my-dir)

#used to skip re-compiling libraw
#include $(CLEAR_VARS)
#LOCAL_MODULE    := libraw_r
#LOCAL_SRC_FILES := ../obj/local/armeabi/libraw_r.so
#LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/libraw
#include $(PREBUILT_SHARED_LIBRARY)

#used for compiling libraw
include $(CLEAR_VARS)
LOCAL_CFLAGS += -I$(SYSROOT)/usr/lib/include/libraw -pthread -w
LOCAL_CXXFLAGS += -I$(SYSROOT)/usr/lib/include/libraw -pthread -w
LOCAL_MODULE     := libraw_r                    # name of your module
LOCAL_LDLIBS     += -L$(SYSROOT)/usr/lib -lstdc++ # libraries to link against, lstdc++ is auto-linked

LOCAL_SRC_FILES  :=  internal/dcraw_common.cpp internal/dcraw_fileio.cpp internal/demosaic_packs.cpp src/libraw_cxx.cpp src/libraw_c_api.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/libraw
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := armapi
LOCAL_SRC_FILES := armapi.cpp
LOCAL_SHARED_LIBRARIES := libraw_r
include $(BUILD_SHARED_LIBRARY)

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog

这是 build.gradle 文件:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0+'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        ndk {
            moduleName "armapi"
            ldLibs "log"
        }
    }

    sourceSets {

        main {
            manifest.srcFile 'AndroidManifest.xml'

            jniLibs.srcDir 'libs' // use the jni .so compiled from the manual ndk-build command
            jni.srcDirs = [] //disable automatic ndk-build call

            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']


        }


        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')


    }

}

最佳答案

看来使用 Gradle + Android Studio 忽略 Android.mk 文件 .

如解释 here ,尝试将以下指令添加到您的 build.gradle 中:

android {
    defaultConfig {
        ndk {
            moduleName "modulename"
            ldLibs "log"
        }
    }
}

关于android-studio - 无法使用 Android Studio 从 JNI 打印日志消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28004255/

相关文章:

android - 未找到改造注释。 (参数#1)

java - Android 运行时错误 : Library 'libmmparser.so' not found

android - Mupdf 仅支持 pdf 编译

c - 如何正确地将 http C 库包含到 Android 项目中?

Java JNA 将输出重定向到记录器

android - Gradle 实验性 JNI OpenCv arm 64-v8a 链接器错误

java - 无法在 SKMap 中设置流量模式

安卓工作室 :Failed to notify project evaluation listener

java - 在没有 Activity 的情况下运行 IntentService

android - ndk-build : undefined reference to . 。静态链接到 libxml.a 时出错