Android NDK, CMake with other libraries

后端 未结 2 1057
攒了一身酷
攒了一身酷 2021-02-07 11:49

So I am trying to build and test out a CMake with the Android NDK on Android Studio. I can get my library to compile, but it doesn\'t seem to want to pull any third-party depend

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-07 12:52

    The process could be little long it will depend on your android skills.

    An example could be similar to this process:

    • Crosscompile sfml.
    • Create your jni bridge
    • Generate with cmake the project and compile
    • Copy your files to android studio. create java loading library code.

    I guess that you have crosscompiled sfml and you know how works crosscompiling process, if I am wrong check these link below:

    Tutorial: https://github.com/SFML/SFML/wiki/Tutorial:-Building-SFML-for-Android

    Source code: https://github.com/SFML/SFML

    Toolchain: https://github.com/SFML/SFML/blob/master/cmake/toolchains/android.toolchain.cmake

    Changes on your cmake: add this file

    FIND_PACKAGE(SFML required)
    

    In cmake put your SFML build directory and cmake will fills your VARIABLES automatically for instance this variables:

    set(SFML_PATH ${ANDROID_NDK}/sources/sfml)
    set(SFML_LIB_PATH ${SFML_PATH}/lib/${ANDROID_NDK_ABI_NAME})
    
    set(SFML_LIB_SYSTEM ${SFML_LIB_PATH}/libsfml-system.so)
    set(SFML_LIB_AUDIO ${SFML_LIB_PATH}/libsfml-audio.so)
    set(SFML_LIB_GRAPHICS ${SFML_LIB_PATH}/libsfml-graphics.so)
    set(SFML_LIB_NETWORK ${SFML_LIB_PATH}/libsfml-network.so)
    set(SFML_LIB_WINDOW ${SFML_LIB_PATH}/libsfml-window.so)
    set(SFML_LIB_ACTIVITY ${SFML_LIB_PATH}/libsfml-activity.so)
    set(SFML_LIB_MAIN ${SFML_LIB_PATH}/libsfml-main.a) 
    

    There are two ways to make android studio native apps:

    Easy way: Create JNI bridge:

    Crosscompile your cmake script and copy your lib to app/src/main/jniLibs

    add library in execution time

    code:

    try
    {
      Log.v(LOG_TAG, "adding your library");
      System.loadLibrary(your_library);
    }
    catch(UnsatisfiedLinkError e)
    {
     Log.e(LOG_TAG,e.getMessage());
    }
    

    More complete way (it allows to debug library)

    Create your ndk module in gradle example

    android.ndk {
        moduleName = "your_library"
        cppFlags.add("-fexceptions")
        //cppFlags.add("-std=c++11")
        //cFlags.add("-fopenmp")
        cppFlags.add("-I" + file("src/main/jni").absolutePath)
        stl = "gnustl_shared" // Which STL library to use: gnustl or stlport
        ldLibs.addAll(["android", "EGL", "GLESv2", "dl", "log", "z"])
        String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi/"
        ldLibs.add(libsDir + "your_native_lib.so")
    }
    

提交回复
热议问题