-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how do you exec a native app in (presumably) the libs folder of the APK? #91
Comments
I am unfamiliar with how this works myself, but I do know I was once able to compile an app with a main() and everything, and execute it off of the external data partition. Not sure if that still works. |
I figured out how to access the libs folder
int GetLibDir(char *out)
{
JavaVM* java_vm = g_App->activity->vm;
JNIEnv* java_env = nullptr;
jint jni_return = java_vm->GetEnv((void**)&java_env, JNI_VERSION_1_6);
if (jni_return == JNI_ERR)
return -1;
jni_return = java_vm->AttachCurrentThread(&java_env, nullptr);
if (jni_return != JNI_OK)
return -2;
jclass contextClass = java_env->GetObjectClass(g_App->activity->clazz);
if (contextClass == nullptr)
return -3;
const jmethodID getApplicationContextMethod =
java_env->GetMethodID(contextClass, "getApplicationContext", "()Landroid/content/Context;");
const jobject contextObject =
java_env->CallObjectMethod(g_App->activity->clazz, getApplicationContextMethod);
const jmethodID getApplicationInfoMethod = java_env->GetMethodID(
contextClass, "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;");
const jobject applicationInfoObject =
java_env->CallObjectMethod(contextObject, getApplicationInfoMethod);
const jfieldID nativeLibraryDirField = java_env->GetFieldID(
java_env->GetObjectClass(applicationInfoObject), "nativeLibraryDir", "Ljava/lang/String;");
const jobject nativeLibraryDirObject =
java_env->GetObjectField(applicationInfoObject, nativeLibraryDirField);
const jmethodID getBytesMethod = java_env->GetMethodID(
java_env->GetObjectClass(nativeLibraryDirObject), "getBytes", "(Ljava/lang/String;)[B");
const auto bytesObject = static_cast<jbyteArray>(java_env->CallObjectMethod(
nativeLibraryDirObject, getBytesMethod, java_env->NewStringUTF("UTF-8")));
const size_t length = java_env->GetArrayLength(bytesObject);
const jbyte* const bytes = java_env->GetByteArrayElements(bytesObject, nullptr);
const std::string libDir(reinterpret_cast<const char*>(bytes), length);
strcpy(out, libDir.c_str());
return 0;
} |
I believe I misunderstood your goals... This feels very ... unusual ... What purpose would you want to do this for? |
That is useful for embedding native android binaries in the APK and running them. For example I just crated an app that when detects you took a picture it will upload it using rsync & ssh to a server https://github.com/aguaviva/Syncy (warning it is highly experimental but it works on my phone :) ) |
Wow, that's janky. I would never have thought to even try it! |
Why janky? It is just an experiment and, not that bad, the great thing is that I fully developed it on Linux (without any attempt at making it portable, I used threads, semaphores, fork, exec, inotify,...) and then it just works on android (just small issues like this one only). |
It's fair I just never thought about that as a reasonable solution. |
If you wanted to write this up, I'd totes accept a PR. Or if you made this another repo based on this one? |
Excited by your lib I started writing an app that 'rsync' files that have been modified so my phone data gets backed up to my server.
All works great, I compiled rsync and put it in the lib folder, but I cannot run it :) Do you have any thoughts on how this could be done? I am super close :)
Cheers
The text was updated successfully, but these errors were encountered: