【问题标题】:Can't print log messages from JNI with Android Studio无法使用 Android Studio 打印来自 JNI 的日志消息
【发布时间】:2015-03-16 06:08:28
【问题描述】:

我得到的错误:

错误:未定义对“__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')


    }

}

【问题讨论】:

  • 好的,我找到了解决方案。我必须在 include $(CLEAR_VARS) 之后放置 LOCAL_LDLIBS := -ldl -llog。
  • 它不需要紧跟在include $(CLEAR_VARS)之后。对于armapi 模块,它只需要在include $(BUILD_SHARED_LIBRARY) 之前。
  • 谢谢菲尔,你知道这是为什么吗?
  • 在你执行include $(BUILD_SHARED_LIBRARY)之前它不会运行任何东西,它只是为当前模块添加信息。
  • 哈,现在我明白了!这很明显,我没有考虑过。再次感谢您的帮助。

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


【解决方案1】:

将此添加到您的 android.mk 文件中

LOCAL_LDLIBS := -llog -ljnigraphics

【讨论】:

    【解决方案2】:

    我将 Gradle 2.5 与 Android Studio 1.4.1 一起使用,它适用于我的语法:

    ldLibs += "log"

    模型 build.gradle 应该是类似的:

    应用插件:'com.android.model.application' 模型{ 安卓 { compileSdkVersion = 23 buildToolsVersion = "23.0.1" defaultConfig.with { applicationId = "com.local.some.project" minSdkVersion.apiLevel = 17 targetSdkVersion.apiLevel = 23 版本代码 = 1 版本名称 = "1.0" } } android.buildTypes { 发布 { minifyEnabled = 假 proguardFiles += file('proguard-rules.pro') } } 安卓.ndk { moduleName = "我的模型" ldLibs += "日志" } compileOptions.with { sourceCompatibility = JavaVersion.VERSION_1_7 targetCompatibility = JavaVersion.VERSION_1_7 } } 依赖{ 编译文件树(包括:['*.jar'],目录:'libs') 编译'com.android.support:appcompat-v7:23.0.1' 编译文件('libs/some3rdParty.jar') }

    【讨论】:

      【解决方案3】:

      似乎使用 Gradle + Android Studio 会忽略 Android.mk 文件

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        • 1970-01-01
        • 2011-07-11
        • 1970-01-01
        • 2016-12-18
        • 2019-04-21
        • 2014-01-24
        相关资源
        最近更新 更多