Where is the runtime directory for SO Android libs?

时光毁灭记忆、已成空白 提交于 2019-12-11 17:58:27

问题


I have an Android app that uses an external .so library to work (OpenALPR).

This .so library also needs an external conf file to work properly. When I load my library and initialize it, I need to specify the path of the conf file to the library, in a native function.

private native void initialize(String country, String configFile, String runtimeDir);

Here is the structure of my project:

Which path am I supposed to give ? I can't find out where to put my file so that my libraries can see them


回答1:


The trick was to move manually the content of Assets to the actual /data/data/com.example.app/ folder, which is where the libs are stored.

Here's a snippet that achieves that (from the official android repo)

 /**
     * Copies the assets folder.
     *
     * @param assetManager The assets manager.
     * @param fromAssetPath The from assets path.
     * @param toPath The to assets path.
     *
     * @return A boolean indicating if the process went as expected.
     */
    public static boolean copyAssetFolder(AssetManager assetManager, String fromAssetPath, String toPath) {
        try {
            String[] files = assetManager.list(fromAssetPath);

            new File(toPath).mkdirs();

            boolean res = true;

            for (String file : files)

                if (file.contains(".")) {
                    res &= copyAsset(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
                } else {
                    res &= copyAssetFolder(assetManager, fromAssetPath + "/" + file, toPath + "/" + file);
                }

            return res;
        } catch (Exception e) {
            e.printStackTrace();

            return false;
        }
    }

    /**
     * Copies an asset to the application folder.
     *
     * @param assetManager The asset manager.
     * @param fromAssetPath The from assets path.
     * @param toPath The to assests path.
     *
     * @return A boolean indicating if the process went as expected.
     */
    private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
        InputStream in = null;
        OutputStream out = null;

        try {
            in = assetManager.open(fromAssetPath);

            new File(toPath).createNewFile();

            out = new FileOutputStream(toPath);

            copyFile(in, out);
            in.close();

            in = null;

            out.flush();
            out.close();

            out = null;

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * Copies a file.
     *
     * @param in The input stream.
     * @param out The output stream.
     *
     * @throws IOException
     */
    private static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];

        int read;

        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }


来源:https://stackoverflow.com/questions/57971178/where-is-the-runtime-directory-for-so-android-libs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!