在.so库中更改函数名称

时间:2019-02-20 08:52:36

标签: android android-ndk

我只有1个来自旧项目的.so文件。 在项目或模块中不创建相同包的情况下如何使用该文件?

1 个答案:

答案 0 :(得分:0)

实际上,您不需要更改.so文件中的函数名称。您可以使用dlopen在运行时动态加载.so库,而可以使用dlsym为您YOUR_FUNCTION_NAME()获取指针,然后通过指针调用YOUR_FUNCTION_NAME()。为此,您可以像这样创建“包装器”:

public class OldSoHelper {
    public static native void loadOldSo();
    public static native <TYPE_OF_RESULT> runFunFromOldSo(<PARAMETERS>);
    public static native void unloadOldSo();
}

以及当前项目的相应.c/.cpp文件中(例如默认为native-lib.cpp):

void *handle;
<TYPE_OF_OLD_FUNCTION> (*old_fun_wrapper)(<PARAMETERS_OF_OLD_FUNCTION>);

extern "C"
JNIEXPORT void JNICALL
Java_<YOUR_PACKAGE_NAME>_OldSoHelper_loadOldSo(JNIEnv *env, jclass type) {
     handle = dlopen("<YOUR_OLD_SO>.so", RTLD_NOW);
     old_fun_wrapper = (<TYPE_OF_OLD_FUNCTION> (*)(<PARAMETERS_OF_OLD_FUNCTION>))(dlsym(handle, "<OLD_FUNCTION_NAME_e.g._Java_com_abc_dee_Native_appInit>"));
}

extern "C"
JNIEXPORT jobject JNICALL
Java_<YOUR_PACKAGE_NAME>_OldSoHelper_runFunFromOldSo(JNIEnv *env, jclass type,
                                                      <PARAMETERS_FOR_OLD_FUNCTION>)
{   
    jclass ResultClass = env->FindClass("YOUR/PACKAGE/NAME/RESULT_CLASS");
    jobject result = ...
    jfieldID fieldId = env->GetFieldID(ResultClass, "<FIELD_NAME>", "<FILED_TYPE_LETTER>");

    <TYPE_OF_OLD_FUNCTION> res = old_fun_wrapper(<PARAMETERS_FOR_OLD_FUNCTION>);

    env->Set<TYPE>Field(result, fieldId , res.filed);

    return result;
}

extern "C"
JNIEXPORT void JNICALL
Java_<YOUR_PACKAGE_NAME>_OldSoHelper_unloadOldSo(JNIEnv *env, jclass type) {
     if (handle) {
         dlclose(handle);
     }
}

,您可以从Java代码中调用:

...
// when you need old .so e.g. in onCreate()
OldSoHelper.loadOldSo();
...

// when you no need to call function from old .so
<TYPE_OF_RESULT> result = OldSoHelper.runFunFromOldSo(<PARAMETERS>);

...
// when you no need old .so e.g. in onDestroy()
OldSoHelper.unloadOldSo();
...