Android - Create Symbolic Link In The App
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.createSymbolicL
Solution 1:
Here's a solution that worked for me, which returns true iff succeeded :
publicstaticbooleancreateSymLink(String symLinkFilePath, String originalFilePath) {
try {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
Os.symlink(originalFilePath, symLinkFilePath);
returntrue;
}
final Class<?> libcore = Class.forName("libcore.io.Libcore");
final java.lang.reflect.FieldfOs= libcore.getDeclaredField("os");
fOs.setAccessible(true);
finalObjectos= fOs.get(null);
final java.lang.reflect.Methodmethod= os.getClass().getMethod("symlink", String.class, String.class);
method.invoke(os, originalFilePath, symLinkFilePath);
returntrue;
} catch (Exception e) {
e.printStackTrace();
}
returnfalse;
}
Or in Kotlin:
companionobject {
@JvmStaticfuncreateSymLink(symLinkFilePath: String, originalFilePath: String): Boolean {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Os.symlink(originalFilePath, symLinkFilePath)
returntrue
}
val libcore = Class.forName("libcore.io.Libcore")
val fOs = libcore.getDeclaredField("os")
fOs.isAccessible = trueval os = fOs.get(null)
val method = os.javaClass.getMethod("symlink", String::class.java, String::class.java)
method.invoke(os, originalFilePath, symLinkFilePath)
returntrue
} catch (e: Exception) {
e.printStackTrace()
}
returnfalse
}
}
Post a Comment for "Android - Create Symbolic Link In The App"