Creation a native code in Android Studio 3.3 - android

I have some problems in creation of native code in new version of Android Studio 3.3. In the previous versions it was easy: create a project and check the Include C++ support
But in the new version that option disappeared and everything changed. I’m trying to find how to include c++ support but I can’t. Can you help me, please

Have you tried with Native C++ option (bottom right)?

Take a look at this link: https://developer.android.com/studio/projects/add-native-code
i think they removed auto use for c++ in this version and you need to add it manually.

This has changed in the January 2019 release (version 3.3), as new projects use CMake by default:
For new projects, Android Studio uses CMake by default, but also supports ndk-build for existing projects. To learn how to include native code in your Android application, read Add C and C++ Code to Your Project. To learn how to debug native code with lldb, see Debug Native Code.
Following the link in the quote will give you an up-to-date walkthrough on how to add C++ code to your project.

Related

Create Android Studio compatible library from OpenGL C++ Project

I am currently trying to create a library file (e.g. .so) from an existing and working OpenGL C++ Visual Studio 19 project.
My plan is to create this library to call functions from the VS-project within Android Studio, as I am trying to extend the AR-Core sample "Hello_AR_c" which should ultimately create an android .apk containing my OpenGL C++ functionality.
I am using Windows 10-64 and Visual Studio19 as well as the current version of Android Studio.
I attempted to create a new Static Library (Android) project in VS and add it to my working solution. After that I included my existing source code files from my OpenGL project and tried to build the android project as ".so".
After fixing some errors which did not appear in the original project I struggled to get rid of the errors in "GLU.h", which prevents me from building it. Nevertheless I am unsure if solving the errors will lead to a compiling and correctly working version.
Error examples:
E0338 more than one instance of overloaded function "gluQuadricCallback" has 'C' linkage GLU.h 232
void APIENTRY gluQuadricCallback(GLUquadric *qobj,GLenum which, void (CALLBACK* fn)())
E0018 expected a ')' GLU.h 364
typedef void (CALLBACK* GLUquadricErrorProc) (GLenum);
I expect that this errors come from a different platform toolset or something similar, as in my android project it is Clang 5.0, where as in my working project it is set to Visual Studio 2017.
Does somebody have any idea for this problem of mine? Is there maybe an easier way to combine a visual studio OpenGL project with an Android studio project?
Thank you in advance.
Error examples:
Android Config - Not building:
OpenGl project - working:

Android Studio C++ NDK library example

I would like to download some working example of Android Studio NDK project with C or C++ which will be compiled to .so library (or APK from which I can extract .so).
I have tried ndkbuild with Android.mk build and also CMake with CMakeLists.txt, official and unofficial tutorials on Windows...
If I try them on android from same app (java), it is working but I want to use NDK in Unity3D on android and I keep getting DllNotFoundException.
I uploaded my Android and Unity projects to github.
First of all, here is a sample Android Project plugin you are looking for.
It's very important to know how to do this yourself.
This used to be hard to do before but the latest Android Studio made it easier to now use C/C++ or generate .so library easily. Update Android Studio to the latest version then follow steps below to create a C++ plugin.
1.Create a new Project in Android Studio
2.Check the Include C++ suport to enable C++.
3.On the Dropdown Menu, check C++ 11 on the C++ Standard drop-down menu. You will need C++ 11 to actually use most useful C++ features. Also, enable exception or frtti if you need them.
That's really it.
When you build the plugin:
The debug library is should be at:
\app\build\intermediates\cmake\debug\obj\
The release library is should be at:
\app\build\intermediates\cmake\debug\obj\
If you only see the debug but not the release version of the plugin, check here for how to make it appear.
Possible reasons you are getting DllNotFoundException on Android:
1.You did not wrap the C/C++ function around extern "C". You must do this for each function in the .cpp file or you do it in the function in the .h file.
2.You put the plugin in the wrong Unity folder.
The armeabi-v7a and x86 plugins generated at <ProjectDirectory>\app\build\intermediates\cmake\release\obj should be placed at Assets\Plugins\Android\libs\armeabi-v7a\ and Assets\Plugins\Android\libs\x86\ in the Unity project.
Make sure to spell these correctly. See this for more information about this.
3.You are loading it incorrectly from the C# side.
Let's say that the name of the plugin is libScreenshot.a, do not include the lib prefix, also do not add the .a when loading it.
[DllImport("Screenshot", CallingConvention = CallingConvention.Cdecl)]
public static extern void takeScreenshot();

