Stack Overflow archive
3 score

Symbolic Link Creation in Android Within an Application's Asset Directory

score
3
question views
5.7K
license
CC BY-SA 3.0

There is no public API to do this. You can however use some dirty reflection to create your symbolic link. I just tested the following code and it worked for me:

java
// static factory method to transfer a file from assets to package files directory
AssetUtils.transferAsset(this, "test.png");

// The file that was transferred
File file = new File(getFilesDir(), "test.png");
// The file that I want as my symlink
File symlink = new File(getFilesDir(), "symlink.png");

// do some dirty reflection to create the symbolic link
try {
    final Class<?> libcore = Class.forName("libcore.io.Libcore");
    final Field fOs = libcore.getDeclaredField("os");
    fOs.setAccessible(true);
    final Object os = fOs.get(null);
    final Method method = os.getClass().getMethod("symlink", String.class, String.class);
    method.invoke(os, file.getAbsolutePath(), symlink.getAbsolutePath());
} catch (Exception e) {
    // TODO handle the exception
}

A quick Google search showed this answer if you don't want to use reflection: http://androidwarzone.blogspot.com/2012/03/creating-symbolic-links-on-android-from.html

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.