How to use memset_s in Android NDK? - android

android.ndk {
moduleName = "hello-jni"
stl = "stlport_static"
CFlags.add("-std=iso9899:2011") // I have also used "-std=c11"
ldLibs.addAll(["android", "log"])
}
I still cannot see memset_s in jni C code. It says undefined reference.
In my c code i have also included string.h, stdlib.h, and stdio.h and also
#define STDC_WANT_LIB_EXT1 1
Still cannot get rid of the error undefined reference error.
If i add the flag Allow_Undefined_symbols it compiles but when ever i call the function memset_s it crashes.
The questions i would like to ask are as follows :
1) In which of the Android NDK tool chains can we get the C11 memset_s api?
2) The other question i have is how can we change the default tool chain for android in the latest android studio with experimental gradle alpha5?

This functionally of "Annex K" in the C11 standard is optional. It is not implemented in many C libraries.
You can test for conformance to Annex K by means of the macro __STDC_LIB_EXT1__.

I was not able to find any tool chain that supports C11 for NDK. So i have used a compliant solution for c99 from the following link:
https://www.securecoding.cert.org/confluence/display/c/MSC06-C.+Beware+of+compiler+optimizations
Which you can customize for your own purpose. I am adding this solution for any one in need.

Related

Android NDK - deduced return types are a C++14 extension

I'm having an issue when compiling my project as an android library.
[jni/../../Src/Core\Util/Concurrent/WorkerThread.h:206:12] warning: 'decltype(auto)' type specifier is a C++14 extension [-Wc++14-extensions]
[jni/../../Src/Core\Util/Concurrent/WorkerThread.h:206:3] error: deduced return types are a C++14 extension
decltype(auto) wait() { return state->wait(); }
It seems related to the C++ version, but I'm not sure. I tried to; change the NDK version, change the C++ version, change the code itself.
I'm currently using Android NDK 23.1.7779620 & Clang 12 if not mistaken.
I'm not familiar with that so let me know if there is any information that I should add.

Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?

