Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio External Native Build Precompiled Headers

I'm porting a project from NDK build to Android Studio.

Currently the project has a precompiled header attached (with NDK build), which speeds up compilation time a lot.

I'm looking for a way to add precompiled header (.pch) to my external native build within Android Studio project. I'm using CMake.

like image 409
Kristupas A. Avatar asked Aug 23 '17 11:08

Kristupas A.


People also ask

How do I use prebuilt .so lib files?

Declare a prebuilt library Give the module a name. This name does not need to be the same as that of the prebuilt library, itself. In the module's Android.mk file, assign to LOCAL_SRC_FILES the path to the prebuilt library you are providing. Specify the path relative to the value of your LOCAL_PATH variable.

Can you use c++ on Android Studio?

You can add C and C++ code to your Android project by placing the code into a cpp directory in your project module. When you build your project, this code is compiled into a native library that Gradle can package with your app.

What is native c++ in Android Studio?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.

How does JNI work on Android?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).


1 Answers

I am in the same situation and I have found several user-made script to do that.

I would recommend this one:

https://github.com/larsch/cmake-precompiled-header

In order to make it work for the latest ndk-tools which uses clang, copy paste the content of this pull request:

https://gist.github.com/leeor/316168500765cf51ae97

into the PrecompiledHeader.cmake file (replace everything in this file, except if you want to keep GCC/ MSVC compatibility, then just you will have to merge the two files which shouldn't be hard).

It should work, except some special CMAKE_CXX_FLAGS that triggers an error in the export_all_flags function, and that I don't know how to fix yet. I'll keep you updated with my progress.

like image 89
downstroy Avatar answered Nov 15 '22 20:11

downstroy