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
With Android Studio 3.6, including libpng into your project is rather easy.
org.libpng.libpng
, same as MacOS). Keep it Java, and choose the min SDK that fits your app.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.
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:
git clone https://github.com/julienr/libpng-android.git
into ${YOUR_LIBS_FOLDER}
(I used ${ANDROID_NDK_ROOT_DIRECTORY}/sources/android
).${ANDROID_NDK_ROOT_DIRECTORY}
(home/username/Android/sdk/ndk-bundle
for me) to global $PATH
variable which is needed for build script).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).${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.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.libpng z
instead of libpng
in target_link_libraries
.