I am trying to use Libcore to set environmental variable so I used the following answer:
How do I set environment variables from Java?
on Android the interface is exposed via Libcore.os as a kind of hidden API.
Libcore.os.setenv("VAR", "value", bOverwrite); Libcore.os.getenv("VAR"));
The Libcore class as well as the interface OS is public. Just the class declaration is missing and need to be shown to the linker. No need to add the classes to the application, but it also does not hurt if it is included.
package libcore.io;
public final class Libcore {
private Libcore() { }
public static Os os;
}
package libcore.io;
public interface Os {
public String getenv(String name);
public void setenv(String name, String value, boolean overwrite) throws Exception;
}
However when I do this it does not seem to work as when I do getenv in my C code I get no return value.
When I do this
char* p;
setenv("VAR", "test variable", 1);
p = getenv("VAR");
LOG(ERROR) << "Test" << p;
It works correctly.
What I did was create two dummy files for Libcore.java and Os.java and compiled them with my main code:
Here is my Java code:
try{
Libcore.os.setenv("VAR", "value", true);
}catch(Exception e){
Log.d("Test set",e.toString());
}
My C code is below:
char* p;
p = getenv("VAR");
LOG(ERROR) << "Test" << p;
I could use JNI, but I am trying to figure why libcore is not working. Is there some special command I need to use with compiler command for it to use the system Libcore?