Android JNINDK && JNI

android 动态加载多个so(包含so引用so)

2018-04-22  本文已影响8人  tpkeeper

0、目标so为a 被引用so为b a引用b

1、被引用的so b

2、生成待使用的目标so a

include_directories( libs/jni/include/ )
add_library( # Sets the name of the library.
             extest-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             IMPORTED )

set_target_properties( # Specifies the target library.
                       extest-lib

                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION

                       # Provides the path to the library you want to import.
                       D:/work/test/extest1/app/libs/jni/${ANDROID_ABI}/libextest-lib.so
                       )

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       extest-lib
                       ${log-lib} )

3、app中使用 so

//防止重复加载
private List<String> loadSoList = new ArrayList<>();

 public void loadSo(String name) {
        Log.e("TAG","loadSo:"+name);
        File file = new File("/sdcard/" + name + ".so");
        String path = getDir("libs", MODE_PRIVATE).getAbsolutePath() + "/" + name + ".so";
        Log.e("TAG", "getdirPath: " + getDir("libs", MODE_PRIVATE).getAbsolutePath() + "to path:" + path);
        File to = new File(path);
        if (!to.exists()) {
            if (file.exists()) {
                Log.e("TAG", "so exit");
                copy(file, to);
            }
        }

        if (!loadSoList.contains(name)) {
            System.load(path);
            loadSoList.add(name);
        }
    }

public static void copy(File from, File to) {
        try {
            FileInputStream fileInputStream = new FileInputStream(from);
            FileOutputStream fileOutputStream = new FileOutputStream(to);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }

            fileInputStream.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4、注意事项

ndk {
            abiFilters  'arm64-v8a'
        }
上一篇 下一篇

猜你喜欢

热点阅读