Building+linking libpng in Android Studio with CMake on Windows

前端 未结 2 632
花落未央
花落未央 2021-01-03 07:03

I\'m trying to start a native app project with Android Studio and CMake on Windows 10, but I\'m stuck at including libpng.

For starters it\'s the first time I\'ve se

相关标签:
2条回答
  • 2021-01-03 07:37

    With Android Studio 3.6, including libpng into your project is rather easy.

    • Download the latest sources from sourceforge and unpack the zip file.
    • In Android Studio, make sure you have NDK and CMake installed (in SDK Manager)
    • Create a new Android Library module, call it pngAndroid (I would recommend to set the Package Name to org.libpng.libpng, same as MacOS). Keep it Java, and choose the min SDK that fits your app.
    • For this module, choose File/Link C++ Project with Gradle. Find CMakleLists.txt of libpng that you downloaded as step 1.
    • In the pngAndroid's build.gradle, add

      android.defaultConfig.externalNativeBuild.cmake.targets "png_static"
      
    • In Project Structure dialog, specify module dependency between your Module and pngAndroid.

    When you build the pngAndroid Module, you will see files libpng16d.a in build/intermediates/cmake/debug/obj directory under pngAndroid.

    In your Module, you also have CMakleLists.txt. In this script, you have a target, e.g.

    add_library(native-library SHARED …
    

    You should add libpng as dependency of native-library:

    target_link_libraries(native-library libpng)
    

    Elsewhere in this CMakleLists.txt you should define the imported libpng:

    add_library(libpng STATIC IMPORTED)
    set_target_properties(libpng PROPERTIES IMPORTED_LOCATION
        ../pngAndroid/build/intermediates/cmake/debug/obj/${ANDROID_ABI}/libpng16d.a)
    set_target_properties(libpng PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
        ../../lpng1637)
    set_target_properties(libpng PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES
        z)
    

    Note that here I used relative paths a) to the pngAndroid Module that you added to your Android Studio project, and b) to the libpng sources that you downloaded. In your setup, the relative paths may be different.


    For completeness, here is a cleaned-up build.gradle script for pngAndroid to build shared library libpng16d.so:

    apply plugin: 'com.android.library'
    
    android {
        compileSdkVersion 29
        defaultConfig {
            minSdkVersion 16
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
            externalNativeBuild {
                cmake {
                    arguments "-DSKIP_INSTALL_ALL=YES"
                    targets "png"
                }
            }
        }
    
        externalNativeBuild {
            cmake {
                path file('../../lpng1637/CMakeLists.txt')
            }
        }
    }
    

    When you build the module, you get in build/outputs an aar file that contains the jni directory with armeabi-v7a/libpng16d.so and all other ABI variants.


    Last, it's worth mentioning that Google is working on an easier native package manager. Unfortunately, it's still not released.

    0 讨论(0)
  • 2021-01-03 07:41

    I've managed libpng to work with android NDK app (CMake build system instead of Android.mk) as static library. I used libpng-android repackaging. Here are things I've done:

    1. git clone https://github.com/julienr/libpng-android.git into ${YOUR_LIBS_FOLDER} (I used ${ANDROID_NDK_ROOT_DIRECTORY}/sources/android).
    2. Add ${ANDROID_NDK_ROOT_DIRECTORY} (home/username/Android/sdk/ndk-bundle for me) to global $PATH variable which is needed for build script).
    3. Build lib with ndk-build (there is ./build.sh for that in directory with lib). Library will be built for different ABI targets ( arm64-v8a, armeabi, x86_64 etc).
    4. At this point you have library headers at ${YOUR_LIBS_FOLDER}/libpng-android/jni and libpng.a at ${YOUR_LIBS_FOLDER}/libpng-android/obj/local/${ANDROID_ABI}/, where ${ANDROID_ABI} is target platform.
    5. Finally you could include lib at CMakeLists.txt. libpng requires zlib compression library so you need to link against it aswell (zlib is provided by android studio so just add -lz flag).

    Here is related piece from my CMakeLists.txt:

    add_library(libpng STATIC IMPORTED)
    set_target_properties(libpng PROPERTIES IMPORTED_LOCATION
        ${YOUR_LIBS_FOLDER}/libpng-android/obj/local/${ANDROID_ABI}/libpng.a)
    
    add_library(appManager SHARED src/main/cpp/appManager.cpp)
    
    target_include_directories(appManager PRIVATE ${YOUR_LIBS_FOLDER}/libpng-android/jni/)
    
    target_link_libraries(appManager
                           android
                           libpng
                           z)
    

    Few things to note there:

    • ${ANDROID_ABI} is a variable set by Android Studio build system.
    • Once again: you need to link against zlib, that is why we have libpng z instead of libpng in target_link_libraries.
    0 讨论(0)
提交回复
热议问题