ARES_LIBRARY_INIT_ANDROID(3)
π ARES_LIBRARY_INIT_ANDROID π
okay so bestie, welcome to the Library Functions Manual, section 3, where we talk about ares_library_init_android β the c-ares library Android initialization. this is the page where Android and c-ares finally get introduced to each other π«Ά
πΈ NAME πΈ
ares_library_init_android β c-ares library Android initialization
β¨ SYNOPSIS β¨
#include <ares.h>
int ares_library_init_android(jobject connectivity_manager)
int ares_library_android_initialized();
void ares_library_init_jvm(JavaVM *jvm)
π DESCRIPTION π
so the ares_library_init_android(3) function does all the initializations that the c-ares library needs internally when you're running on Android. it can happen any time after ares_library_init(3), no rush, but it must happen after ares_library_init_jvm. and lowkey important: ares_library_init_android has to be called before DNS resolution will even work on Android 8 (Oreo) or newer when targetSdkVersion is set to 26+. no init, no DNS, periodt. π
starting with Android 8 (API level 26), getting DNS server information got way more restrictive and it can only be accessed using the Connectivity Manager. so you gotta pass the connectivity manager over to c-ares via JNI. also, the ACCESS_NETWORK_STATE permission has to be present in the Android application. not optional, bestie. π
Android older than 8? they don't need to be initialized because they're less restrictive. BUT β and this is the plot twist β this is a run time limitation, not a compile time one. proper Android initialization should happen regardless of the targeted Android version. like, just do it. it's giving responsible. β¨
deinitialization happens through ares_library_cleanup(3), so it all comes full circle. π
the ares_library_init_jvm function is what lets the caller register the JVM with c-ares. it's meant to be called during JNI_OnLoad because that's the function where you're guaranteed to have the JVM. the JVM is required in order to use the Connectivity Manager registered using ares_library_init_android(3). and yes, this must be called before ares_library_init_android(3). order matters, queen. π
the ares_library_android_initialized function is your lil vibe check β use it to see whether c-ares has been initialized for use with Android. π
π₯ RETURN VALUES π₯
ARES_SUCCESS gets returned on success. otherwise you get an error code. simple as that. π
π THREAD SAFETY π
these init functions are not thread safe. you have to call them once the program has started, but that call must be done before the program starts any other thread. this is required to avoid potential race conditions in library initialization, and also because these might call functions from other libraries that are thread unsafe, and could conflict with any other thread that's already using these other libraries. so likeβ¦ call it first, call it alone, and then go live your life. π
no cap, do NOT be out here calling these from multiple threads. it's giving chaos and we don't do chaos in library init. π
π¦ JNI π¦
accessing the Connectivity Manager through Java, step by step:
register the ares_library_android_init.
static JNINativeMethod funcs[] = {
{ "initialize_native", "(Landroid/net/ConnectivityManager;)I",
(void *)&ares_library_init_android}
};
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
JNIEnv *env = NULL;
jclass cls = NULL;
jint res;
if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_6) != JNI_OK)
return -1;
cls = (*env)->FindClass(env, JNIT_CLASS);
if (cls == NULL)
return -1;
res = (*env)->RegisterNatives(env, cls, funcs, sizeof(funcs)/sizeof(funcs[0]));
if (res != 0)
return -1;
ares_library_init_jvm(vm);
return JNI_VERSION_1_6;
}
calling the registered function from Java:
public class MyObject {
static {
System.loadLibrary("cares");
}
private static native boolean initialize_native(ConnectivityManager
connectivity_manager);
public static boolean initialize(Context context) {
initialize_native((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
}
}
initializing the Connectivity Manager in JNI directly using an Android Context. it's assumed the JVM has already been registered through JNI_OnLoad.
void initialize(jobject android_context)
{
jclass obj_cls = jni_get_class(env, "android/content/Context");
jmethodID obj_mid = jni_get_method_id(env, obj_cls, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
jfieldID fid = (*env)->GetStaticFieldID(env, obj_cls, "CONNECTIVITY_SERVICE", "Ljava/lang/String;");
jstring str = (*env)->GetStaticObjectField(env, obj_cls, fid);
connectivity_manager = (*env)->CallObjectMethod(env, android_context, obj_mid, str);
if (connectivity_manager == NULL)
return;
ares_library_init_android(connectivity_manager);
}
π AVAILABILITY π
this function was first introduced in c-ares version 1.15.0. so if your c-ares is older than thatβ¦ yeah, she's not showing up to the party. π
π§ SEE ALSO π§
and that's the whole page, bestie!! π this was the Library Functions Manual entry for ARES_LIBRA...NIT_ANDROID(3), last touched 13 Sept 2017. slay, go initialize responsibly β¨ππ
π¦ where this page comes from
- alpine 3.22: c-ares-doc 1.34.9-r0 Β· MIT
- alpine 3.23: c-ares-doc 1.34.9-r0 Β· MIT
- alpine 3.24: c-ares-doc 1.34.9-r0 Β· MIT
- arch rolling: c-ares 1.34.9-1 Β· MIT
- centos-stream 10: c-ares-devel 1.34.9-1.el10 Β· MIT
- debian trixie: libc-ares-dev 1.34.5-1+deb13u1 Β· MIT
- fedora 43: c-ares-devel 1.34.5-2.fc43 Β· MIT
- fedora 43: c-ares-devel 1.34.5-2.fc43 Β· MIT
- fedora 43: c-ares-devel 1.34.8-1.fc43 Β· MIT
- fedora 43: c-ares-devel 1.34.8-1.fc43 Β· MIT
- fedora 44: c-ares-devel 1.34.6-3.fc44 Β· MIT
- fedora 44: c-ares-devel 1.34.6-3.fc44 Β· MIT
- fedora 44: c-ares-devel 1.34.8-1.fc44 Β· MIT
- fedora 44: c-ares-devel 1.34.8-1.fc44 Β· MIT
- nixos 26.05: c-ares 1.34.9
- nixpkgs unstable: c-ares 1.34.8
- opensuse-leap 16.0: c-ares-devel 1.34.8-160000.1.1 Β· MIT
- opensuse-leap 16.0: c-ares-devel 1.34.8-160000.1.1 Β· MIT
- opensuse-leap 16.0: c-ares-devel 1.34.8-160000.1.1 Β· MIT
- opensuse-leap 16.0: c-ares-devel 1.34.8-160000.1.1 Β· MIT
- ubuntu questing: libc-ares-dev 1.34.5-1
- ubuntu questing: libc-ares-dev 1.34.5-1ubuntu0.1
- ubuntu resolute: libc-ares-dev 1.34.6-1 Β· MIT
- ubuntu resolute: libc-ares-dev 1.34.6-1ubuntu0.1
- ubuntu resolute: libc-ares-dev 1.34.6-1ubuntu0.2 Β· MIT
- ubuntu stonking: libc-ares-dev 1.34.8-1 Β· MIT
- π the page's license: MIT
- Copyright (C) 2017 by John Schember
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms