Building+linking libpng in Android Studio with CMake on Windows

前端 未结 2 652
花落未央
花落未央 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.

提交回复
热议问题