Have you ever run anything other than java on android? If you just want to run it, you can often do it by using termux or an application for each language, but it is difficult to pack it in an apk and distribute it (I was frustrated about python) However, there are some such as Node.js introduced this time that have some way to work with android due to the wisdom of our predecessors. So let's finish the introduction so far and see how to actually do it
node in android Environment to run java on android
Let's put what we need in assets I don't need npm for the time being Put the downloaded file called node in assets / resource / bin Add WriteExternalStorage permission or readExternalStorage permission as appropriate. import At first
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.*;
Please import
Since only files in localStorage can be executed, move them from assets with the copyFiles function.
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();
}
Please change the parts of getContext () and getActivity () to appropriate ones.
Call node.js with the adbCommandExe function sketchPath function returns the location of localstorage The addEnv function adds an environment variable
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();
}
Actually call the function
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);
}
Try calling the run function when you want to run 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();
}
That's all for the explanation of running node.js from android java (processing). Did you do it?
Recommended Posts