Salut,

J'ai besoin de pouvoir exécuter des scripts shell en tant que root dans une application Android écrite en java. J'aimerai pouvoir exécuter des commandes android comme am ou pm et qui nécessite la la présence de la variable d'environnement LD_LIBRARY_PATH=/system/lib. Malheureusement, la commande export ne fonctionne pas et j'en ai réellement besoin :/

Voici le code Java que j'ai écrit :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
private String readScript(String scriptPath) {
 
    String line = null;
    StringBuffer content = new StringBuffer();
 
    try {
 
        // Read the script line by line
        BufferedReader reader = new BufferedReader(new FileReader(scriptPath));
        while ((line = reader.readLine()) != null) {
            content.append(line).append('\n');
        }
        content.append("exit").append('\n');
        reader.close();
 
    } catch (IOException e) {
 
        Log.d(UpdateService.LOG_NAME, "Impossible to read the script : " + e.getMessage());
 
    }
 
    return content.toString();
 
}
 
public StringBuffer runScript(String scriptPath) {
 
    String line = null;
    StringBuffer output = new StringBuffer();
 
    try {
 
        // Start the shell process
        ProcessBuilder pb = new ProcessBuilder("su");
        pb.redirectErrorStream(true);
        pb.directory(new File(context.getApplicationInfo().dataDir));
        pb.environment().put("LD_LIBRARY_PATH", "/system/lib");
        Process process = pb.start();
 
        // Execute the script
        OutputStreamWriter os = new OutputStreamWriter(process.getOutputStream());
        os.write(readScript(scriptPath));
        os.flush();
        os.close();
 
        // Get the output of the commands
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((line = in.readLine()) != null) {
            output.append(line).append('\n');
        }
        in.close();
 
    } catch (Exception e) {
 
        Log.d(UpdateService.LOG_NAME, "Error while executing " + scriptPath + " : " + e.getMessage());
 
    }
 
    return output;
 
}
Je l'appelle simplement comme ceci :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
Shell = new Shell();
StringBuffer output = shell.runScript("script.sh");
Et voici le script que je tente d'exécuter :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
export LD_LIBRARY_PATH=/system/lib
printenv
am start -W -n com.android.settings/.Settings
À la sortie j'obtiens :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
_=/system/bin/printenv
ANDROID_BOOTLOGO=1
ANDROID_PROPERTY_WORKSPACE=9,32768
LOOP_MOUNTPOINT=/mnt/obb
EXTERNAL_STORAGE=/mnt/sdcard
ANDROID_DATA=/data
RANDOM=21985
ANDROID_SOCKET_zygote=10
ASEC_MOUNTPOINT=/mnt/asec
BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/filterfw.jar
ANDROID_ROOT=/system
ANDROID_ASSETS=/system/app
PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin
Segmentation fault
Comme on peut le voir, la variable d'environnement initialisée avec la méthode environment() de ProcessBuilder n'est pas settée et la commande export présente dans le scritp ne fonctionne pas non plus et donc la commande am échoue.

Une idée de la raisons pourquoi cette commande am ne fonctionne pas ?

Merci beaucoup