I'm trying to migrate some Java library from 'normal' JVM to android and stuck with some Java11 APIs used in the code.
The first thing I already got - Java11 language features seems to work only with Canary build of Android Studio, see answer here
Now I need to get understanding about which APIs can be really used. Here are two use-cases which do not work for me and I can't get if I'm doing something wrong or it never should work:
List.copyOf() - introduced in Java11, method copyOf is not available on android. Methods 'List.of()', introduced with Java 9, work OK.
class java.lang.invoke.LambdaMetafactory - introduced with Java 1.8 - to be used for programmatic creation of lambdas for usage instead for reflection, is not visible on Android.
I see both of them in sources of desugar_jdk_libs here:
https://github.com/google/desugar_jdk_libs/blob/master/jdk11/src/java.base/share/classes/java/lang/invoke/LambdaMetafactory.java
https://github.com/google/desugar_jdk_libs/blob/master/src/share/classes/java/util/List.java
So - the question is: how can I identify if some Java API is supposed to be available in 'desugared' android build or no? What really can be expected from 'desugaring'?
Steps to reproduce:
Using Android Studio Canary generate a dummy "Basic Activity" project
Make sure following is provided in build.gradle
android {
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
Add following lines somewhere in code
List<Integer> ints1 = List.of(1, 2, 3);
Supplier<List<Object>> listSupplier = () -> new ArrayList<>();
List<Object> alist = listSupplier.get();
List<Integer> ints2 = List.copyOf(ints1);
LambdaMetafactory.metafactory(null,null,null,null,null,null);
Last 2 lines fail to compile for me.
PS: final application is supposed to work on Android 10+.
Contrary to the other answer, desugaring is totally possible.
The dependency to add is
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
You can find more information at the official Android Java 8 desugaring documentation.
Desugaring lib is considered unofficial. We can't expect an exact answer. We get the feature when it is ready. Now List.copyOf() method now working with the latest Gradle version.
About the LambdaMetafactory class, It is not included in Android Javadoc. This means we assume we don't have LambdaMetafactory at all. Google stripped down some java API for being lightweight.
In general, We should check android Javadoc first. If android Javadoc has no mention about some API. We can be sure we won't get that feature anytime soon.

Android h3: A Hexagonal Hierarchical Geospatial Indexing System

I want to integrate h3 java binding library to my android app and I'm getting following exception:
java.lang.UnsatisfiedLinkError: No native resource found at /android-armv7l/libh3-java.so
at com.uber.h3core.H3CoreLoader.copyResource(H3CoreLoader.java:67)
Does anyone used this library before for Android OS?
Thank you.
Initially, following the intended usage as seen in their README should make it work. If it doesn't, see below.
Known Issue: Android, can't use library
UnsatisfiedLinkError: This can be encountered when the corresponding native library is not copied/detected in the project. Following NickRadu's workaround should make it work.
Below is a step-by-step guide.
Add a JNI folder in your project app folder and rename it jniLibs (app/src/main/jniLibs) (for some reason, having it named jni only doesn't work for me).
Get the H3 JAR (make sure you use the same version) and extract the JAR contents.
Copy the folders prefixed with android- and insert them in the jniLibs folder (from step 1).
Rename the copied folders, remove the android- prefix.
Add splits { abi { enable false } } to your app's build.gradle file (within android).
Done. In general, the library should now work as expected.
If during the app installation you encounter:
INSTALL_FAILED_NO_MATCHING_ABIS,
then depending on your test device, create a copy of the folder (along with its contents) and rename it as needed.
For example, a device running on arm64-v8a, I just made a copy of the arm64 folder and renamed it to arm64-v8a. Or if you're using an emulator, make sure that you're not using one with an x86 CPU.
D8 errors: Invoke-customs are only supported starting with Android O (--min-api 26), add these compile options in your app's build.gradle (within android -- note that it may change depending on your system's Java version)
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Note: It is best to test the app on multiple CPU architecture types first to see it's behavior.
To quickly see the CPU Architecture of the device, you could install Droid Hardware Info, or run a quick test code yourself.
Here's a test block I used and its corresponding result logs:
private fun testH3() {
val h3 = H3Core.newSystemInstance()
val lat = 37.775938728915946
val lng = -122.41795063018799
val resolution = 8
val hexAddr = h3.geoToH3Address(lat, lng, resolution)
val hex = h3.stringToH3(hexAddr)
val kRingsResult = h3.kRings(hexAddr, 1)
Log.d("H3Test", "geoToH3Address: $hexAddr")
Log.d("H3Test", "stringToH3: $hex")
Log.d("H3Test", "kRings: $kRingsResult")
}
Result:
D/H3Test: geoToH3Address: 8828308281fffff
D/H3Test: stringToH3: 613196570331971583
D/H3Test: kRings: [[8828308281fffff], [8828308281fffff, 882830828dfffff, 8828308285fffff, 8828308287fffff, 8828308283fffff, 882830828bfffff, 8828308289fffff]]
I made a sample project where the library works as expected. See android-uber-h3-sample.
Also be advised that the library will not work on Android api < 26 without some modifications to the code. The function that H3Core relies on to parse the hex long to hex string Long.parseUnsignedInt was not added to Android Java until api 26.

How to enable std::experimental::optional on Android

I'd like to use optionals in my Android app's C++ code, but I'm having trouble figuring out how to configure the STL to support either std::experimental::optional or std::optional. I get a build failure, "cannot find -lc++experimental," if I just add "-lc++experimental" to the cppFlags. This is what the externalNativeBuild portion of my Gradle file looks like presently.
externalNativeBuild {
cmake {
arguments "-DANDROID_STL=c++_shared"
cppFlags "-std=c++11", "-frtti", "-fexceptions"
}
}
How can I enable the use of std::optional or std::experimental::optional?
EDIT: I was able to download boost and use boost::optional instead, which was easy enough since it's a header-only library. Still, it would be nice to know how to to use one of the STLs that ships with the NDK, if that's possible. When I open up $ANDROID_NDK_HOME/sources/cxx-stl/llvm-libc++/include/experimental/optional, I see what looks like an implementation of std::experimental::optional, but it's surrounded by a lot of preprocessor statements that I imagine are disabling it.
std::optional is part of the C++17 STL. If your compiler supports it add -std=c++17otherwise I believe the correct flag is -std=c++1z.
See this discussion for more details.

Using -std=c++11 on VS2015

I have created a shared object for Android in Visual Studio 2015.
It works fine so far, but pop_back() for a wstring does not work:
wstring element = "JustATest!";
if (element.back() == L'!')
{
element.pop_back();
}
VS2015 tells me:
"no member named 'pop_back' in 'std::basic_string<wchar_t>'".
Can anybody tell me how to get rid of this error?
I have no idea why this should not work.
Is that because for some reason VS2015 does not use C++11 here?
Thank you for the help!
Edit: Another error:
When I try to use _wtoi, VS tells me: "use of undeclared identifier '_wtoi'.
Very very strange.
You need to turn on STL support. Turn on STL with Configuration Properties -> General -> Use of STL. Good options are LLVM libc++ static library (fewer features, more compatible with CLANG) and GNU STL static library (more features, I had an issue that required me to turn the CLANG optimizer to -Oz to prevent a segfault).

Categories

Resources