I want create programmatically symbolic link in my app. Is it possible in Android (4.4+)?
In Java we can use:
Path newLink = ...;
Path target = ...;
try {
Files.createSymbolicLink(newLink, target);
} catch (IOException x) {
System.err.println(x);
} catch (UnsupportedOperationException x) {
// Some file systems do not support symbolic links.
System.err.println(x);
}
from java.nio.file
but what I should use in Android?
https://docs.oracle.com/javase/tutorial/essential/io/links.html
EDIT:
I tested using reflection/native code/OS.symlink() method
and nothing work. I always get Operation not permitted (EPERM). I think you have to have root permission for create the symlink.
The problem can be with that /mnt/sdcard
is a FUSE shim that wraps /data/media/xxx
. So I started using /data/media/xxx
but I always get Permission denied
I think it's a problem with root permissions.
Here's a solution that worked for me, which returns true iff succeeded :
public static boolean createSymLink(String symLinkFilePath, String originalFilePath) {
try {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
Os.symlink(originalFilePath, symLinkFilePath);
return true;
}
final Class<?> libcore = Class.forName("libcore.io.Libcore");
final java.lang.reflect.Field fOs = libcore.getDeclaredField("os");
fOs.setAccessible(true);
final Object os = fOs.get(null);
final java.lang.reflect.Method method = os.getClass().getMethod("symlink", String.class, String.class);
method.invoke(os, originalFilePath, symLinkFilePath);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Or in Kotlin:
companion object {
@JvmStatic
fun createSymLink(symLinkFilePath: String, originalFilePath: String): Boolean {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Os.symlink(originalFilePath, symLinkFilePath)
return true
}
val libcore = Class.forName("libcore.io.Libcore")
val fOs = libcore.getDeclaredField("os")
fOs.isAccessible = true
val os = fOs.get(null)
val method = os.javaClass.getMethod("symlink", String::class.java, String::class.java)
method.invoke(os, originalFilePath, symLinkFilePath)
return true
} catch (e: Exception) {
e.printStackTrace()
}
return false
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With