Can't print log messages from JNI with Android

2019-02-17 00:45发布

Error I'm getting:

error: undefined reference to '__android_log_print'

I've already added this line to my .cpp file:

#include <android/log.h>

I've tried with both: __android_log_print and __android_log_write

I've also linked against the logging library, in my Android.mk file (which I'm manually compiling).

LOCAL_LDLIBS := -llog

I've also tried several alternatives I found like:

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

Here's my 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

And here is the build.gradle file:

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')


    }

}

3条回答
别忘想泡老子
2楼-- · 2019-02-17 01:41

I use Gradle 2.5 with Android Studio 1.4.1 and it works for me with this syntax:

ldLibs += "log"

The model build.gradle should be something similar:


apply plugin: 'com.android.model.application'

    model{
        android {
            compileSdkVersion = 23
            buildToolsVersion = "23.0.1"

            defaultConfig.with {
                applicationId = "com.local.some.project"
                minSdkVersion.apiLevel = 17
                targetSdkVersion.apiLevel = 23
                versionCode = 1
                versionName = "1.0"
            }
        }

        android.buildTypes {
            release {
                minifyEnabled = false
                proguardFiles += file('proguard-rules.pro')
            }
        }

        android.ndk {
            moduleName = "MyModel"
            ldLibs += "log"
        }

        compileOptions.with {
            sourceCompatibility = JavaVersion.VERSION_1_7
            targetCompatibility = JavaVersion.VERSION_1_7
        }
    }

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.android.support:appcompat-v7:23.0.1'
        compile files('libs/some3rdParty.jar')
    }
    
查看更多
Deceive 欺骗
3楼-- · 2019-02-17 01:45

ADD this in Your android.mk file

LOCAL_LDLIBS := -llog -ljnigraphics

查看更多
霸刀☆藐视天下
4楼-- · 2019-02-17 01:48

It seems that using Gradle + Android Studio the Android.mk file is ignored.

As explained here, try adding the following directive to your build.gradle:

android {
    defaultConfig {
        ndk {
            moduleName "modulename"
            ldLibs "log"
        }
    }
}
查看更多
登录 后发表回答