Develop in C/C++

216 views
Skip to first unread message

mac actiaitalia

unread,
Jul 6, 2020, 11:38:59 AM7/6/20
to android-ndk
Hi everyone,

My company has to develop an API library (.so) in C / C ++ on the Android platform. The library can be incorporated into other Apps also written with different Framework (Xamarin, Flutter, Native Java / Kotlin etc.). The library will use to communicate with our external device in Bluetooth (BLE). I have not found an example to start to set up a C / C ++ project and to be able to access Bluetooth from here.
Do you have any suggestions?

Alias R. Cummins

unread,
Jul 6, 2020, 5:54:28 PM7/6/20
to andro...@googlegroups.com
Access to the bluetooth stack from native code is quite difficult. I'd suggest you call out to JNI for that, the Java/Kolin API will be much easier to use.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/0f3030ca-963d-4602-9948-fd26f968d26do%40googlegroups.com.

georg...@trimble.com

unread,
Jul 6, 2020, 5:54:28 PM7/6/20
to android-ndk
Qt? 

There's a QBluetooth API inside Qt which will give you access to Bluetooth through a C++ interface (the Qt signals and slots is required for that to work though). My organization however just called Java methods through JNI.

mac actiaitalia

unread,
Jul 7, 2020, 2:28:00 PM7/7/20
to android-ndk

Thank you for your suggestion. There is a link for tutorial or code example?

Regards


Il giorno lunedì 6 luglio 2020 23:54:28 UTC+2, Alias R. Cummins ha scritto:
Access to the bluetooth stack from native code is quite difficult. I'd suggest you call out to JNI for that, the Java/Kolin API will be much easier to use.

On Mon, 6 Jul 2020 at 17:38, mac actiaitalia <mac.act...@gmail.com> wrote:
Hi everyone,

My company has to develop an API library (.so) in C / C ++ on the Android platform. The library can be incorporated into other Apps also written with different Framework (Xamarin, Flutter, Native Java / Kotlin etc.). The library will use to communicate with our external device in Bluetooth (BLE). I have not found an example to start to set up a C / C ++ project and to be able to access Bluetooth from here.
Do you have any suggestions?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to andro...@googlegroups.com.

Glenn Kasten

unread,
Jul 7, 2020, 2:30:50 PM7/7/20
to android-ndk

Alias R. Cummins

unread,
Jul 7, 2020, 5:35:10 PM7/7/20
to andro...@googlegroups.com
Yeah, if you want a cross platform native library, Qt is a good choice.

Nevertheless, you might be making your life unnecessarily difficult by going this route.
As I understand it, Bluetooth access from the NDK is deliberately limited because of the
potential security problems it could call, also Bluetooh is surprisingly complex under the
hood.

I'd recommend you start with the Qt docs:
Docs here:
https://doc.qt.io/qt-5/qtbluetooth-index.html

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/07809b36-6345-45d1-a348-dbec69aeb28cn%40googlegroups.com.

Alias R. Cummins

unread,
Jul 7, 2020, 5:35:11 PM7/7/20
to andro...@googlegroups.com
SWIG can also help reduce the JNI workload:

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/7836cf9f-6e29-4bec-b28f-74158d9a2944o%40googlegroups.com.

mac actiaitalia

unread,
Jul 8, 2020, 2:30:21 AM7/8/20
to android-ndk
Yes, QT is also an alternative, but we didn't want to drag too many dependencies like QT's ".so" libraries.


Il giorno martedì 7 luglio 2020 23:35:10 UTC+2, Alias R. Cummins ha scritto:
Yeah, if you want a cross platform native library, Qt is a good choice.

Nevertheless, you might be making your life unnecessarily difficult by going this route.
As I understand it, Bluetooth access from the NDK is deliberately limited because of the
potential security problems it could call, also Bluetooh is surprisingly complex under the
hood.

I'd recommend you start with the Qt docs:
Docs here:
https://doc.qt.io/qt-5/qtbluetooth-index.html

