First, i want to thank all guys above, without your inspiration, i cannot get this answer.
I write this answer down NOT becase those answers are wrong, i just want to help newbies like me, setp by step, with all details should know.
This answer was proved in Android Studio 3.2.1 with CMake 3.4.1, i triple checked.
Now if you create a new project with C++ support, click "next" all the way, you will get a CMakeLists.txt
, whick should looks like :
cmake_minimum_required(VERSION 3.4.1)
add_library(
native-lib
SHARED
src/main/cpp/native-lib.cpp)
find_library(
log-lib
log)
target_link_libraries(
native-lib
${log-lib})
Change SHARED to STATIC won't output any .a
file, you will get nothing, even no .so
file( of course, there is no SHARED library be added in this file).
This is Gradle/Android Studio 's "fault", the answers above already mentioned it, if you use CMake alone, you'll definetely get a .a
file.
OK, now is my TRICK:
We already have a add SHARED library, then we add a STATIC library, which uses the same source file. Add below in your CMakeLists.txt
:
add_library(
native-lib-static
STATIC
src/main/cpp/native-lib.cpp
)
"native-lib-static" can be replaced with any name but "native-lib" since it is used for the SHARED version.
Change your target_link_libraries
like this:
target_link_libraries(
native-lib
native-lib-static
${log-lib})
Gradle->app->build->assembleDebug/assembleRelease
Then you'll get libnative-lib-static.a
, in
app\.externalNativeBuild\cmake\debug(release)\<abi>\libnative-lib-static.a
This path is set in app\build.gradle
:
android{
defaultConfig{
externalNativeBuild{
CMake{
ATM i'm not sure whether Google will change it in the future, but you can always search the project folder for *.a
files.
I don't think i miss anything this time.
All credit to @Michael @Dan Albert @Alex Cohn @shizhen
app/.externalNativeBuild/cmake/buildVariant/abi/src/main/cpp/
(wherebuildVariant
is one of your build variants, andabi
is one of the ABIs you're building for). – Duteous