Avez-vous déjà exécuté autre chose que Java sur Android? Si vous voulez juste l'exécuter, vous pouvez souvent le faire en utilisant termux ou une application pour chaque langage, mais il est difficile de le compresser dans apk et de le distribuer (j'étais frustré par python) Cependant, il y en a certains tels que Node.js introduit cette fois qui ont un moyen de travailler avec Android grâce à la sagesse de nos prédécesseurs. Alors terminons l'introduction jusqu'à présent et voyons comment le faire réellement
node in android Environnement pour exécuter Java avec Android
Mettons ce dont nous avons besoin dans les actifs Je n'ai pas besoin de npm pour le moment Placez le fichier téléchargé appelé node dans assets / resource / bin Ajoutez l'autorisation WriteExternalStorage ou l'autorisation readExternalStorage selon le cas. import En premier
import java.io.IOException;
import java.util.Map;
import fi.iki.elonen.*;
import android.content.res.AssetManager;
import android.os.Bundle;
import java.io.*;
import java.util.*;
Veuillez importer
Seuls les fichiers de localStorage peuvent être exécutés, alors déplacez-les des actifs avec la fonction copyFiles.
private boolean isDirectory(final String path) throws IOException {
AssetManager assetManager = getActivity().getResources().getAssets();
boolean isDirectory = false;
try {
if (assetManager.list(path).length > 0) {
isDirectory = true;
} else {
assetManager.open(path);
}
}
catch (FileNotFoundException fnfe) {
isDirectory = true;
}
return isDirectory;
}
private void copyFiles(final String parentPath, final String filename, final File toDir) throws IOException {
AssetManager assetManager = getActivity().getResources().getAssets();
String assetpath = (parentPath != null ? parentPath + File.separator + filename : filename);
if (isDirectory(assetpath)) {
if (!toDir.exists()) {
toDir.mkdirs();
}
for (String child : assetManager.list(assetpath)) {
copyFiles(assetpath, child, new File(toDir, child));
}
} else {
copyData(assetManager.open(assetpath), new FileOutputStream(new File(toDir.getParentFile(), filename)));
}
}
private void copyData(InputStream input, FileOutputStream output) throws IOException {
int DEFAULT_BUFFER_SIZE = 1024 * 4;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
output.close();
input.close();
}
Veuillez changer la partie de getContext () et getActivity () en une partie appropriée.
Appelez node.js avec la fonction adbCommandExe La fonction sketchPath renvoie l'emplacement de localstorage La fonction addEnv ajoute des variables d'environnement
String sketchPath(String path){
return getContext().getFilesDir().getAbsolutePath()+"/"+path;
}
void addEnv(ProcessBuilder pb,String e,String value){
Map<String, String> env = pb.environment();
env.put(e,value);
}
private String adbCommandExe(String command,File dir) {
StringBuilder suilder = new StringBuilder();
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
if(dir!=null)processBuilder.directory(dir);
addEnv(processBuilder,"LD_LIBRARY_PATH",sketchPath("lib"));
addEnv(processBuilder,"PATH",sketchPath("bin"));
addEnv(processBuilder,"LANG","en_US.UTF-8");
InputStream iStream = null;
InputStreamReader isReader = null;
try {
Process proc = processBuilder.start();
iStream = proc.getInputStream();
isReader = new InputStreamReader(iStream);
BufferedReader bufferedReader = new BufferedReader(isReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
suilder.append(line);
suilder.append("\n");
println(line);
}
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
while ((line = br.readLine()) != null) {
suilder.append(line);
suilder.append("\n");
println(line);
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if (iStream != null) {
iStream.close();
}
if (isReader != null) {
isReader.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
return suilder.toString();
}
Appelez en fait la fonction
void run(){
try {
copyFiles(null, "resource", new File(sketchPath("")));
}
catch (IOException ioe) {
println("copy error");
}
for(File file:new File(sketchPath("bin")).listFiles()){
if (file.setExecutable(true, true))println("a");
}
for(File file:new File(sketchPath("lib")).listFiles()){
if (file.setExecutable(true, true))println("a");
}
File dir = new File("/storage/emulated/0/webIDE/temp");
adbCommandExe("node -v",dir);
}
Essayez d'appeler la fonction d'exécution lorsque vous souhaitez exécuter node.js
void run(){
try {
copyFiles(null, "resource", new File(sketchPath("")));
}
catch (IOException ioe) {
println("copy error");
}
for(File file:new File(sketchPath("bin")).listFiles()){
if (file.setExecutable(true, true))println("a");
}
for(File file:new File(sketchPath("lib")).listFiles()){
if (file.setExecutable(true, true))println("a");
}
File dir = new File("/storage/emulated/0/webIDE/temp");
adbCommandExe("node -v",dir);
}
String sketchPath(String path){
return getContext().getFilesDir().getAbsolutePath()+"/"+path;
}
void addEnv(ProcessBuilder pb,String e,String value){
Map<String, String> env = pb.environment();
env.put(e,value);
}
private String adbCommandExe(String command,File dir) {
StringBuilder suilder = new StringBuilder();
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
if(dir!=null)processBuilder.directory(dir);
addEnv(processBuilder,"LD_LIBRARY_PATH",sketchPath("lib"));
addEnv(processBuilder,"PATH",sketchPath("bin"));
addEnv(processBuilder,"LANG","en_US.UTF-8");
InputStream iStream = null;
InputStreamReader isReader = null;
try {
Process proc = processBuilder.start();
iStream = proc.getInputStream();
isReader = new InputStreamReader(iStream);
BufferedReader bufferedReader = new BufferedReader(isReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
suilder.append(line);
suilder.append("\n");
println(line);
}
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
while ((line = br.readLine()) != null) {
suilder.append(line);
suilder.append("\n");
println(line);
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if (iStream != null) {
iStream.close();
}
if (isReader != null) {
isReader.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
return suilder.toString();
}
private boolean isDirectory(final String path) throws IOException {
AssetManager assetManager = getActivity().getResources().getAssets();
boolean isDirectory = false;
try {
if (assetManager.list(path).length > 0) {
isDirectory = true;
} else {
assetManager.open(path);
}
}
catch (FileNotFoundException fnfe) {
isDirectory = true;
}
return isDirectory;
}
private void copyFiles(final String parentPath, final String filename, final File toDir) throws IOException {
AssetManager assetManager = getActivity().getResources().getAssets();
String assetpath = (parentPath != null ? parentPath + File.separator + filename : filename);
if (isDirectory(assetpath)) {
if (!toDir.exists()) {
toDir.mkdirs();
}
for (String child : assetManager.list(assetpath)) {
copyFiles(assetpath, child, new File(toDir, child));
}
} else {
copyData(assetManager.open(assetpath), new FileOutputStream(new File(toDir.getParentFile(), filename)));
}
}
private void copyData(InputStream input, FileOutputStream output) throws IOException {
int DEFAULT_BUFFER_SIZE = 1024 * 4;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
output.close();
input.close();
}
Ceci conclut l'explication de l'exécution de node.js depuis android java (traitement). L'AS-tu fait?
Recommended Posts