Resolve deprecated NDK warnings in Android Studio

I have an Android app that uses OpenCV's Java wrapper for image processing. To add OpenCV to the project, I had to add the following line to gradle.properties:
android.useDeprecatedNdk=true
This had been working fine without any warnings or errors (that I know of) up until I updated my Android Studio and Gradle after a long time (I'm using Gradle 2.2.2 and Android Studio 2.2.2 now). Now when I build my app, I get the following warnings on the messages window:
Warning:Native C/C++ source code is found, but it seems that NDK
option is not configured. Note that if you have an Android.mk, it is
not used for compilation. The recommended workaround is to remove the
default jni source code directory by adding:
Warning:Deprecated NDK integration enabled by useDeprecatedNdk flag in
gradle.properties will be removed from Android Gradle plugin soon.
Even though the build is successful at this time, as the second warning informs, this is highly likely to cause some trouble in the future, so I want to resolve them as soon as possible before things go sideways.
Does anyone know how to get resolve these warnings? Or are there any other way to import OpenCV to an Android project, other than adding all the compiled code (the .so files) and setting the flag I mentioned earlier? Any help on this will be appreciated.
I would suggest to migrate over to using the native support with cmake. You could check the link which provides a step-by-step tutorial to add OpenCV using cmake via the following link.
You're code should stay the same without any changes, only necessary action is to figure out how to include them within the build process using CMakeLists.txt.
In the project pane, right click your app (in my React Native project it's the "app" catalog), and select "Link C++ project with gradle".
Now you must find the Android.mk file - it should be already generated in your_app/app/build/intermediates/ndk/debug/

Android NDK unable to debug native code with two gradle plugins

I'm using NDK with the experimental gradle plugin, and initially I was able to debug my native code.
Then I saw this issue https://github.com/googlesamples/android-ndk/issues/119 and this blog post http://frogermcs.github.io/json-parsing-with-flatbuffers-in-android/.
This approach would be great, because I could use the stable gradle plugin for android specific code, and the experimental gradle plugin for the native code, witch would be great to use use databinding for example.
I was able to use this structure, but I lost the ability to debug native code.
I have created a sample project that illustrate my problem.
https://github.com/4brunu/AndroidNDKDebug
In there you can find two projects.
The first one "hello-jni-one-gradle-plugin", only use the experimental gradle plugin, and I'm able to debug the native code.
The second one "hello-jni-two-gradle-plugins", I use the stable gradle plugin for android specific code, and the experimental gradle plugin for the native code, and I'm unable to debug the native code.
Am I doing something wrong?
Could you help me enable native code debug in the second project please?
Thanks
I'm using the same setup in one of my projects, with Android Studio 2.1-preview5, gradle-experimental 0.7.0-alpha5, and gradle plugin 2.1.0-alpha5, and debugging works.
The secret missing step is to add the path to your non-stripped libs to the debugger: lib/build/intermediates/binaries/release/obj/ABI

How to build static library from C++ using NDK on Android using Android Studio with Gradle?

I have a project written in C++ and I want to build a static library to be able to link against it in other projects. But Android Studio along with NDK produces only shared library objects which are for me not usable (I need static objects). I use gradle plugin to build the code.
While the code compiles I cannot find a way to get a "libname.a" file.
Anybody knows the magic behind this in Gradle ?
While not well familiar with the NDK Gradle supports building native static libraries. You can see an example in C++ with Gradle or learn more about it in gradle user guide Native Binaries chapter.

Categories

Resources