Add reference to CMake project in Android Studio - android-studio

In my particular case, I implemented a cross platform project in C++11 that uses CMake. I initially designed this project to work on PC (Windows, Unix), but now I realized that I can use it on mobile platforms too, without re-implementing the business logic on every platform. The project uses standard libraries: Boost, Poco, OpenSSL, Protobuf.
After a few searches I came to conclusion that this is not even an usual mode to put together native and managed code in Android.
Is it possible to add reference in a Gradle project in Android Studio to a native project that uses CMake?
Can NDK adopt the project in a simple manner if I compile everything in command line (assuming I don't use Android Studio)?
If it is possible, is it recommended at all?

Is it possible to add reference in a Gradle project in Android Studio to a native project that uses CMake?
Since Google announced Android Studio 2.2 that comes with cmake plugin, so it's possible to reference CMake project to android project.
Can NDK adopt the project in a simple manner if I compile everything in command line (assuming I don't use Android Studio)?
In my experience, i compile NDK in separate way and then link the static/shared library (.so file) to be used in android project. I used NDK while developing PDF Reader using mupdf here
Hope this helps.

it would be possible with android studio 2.2, mainly the android plugin for gradle makes it possible. you could look at some examples in:
https://github.com/googlesamples/android-ndk/tree/master-cmake
android studio directly reads-in your cmake scripts -- there is nothing like desktop systems that generate project files for their native IDEs ( Visual Studio or XCode ); so you might have to modify your existing cmake files to eventually make it to work for android studio.
as long as you build to the correct APK, command line or IDE does not matter too much. If you intend to ship source code, having your project ready with an IDE would be attractive.

Please see CMaker_Boost, build the Boost with the CMake at a configure time. Now it is tested on the Linux and Android, gcc and clang. This can be included to the gradle project in the Android Studio. I hope this helps.

Related

CMake to create a Visual Studio Code project

Does any know if there are any efforts for cmake to generate a Visual Studio Code project (not to be confused with Visual Studio)? IE
cmake -G "Eclipse CDT4 - Unix Makefiles"
not for eclipse, but for Visual Studio Code? This would be awesome for linux projects.
As already mentioned, VS Code has the vscode-cmake-tools extension to integrate with CMake projects.
See here for documentation on what features it has to offer. You will see that building and debugging within the IDE are supported.
The design of VS Code is very much centered around being simple and extensible, whereas Visual Studio is more of a monolith and includes its own build system for C++. VS Code is not monolithic like that.

Building tess-two into a project using Android Studio's gradle build

There are answers about getting the tess-two project integrated into an Android project within Android Studio, but many are out-dated and none used the current capabilities defined here:
Using Android Studio 2.2 and higher, you can use the NDK to compile C and C++ code into a native library and package it into your APK using Gradle, the IDE's integrated build system. Your Java code can then call functions in your native library through the Java Native Interface (JNI)
What specific steps would be required to make tess-two functionality a part of the resulting APK, using functionality within Android Studio, rather than external file placement, manipulation and command line tools? So taking the building, creation of *.so files into the IDE. Specifically using just Android Studio's integrated build system (Gradle) as described here.
1. Start a new project
2. Import Tess-Two into the project
2b. Add any needed plug-ins
3. Add code in the main activity to get native functionality
4. Configure Android Studio build so that native functionality is available
This is where the specifics are required
4. Configure Android Studio build for native functionality
4a. Link C++ Project with Gradle
First, check the tess-two project for build files. You may select CMakeLists.txt or Android.mk files. Both are currently supported.
In this case, I used ndk-build, which seemed like a good bet for integrating the native code.
For more information, see Android Studio documentation
4b. Manage Long Commands
In Windows, errors may be encountered if the command length grows too large. To prevent problems, use LOCAL_SHORT_COMMANDS AND APP_SHORT_COMMANDS in the Android.mk file.
The "e=87" error is what you are avoiding by doing this:
For more on that topic, see stackoverflow question about error 87.
4c. Add Module Dependency for tess-two
In File > Project Structure > Dependencies use the + to add the tess-two dependency:
4d. Build the Project and check .apk file for .so files
The build, which takes a long time, should complete now. Validate that the .apk file contains the .so files, created during the build. With the tess-two libraries in an static initializer, run the project on your Android device:

Does Android Studio support Code completion for C/C++?

The code completion is fine on Android Studio java project. But I want to use Android Studio to edit some existing C file(.cpp).
for example, if i have declared some function, then i type the function in other section. But the android studio doesn't show up the code suggestion or some debugging.
Can Android Studio support Code completion for C/C++ like this:?
Yes it does, as long as the project is setup to use the NDK and the C/CPP sources are included in the project.
Try pulling the ndk sample repo and try opening one of the projects.
If you have a java project and want to edit some arbitrary C or C++ source, then no. It does not have enough information about the source file, include paths, compiler etc...
If your project uses Android.mk to build, it will be a bug of Android Studio. I have been reported.
Android.mk project cannot show code completion for cpp.
It seems that old version of Android Studio does not support code completion of jni native build (with Android.mk as configuration file), and that should already be fixed now.
But if you are using CMake in your project as external building tool and code completion still does not work, It's probably because you have more than one version of cmake installed. You can try to remove extra ones from SDK manager and restart Android Studio, it should work.
To see installed CMake version, you can open the SDK manager and check the 'show package details' on the right bottom. You can view cmake tools installed with different versions.

Configure android ndk for android studio code suggestion

I'm new to android NDK and just know how to use native code in an java project as in this tutorial: http://kn-gloryo.github.io/Build_NDK_AndroidStudio_detail/
But when writing in C++ the IDE features for code completion, suggestion and other stuff disappear, while in java those features work as usual.
I tried to import some ndk sample from google, they are all written in C++ instead of calling native code from JNI as I'm doing, and the projects support code completion.
Can anyone show me a way to configure Android Studio for code completion to work when developing with NDK and JNI?

Android studio 1.4 Library module with JNI

I'm trying to create an android module with JNI in android studio, but all the documentation I found is for older versions of android studio that did not officially support JNI. So, what is the correct way to create Library module with JNI in Android studio 1.4?
It is exactly same as with 'com.android.model.application'. Everything seems to work fine except that for me, native debugging required running NDK build steps in the app module.

Resources