在运行时Android Studio上编译cpp文件

时间:2019-01-30 04:55:32

标签: android

我正在使用自定义的语言制作一个编译应用程序。此应用程序会将自定义的语言代码自动更改为CPP代码。因此,我要做的就是在运行时编译CPP代码。 我无法在应用中编译CPP文件。

我已经使用dexmaker和image-playground进行了签出,但是我在Gradle-build上遇到了问题。他们是一些较旧的项目,我不确定如何使用它,所以我通过了那个项目。

因此,我尝试在android studio中使用命令行工具。我已经保存了CPP文件,可以使用它。以下包括我的文件I / O代码。这是我的代码。

代码:

public class Code_cpp extends AppCompatActivity {

private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.code_cpp);
    TextView textView = (TextView) findViewById(R.id.view);
    textView.setMovementMethod(new ScrollingMovementMethod());
    context = this;
}

public void savebutton(View v) {
    final EditText edit = (EditText) findViewById(R.id.edit_text);
    TextView textView = (TextView) findViewById(R.id.view);
    Log.v("EditText", edit.getText().toString());


    String messageString = edit.getText().toString();

    writeToFile(messageString,context);
    Toast.makeText(getApplicationContext(), "saved", Toast.LENGTH_LONG).show();

}

private void writeToFile(String data,Context context) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.cpp", Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
        Log.e("Success", "File write Success ");
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

public void loadbutton(View v){
    String msg = readFromFile(context);
    TextView textView = (TextView) findViewById(R.id.view);
    textView.setText(msg);
}
private String readFromFile(Context context) {

    String ret = "";

    try {
        InputStream inputStream = context.openFileInput("config.cpp");

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString).append("\n");
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }
    }
    catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());
    }

    return ret;
}
public void run(View v){
    try {

        Process process = Runtime.getRuntime().exec("g++ config.cpp;./a.out");

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        int read;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();

        // Waits for the command to finish.
        process.waitFor();

        //return output.toString();
        String result=output.toString();
        TextView textView = (TextView) findViewById(R.id.view);
        textView.setText(result);

    } catch (IOException e) {

        throw new RuntimeException(e);

    } catch (InterruptedException e) {

        throw new RuntimeException(e);
    }
}

}

单击运行按钮时出现错误

Caused by: java.io.IOException: Cannot run program "g++": error=13, Permission denied 

我不想植根我的手机,因为我不想仅在我的手机上使用此应用程序,而是在下载它时也将其与其他手机一起使用。我是否需要尝试另一种方式来编译它?为什么会出现权限被拒绝的错误?

0 个答案:

没有答案