On Mon, 6 Jul 2020 at 23:54, georg...@trimble.com <georg...@trimble.com> wrote:
Qt? 

There's a QBluetooth API inside Qt which will give you access to Bluetooth through a C++ interface (the Qt signals and slots is required for that to work though). My organization however just called Java methods through JNI.

On Tuesday, July 7, 2020 at 3:38:59 AM UTC+12 mac.act...@gmail.com wrote:
Hi everyone,

My company has to develop an API library (.so) in C / C ++ on the Android platform. The library can be incorporated into other Apps also written with different Framework (Xamarin, Flutter, Native Java / Kotlin etc.). The library will use to communicate with our external device in Bluetooth (BLE). I have not found an example to start to set up a C / C ++ project and to be able to access Bluetooth from here.
Do you have any suggestions?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to andro...@googlegroups.com.

Alias R. Cummins

unread,
Jul 8, 2020, 4:30:10 PM7/8/20
to andro...@googlegroups.com
Qt is also expensive to your bottom line.

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/833a7338-483a-4aa7-b919-b4dfeb8ce002o%40googlegroups.com.

mac actiaitalia

unread,
Jul 9, 2020, 3:17:36 AM7/9/20
to android-ndk
Yes, I know. if it is the simplest solution then it might be ok.

Now I'm trying a solution without QT. I am using Android Studio and created a simple project in JNI with a method that returns a jstring (Hello World :-)). I'm trying to use Xamarin as a UI, the library created in JNI is loaded, but the method that returns the jstring returns an exception. If I create a method that returns void, everything is fine. Do you have any idea about type compatibility and how to return strings from one environment to another?

Alias R. Cummins

unread,
Jul 9, 2020, 1:42:48 PM7/9/20
to andro...@googlegroups.com
Yes, I have a fair bit of experience in this but we are drifting dangerously close to the arena of paid consultancy work.

Could you detail the error?

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/78d83f77-0476-491b-a998-50952596e4fbo%40googlegroups.com.

Steven Winston

unread,
Jul 14, 2020, 2:44:13 AM7/14/20
to android-ndk
You can use bluez if you really want to.  But seriously, stop.  Use the Java library.  That handles the permissions in a way that wont make you crazy.

mac actiaitalia

unread,
Jul 14, 2020, 7:19:02 AM7/14/20
to android-ndk
Yes, I know :-D
It's crazy to use C ++ in android.
However I have to create a wrap in C++ that makes calls in Java, this is a prerogative of my company.

Andrew Esh

unread,
Jul 14, 2020, 8:39:47 AM7/14/20
to android-ndk
It's not crazy to use C++ on Android. I do it all the time. At the company I work for, I develop and maintain a large amount of C and C++ code that is dual platform: Linux and Android. If we implemented everything in Java on the Android platform, then we'd have two products to support.

On the Android platform we compile the C/C++ code within the Native Development Kit (NDK). If we need to do something that is best done on the Java side, we call out through the Java Native Interface (JNI) and make use of Java-provided services. We implement some of our functionality in Java so as to provide an API to our C/C++ code for those who want to call it from a Java environment.

Sometimes it is easier to use functionality on the Android platform that is on the Java side. Such functionality is probably the reference implementation provided by Google. The implementation language should not be taken as a mandate that the entire stack of software must be written in the same language. With the NDK, and the JNI, access to any functionality can be obtained, regardless of the implementation language.

A library written in C/C++ can be a full and equal partner to any other part of the application which is written in Java.

Alias R. Cummins

unread,
Jul 14, 2020, 11:17:25 AM7/14/20
to andro...@googlegroups.com
Actually you don't need Qt, you could do everything with SWIG bindings and a minimal skeleton app.

A

On Tue, 14 Jul 2020 at 13:55, Alias R. Cummins <alia...@gmail.com> wrote:
It's not crazy to use C++ on Android, it's just a bit more difficult.

There's 2 routs you could go:

 - Make your app GUI in Qt and call out to the bluetooth stack via the Qt JNI bindings
- Use bluez and do everything in native

