-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
157 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// Created by baiiu on 2020/12/24. | ||
// | ||
#include <dlfcn.h> | ||
#include <string.h> | ||
#include <cstdlib> | ||
#include "jni.h" | ||
#include "log.h" | ||
|
||
extern "C" | ||
JNIEXPORT void JNICALL | ||
Java_com_baiiu_jnitest_dlopen_DLOpenFragment_testdlopen(JNIEnv *env, jobject thiz, jstring jstr) { | ||
const char *soPath = env->GetStringUTFChars(jstr, JNI_FALSE); | ||
|
||
LOGD("native--> soPath: %s", soPath); | ||
void *handle = dlopen(soPath, RTLD_NOW); | ||
|
||
char *error_msg = nullptr; | ||
if (handle == nullptr) { | ||
error_msg = strdup(dlerror()); | ||
LOGD("%s", error_msg); | ||
} | ||
|
||
LOGD("handle is %p", handle); | ||
|
||
// void *sym = dlsym(handle, "fibonacci"); | ||
void *sym = dlsym(handle, "_Z9fibonaccii"); | ||
if (sym == nullptr) { | ||
LOGD("can not find the function"); | ||
return; | ||
} | ||
|
||
using FIBONACCI = int (*)(int); | ||
FIBONACCI fibonacci = reinterpret_cast<FIBONACCI>(sym); | ||
int result = (*fibonacci)(5); | ||
|
||
LOGD("fibonacci result is: %d", result); | ||
|
||
dlclose(handle); | ||
|
||
env->ReleaseStringUTFChars(jstr, soPath); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
jni/jnitest/src/main/java/com/baiiu/jnitest/dlopen/DLOpenFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.baiiu.jnitest.dlopen; | ||
|
||
import android.text.TextUtils; | ||
|
||
import com.baiiu.jnitest.base.BaseFragment; | ||
import com.baiiu.jnitest.testSocket.TestSocketFragment; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
|
||
import dalvik.system.BaseDexClassLoader; | ||
|
||
/** | ||
* author: baiiu | ||
* time: 2020/12/24 | ||
* description: | ||
*/ | ||
public class DLOpenFragment extends BaseFragment { | ||
|
||
static { | ||
System.loadLibrary("dlopen-lib"); | ||
} | ||
|
||
@Override | ||
protected void initOnCreateView() { | ||
try { | ||
Method findLibrary = BaseDexClassLoader.class.getDeclaredMethod("findLibrary", String.class); | ||
findLibrary.setAccessible(true); | ||
|
||
BaseDexClassLoader dexPathClassLoader = (BaseDexClassLoader) TestSocketFragment.class.getClassLoader(); | ||
String targetSoAbsolutePath = dexPathClassLoader.findLibrary("socket-lib"); | ||
android.util.Log.e("mLogU", "ClassLoader#findLibrary: " + targetSoAbsolutePath); | ||
|
||
} catch (Exception e) { | ||
android.util.Log.e("mLogU", e.toString()); | ||
} | ||
|
||
|
||
try { | ||
Field pathListF = BaseDexClassLoader.class.getDeclaredField("pathList"); | ||
pathListF.setAccessible(true); | ||
ClassLoader dexPathClassLoader = TestSocketFragment.class.getClassLoader(); | ||
Object pathList = pathListF.get(dexPathClassLoader); | ||
|
||
Field nativeLibraryPathElementsF = Class.forName("dalvik.system.DexPathList").getDeclaredField("nativeLibraryPathElements"); | ||
nativeLibraryPathElementsF.setAccessible(true); | ||
Object nativeLibraryPathElements = nativeLibraryPathElementsF.get(pathList); | ||
android.util.Log.e("mLogU", nativeLibraryPathElements.toString()); | ||
|
||
// find the nativeLibraryPathElements | ||
Object[] arr = (Object[]) nativeLibraryPathElements; | ||
for (Object o : arr) { | ||
android.util.Log.e("mLogU", o.toString()); | ||
} | ||
|
||
|
||
Field pathF = Class.forName("dalvik.system.DexPathList$NativeLibraryElement").getDeclaredField("path"); | ||
pathF.setAccessible(true); | ||
|
||
String targetSoAbsolutePath = null; | ||
for (Object o : arr) { | ||
File path = (File) pathF.get(o); | ||
targetSoAbsolutePath = findNativeLibrary(path, "libsocket-lib.so"); | ||
if (!TextUtils.isEmpty(targetSoAbsolutePath)) { | ||
break; | ||
} | ||
} | ||
|
||
testdlopen(targetSoAbsolutePath); | ||
|
||
} catch (Exception e) { | ||
android.util.Log.e("mLogU", e.toString()); | ||
} | ||
|
||
} | ||
|
||
|
||
private native void testdlopen(String targetSoAbsolutePath); | ||
|
||
|
||
public String findNativeLibrary(File path, String name) { | ||
File file = new File(path, name); | ||
android.util.Log.e("mLogU", file.getAbsolutePath()); | ||
|
||
if (file.exists()) { | ||
return file.getAbsolutePath(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
} |