Now i create a new project include c++ support in Android Studio 3.2, it has native-lib.cpp
naturally, the CMakeLists.txt
looks like this:
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp)
now if i build this project, i can find libnative-lib.so
in some "debug" directoies, that's OK, but i want static library ie .a
files.
Change SHARED to STATIC won't generate those files, what else should i do?
The CMake docs does not mention other way than add_library()
.
Every docs i searched only talk about SHARED/STATIC
.
What did i miss?
You can have the static library by setting your lib as
STATIC
and the generated.a
is located at<project-dir>/build/intermediates/cmake/debug/obj/<abi>/libnative-lib.a
But, like @Dan Albert said, this static lib itself is not much useful. You still need to link it with your other parts of code/libs to come out a final shared library, so that your Android app can use.
For example,
Then on your Java side, you can use this shared library, i.e.
libmy-shared-native.so
.Adding to @DanAlbert 's comment, the question is not how useful a static library alone can be. The issue is that Android Studio via its Gradle plugin, by default, will not build a CMake target that is a STATIC library.
Luckily, you can explicitly specify the targets you want to produce, e.g.
Actually, there is not much added value building the static library via integrated externalNativeBuild. You can achieve same if you run
cmake --build
from command line.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 :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
:"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:Gradle->app->build->assembleDebug/assembleRelease
Then you'll get
libnative-lib-static.a
, inThis path is set in
app\build.gradle
: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