If this is something that needs to do weird and advanced stuff with the Bluetooth stack, bluez might be a good option.

Do you need to also deploy on iOS? Is that why you want to use C++? Is the requirement for cross platform deployment?

A

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

mac actiaitalia

unread,
Jul 14, 2020, 11:17:39 AM7/14/20
to android-ndk
Ok. thanks Andrew, very clear!
My problem I don't have much experience in making the two languages interact with JNI. For this I would need examples and tutorials to better understand how to write methods in C / C ++ that can call Android functionality in Java.

Andrew Esh

unread,
Jul 14, 2020, 2:02:07 PM7/14/20
to android-ndk
There are many JNI examples among the NDK documentation on the Google Developer site: https://developer.android.com/ndk/samples/sample_hellojni  

Dime Bar

unread,
Jul 14, 2020, 5:34:31 PM7/14/20
to andro...@googlegroups.com
Unfortunately the latest Android 11 privacy file system is mostly in Java. 

Your gonna have to go through hops to get around the privacy settings and even then you might not be able to access data natively which is a shame. 



--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

mac actiaitalia

unread,
Jul 20, 2020, 10:35:39 AM7/20/20
to android-ndk
Hello everyone,

I have an Android test project in Android Studio with a part in C/C++. The example code is the following:


static JavaVM *_gJVM = nullptr;
static JNIEnv *_gpJNIEnv = nullptr;

extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
__android_log_print(ANDROID_LOG_DEBUG, "native-lib", "IN JNI_OnLoad");

_gJVM = vm;

_gJVM->GetEnv(reinterpret_cast<void **>(&_gpJNIEnv), JNI_VERSION_1_6);

__android_log_print(ANDROID_LOG_DEBUG, "native-lib", "OUT JNI_OnLoad");

return JNI_VERSION_1_6;
}

extern "C" JNIEXPORT int NativeTest3Wrap()
{
return Java_com_example_testrp1227_MainActivity_NativeTest3(_gpJNIEnv, nullptr);
}

Firtsly, I don't know if it's right call Java metod in this way:

extern "C" JNIEXPORT jint JNICALL
Java_com_example_testrp1227_MainActivity_NativeTest3(JNIEnv *env, jobject thiz)
{
__android_log_print(ANDROID_LOG_DEBUG, "native-lib", "IN NativeTest3");
jclass testCl;

if(thiz == nullptr)
{
testCl = env->FindClass("com/example/testrp1227/TestJavaCall");
} else
{
testCl = env->GetObjectClass(thiz);
}

__android_log_print(ANDROID_LOG_DEBUG, "native-lib", "NativeTest3 1");

jmethodID methodCall = env->GetStaticMethodID(testCl,"test1", "()I");

__android_log_print(ANDROID_LOG_DEBUG, "native-lib", "OUT NativeTest3");

return env->CallStaticIntMethod(testCl, methodCall);
}

This is Java method to call from C / C ++ that I have in the same project:

package com.example.testrp1227;

public class TestJavaCall
{
public static int test1()
{
return 20;
}
}

