[FIXED] Android NDK ImageDecoder functions requires build error

Issue

I’m trying to get byte data of png file for GLES texture.
I have my png file in app\source\main\assets.

Texture::Texture(const char *path) {
    AAsset* asset = loadAsset(path);

    AImageDecoder* decoder;
    int result = AImageDecoder_createFromAAsset(asset, &decoder);
    if(result != ANDROID_IMAGE_DECODER_SUCCESS){
        __android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor create decode FAIL. path: &s", path);
    }

    const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
    this->width = AImageDecoderHeaderInfo_getWidth(info);
    this->height = AImageDecoderHeaderInfo_getHeight(info);

    size_t stride = AImageDecoder_getMinimumStride(decoder);
    size_t size = height * stride;
    void* pixels = malloc(size);
    result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
    if(result != ANDROID_IMAGE_DECODER_SUCCESS){
        __android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor decode image FAIL. path: &s", path);
    }

    //GLES create texture and bind data(pixels)

    free(pixels);
    AAsset_close(asset);
}

but I got following build errors:

Build command failed.
Error while executing process C:\Users\jaewo\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\.cxx\cmake\debug\x86 native-lib}
ninja: Entering directory `C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\.cxx\cmake\debug\x86'
[1/2] Building CXX object CMakeFiles/native-lib.dir/Texture.cpp.o
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:20:122: warning: data argument not used by format string [-Wformat-extra-args]
        __android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor create decode FAIL. path: &s", path);
                                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  ^
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:32:121: warning: data argument not used by format string [-Wformat-extra-args]
        __android_log_print(ANDROID_LOG_VERBOSE, "Android2", "Texture constructor decode image FAIL. path: &s", path);
                                                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  ^
2 warnings generated.
[2/2] Linking CXX shared library C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\build\intermediates\cmake\debug\obj\x86\libnative-lib.so
FAILED: C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/build/intermediates/cmake/debug/obj/x86/libnative-lib.so 
cmd.exe /C "cd . && C:\Users\jaewo\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=i686-none-linux-android30 --gcc-toolchain=C:/Users/jaewo/AppData/Local/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/jaewo/AppData/Local/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security  -std=c++17 -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o C:\Users\jaewo\AndroidStudioProjects\ANdroid2\app\build\intermediates\cmake\debug\obj\x86\libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o CMakeFiles/native-lib.dir/AssetManager.cpp.o CMakeFiles/native-lib.dir/Texture.cpp.o  -llog -landroid -lGLESv3 box2d/libbox2d.a -latomic -lm && cd ."
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:18: error: undefined reference to 'AImageDecoder_createFromAAsset'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:23: error: undefined reference to 'AImageDecoder_getHeaderInfo'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:24: error: undefined reference to 'AImageDecoderHeaderInfo_getWidth'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:25: error: undefined reference to 'AImageDecoderHeaderInfo_getHeight'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:27: error: undefined reference to 'AImageDecoder_getMinimumStride'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:30: error: undefined reference to 'AImageDecoder_decodeImage'
C:/Users/jaewo/AndroidStudioProjects/ANdroid2/app/src/main/cpp/Texture.cpp:35: error: undefined reference to 'AImageDecoder_delete'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

in app build.gradle file, I set minSDKVersion to 30.
I think I should link a library about ImageDecoder or do something in my CMakeLists.txt.
but I’m not sure.

target_link_libraries(
        native-lib

        log
        android
        GLESv3
        box2d)

Solution

You need to link jnigraphics.

For example:

target_link_libraries(
        native-lib

        log
        android
        GLESv3
        box2d
        jnigraphics)

There’s an example at the image-decoder sample project.

Answered By – Patrick Martin

Answer Checked By – Katrina (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published