Secondly, onceI have compiled the project I get my ".so" library. At this point, I use Xamarin (Visual Studio 2019) to call the C/C++ NativeTest3Wrap method which should call the Java method, but it does not work. I get an exception in Xamarin:
 JNI DETECTED ERROR IN APPLICATION: JNI GetStaticMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class "com.example.testrp1227.TestJavaCall" on path: DexPathList[[zip file "/data/app/com.ACTIA.Diag4App-RjGvk1bh4b94eSR59z7OxA==/base.apk"],nativeLibraryDirectories=[/data/app/com.ACTIA.Diag4App-RjGvk1bh4b94eSR59z7OxA==/lib/arm64, /data/app/com.A

But, I have inserted only the ".so" file in the Xamarin project, maybe I should also insert the jar file of the Android Studio project?

Thanks
To unsubscribe from this group and stop receiving emails from it, send an email to andro...@googlegroups.com.

Alias R. Cummins

unread,
Jul 20, 2020, 5:51:15 PM7/20/20
to andro...@googlegroups.com
Please explain where you are adding the .so files in the project. 

Normally this would be in main/jni/{your target architecture, e.g. ARM64-8a}

However it's better to use Android Studio backed by gradle aware make because it's supported by Google. 

If you could post both your gradle build files that would help too.

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/207b4f68-76a8-4b00-8e8a-df233317215bo%40googlegroups.com.

mac actiaitalia

unread,
Jul 21, 2020, 7:27:44 PM7/21/20
to andro...@googlegroups.com
In attachment there is an Android Studio Test project. In "app" project there is cpp part "native-lib.cpp", in "testlib" module there is a java class with a method that I have to call by C/C++ function.
But, my issue is how could I reuse this library (".so" and ".aar") in a Xamarin Project using the C/C++ function?

Thanks
Giulio.


Raul Piper

unread,
Jul 21, 2020, 7:27:55 PM7/21/20
to andro...@googlegroups.com
One simple questions: do you have the sources of .so?, 
One suggestion : Never ever use jni to access ble in Android. Android has already given you sdk to access the hardware.
And even if you are able to use jni to access the ble, your solution will not be long lasting on Android devices unless you are shipping the solution with your own custom Android.
So to save your time i will suggest - DO NOT USE JNI with BLE . On top of it you are using Xamarin . This is disastrous !! Sorry for being so straight !
You can PM me for detailed discussion.
I
Regards,
Rp

Alias R. Cummins

unread,
Jul 22, 2020, 11:08:55 AM7/22/20
to andro...@googlegroups.com
I agree with Raul with regard to Xamarin. It will offer absolutely nothing to your project, and will only cause you to be locked into the
Microsoft toolchain. If you genuinely want to develop a cross platform library, do it on Linux.

If it works on Linux, it will work everywhere else.
PM me if you would like a private consultation.
ad...@ufidstudios.ch

Best regards
Alias Cummins

mac actiaitalia

unread,
Jul 22, 2020, 11:08:55 AM7/22/20
to andro...@googlegroups.com
Hi,

The ".so" library is in Android Project that I have already attached. It is the "cpp" part of the project.
Yes, I know that could be a disaster to use all these frameworks :-D, but my company has to distribute a library based on automotive diagnostics specifications RP1227. These specifications require C/C++ to be used as the base language. Once the ".so" library has been generated it will be distributed to our customers, they will then be able to develop their interfaces independently.

Regards
Giulio.






Steven Winston

unread,
Jul 22, 2020, 1:20:19 PM7/22/20
to android-ndk
There's been a bunch of us suggesting using the Java framework. With
very few answering the "how to use" the Java framework. Here's a very
quick effort surrounding that:

https://stackoverflow.com/questions/28622036/android-ndk-calling-java-functions-from-c/28638203
https://developer.android.com/ndk/samples/sample_hellojni

My personal preference when working with system calls like this that
Google has secured by restricting is by using as little Java code as I
can get away with. I start with something like this:
https://developer.android.com/ndk/samples/sample_na

Then I transition to wrapped calls that are similar to this:
https://stackoverflow.com/questions/28622036/android-ndk-calling-java-functions-from-c/28638203

If that's not your cup of tea, then there's an automated way to do it:
https://www.scapix.com/documentation/java_link/

If you implement your code in this way, then you will never have to
worry about the paranoid permission issues.
> You received this message because you are subscribed to a topic in the Google Groups "android-ndk" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/android-ndk/662_ojD6ne8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to android-ndk...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAP5h4q9nyDYrQSZr0_EW4cEUC9_gZgj56T6UoOnonEme50BvvQ%40mail.gmail.com.

Alias R. Cummins

unread,
Jul 22, 2020, 2:54:17 PM7/22/20
to andro...@googlegroups.com
If that's not your cup of tea, then there's an automated way to do it:
https://www.scapix.com/documentation/java_link/


Ooh, Java link looks very interesting!

How does it compare to SWIG?


 

Dirar Abu-Saymeh

unread,
Jul 22, 2020, 2:54:17 PM7/22/20
to andro...@googlegroups.com
Hi,

I use a JNI .so library in one of my Xamarin projects and this is what I had to do to get it to work:

Create a folder structure like this in the Xamarin project:

lib
|
|_ arm64-b8a
|_ armeabi-v7a

and place the .so library into the appropriate folder based on architecture.
Set the build action on the .so file to be “Android Native Library”

in the OnCreate method of the main activity load the library:
JavaSystem.LoadLibrary(“teslib”);

without this, the OnLoad method for JNI is never called.

I don’t have a jar or aar file in my projects so I’m not sure what to do with that. But maybe this helps you: 

Regards

Alias R. Cummins

unread,
Jul 22, 2020, 2:54:18 PM7/22/20
to andro...@googlegroups.com
OK, that's a different requirement, which can be satisfied without relying on xamarin.

I'm running your example in my IDE and I'm getting the error:
2020-07-22 13:04:37.363 22456-22456/com.example.testrp1227 E/mple.testrp122: Unknown bits set in runtime_flags: 0x8000

Are you seeing the same problem?

A

mac actiaitalia

unread,
Jul 22, 2020, 2:54:18 PM7/22/20
to andro...@googlegroups.com
Ok,  I agree with you. However I don't know what frameworks my clients will use.
The reason why I use Xamarin is to be cross platform. The reason why I use C/C++ in Android is to be compliant with Automotive specification RP1227.
I know it doesn't make sense, but I have to comply with the specifications

Thanks
Giulio.


JB Data31

unread,
Jul 22, 2020, 4:20:21 PM7/22/20
to andro...@googlegroups.com
As a NDK skill, I have deved an APK that calls the tesseract API with my own JNI mapping.

Do you pass a first step : dev a minimal java main that calls your native C++/C method.
1st line : System.loadLibrary("native-lib");
2nd line : the call of your native.

@JBΔ



mac actiaitalia

unread,
Jul 23, 2020, 10:45:46 AM7/23/20
to andro...@googlegroups.com
Hi everyone,

About my test project, don't run the Java part on your smartphone because I already know it doesn't work. I am using this Android Project only to compile the C/C++ part, I don't use the MainActivity of Android Studio to test the cpp part. My need is to run the part in C/C++ in an external project like Xamarin, because it is the one I know how to use best. If I can get it to work there then our customers who use Xamarin, Flutter or other Framework can use my library.
My current successes are to be able to compile the part in C/C++ and get the ".so" library. Then I import the ".so" files (with the various arm64/armeabi and x86/x86_64 platforms) into Visual Studio with a Xamarin test project. From here I can call, with Java.Lang.JavaSystem.LoadLibrary and Pinvoke, and I can execute the C/C++ functions created in Android Studio.
My question now is to create C/C ++ functions that use JNI to call Android API and use Bluetooth for example.

Thanks
Giulio


Alias R. Cummins

unread,
Jul 23, 2020, 10:45:46 AM7/23/20
to andro...@googlegroups.com
I must agree with the others - you can certainly do cross platform, and comply
with the automotive standards, *without* using xamarin - Xamarin will actually
make it harder to be cross platform because it will prevent you from using the
latest APIs and you'll be using a non-standard build system.

If you develop a minimal API it will be easier to plug into other types of frameworks.
If you depend on xamarin from day one you will likely make life difficult for your clients.

A

mac actiaitalia

unread,
Jul 23, 2020, 11:37:36 AM7/23/20
to andro...@googlegroups.com
Hi Alias,

I have difficulty making myself understood, because English is not my main language... Sorry :-)
Mainly my company works with Microsoft and recently we are facing into the mobile world. Since we have many skills with the Microsoft .Net world, it seemed natural to use Xamarin. Now things are changing because we need to use C/C++ and make direct calls with the Android API to be compliant with this automotive specification. We are using Xamarin only to be faster in testing this new library developed in C / C ++ in the Android environment, but it doesn't necessarily mean that our customers will use Microsoft.

Regards
Giulio.


Reply all
Reply to author
Forward
0 new messages