Git Product home page Git Product logo

Comments (7)

xiaoyvyv avatar xiaoyvyv commented on July 19, 2024

hello bro me using in code

import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.Manifest;
import android.os.Environment;
import android.os.Handler;
import android.widget.Button;
import android.widget.Toast;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import androidx.core.content.ContextCompat;
import androidx.core.app.ActivityCompat;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.xiaoyv.ccompile.compiler.listener.CompileCallback;
import com.xiaoyv.ccompile.compiler.listener.ExecuteListener;
import java.io.File;
import java.util.List;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
	private Button runGcc, runCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		runCode = findViewById(R.id.runCode);
		runGcc = findViewById(R.id.runGcc);

		setFiles();
		runCode.setOnClickListener(v -> {
			CompileCpp("/storage/emulated/0/apk/Main.cpp");
			Handler handler = new Handler();
			handler.postDelayed(() -> {
				RunCodes("/storage/emulated/0/apk/");
			}, 3000);

		});
		runGcc.setOnClickListener(c -> {
			installGcc();
		});
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		if (requestCode == 1000) {
			setFiles();
		}
	}

	private void getFiles() {
		if (ContextCompat.checkSelfPermission(this,
				Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED
				|| ContextCompat.checkSelfPermission(this,
						Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
			ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE,
					Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1000);
		} else {
			setFiles();
		}
	}

	private void setFiles() {
		new MaterialAlertDialogBuilder(this).setTitle("مجوز فایب").setMessage("مجوز فایب رو بده جاکش")
				.setPositiveButton("ok", (ccc, dd) -> {
					getFiles();
				}).show();
	}

	private void installGcc() {
		ProgressDialog dialogs = new ProgressDialog(MainActivity.this, ProgressDialog.THEME_DEVICE_DEFAULT_DARK);
		dialogs.setTitle("Loading...");
		dialogs.show();
		File dir = new File(Environment.getExternalStorageDirectory() + "/c_compiler/gcc.zip");

		CCppEngine.installGcc(this, new File(Environment.getExternalStorageDirectory() + "/c_compiler/gcc.zip"),
				new CCppEngine.OnInstallListener() {
					@Override
					public void onSuccess() {
						Log.e("aaaa", "安装成功");
						Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT).show();
						dialogs.dismiss();
					}

					@Override
					public void onError(String error) {
						Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
						dialogs.dismiss();
					}
				});
	}

	private void CompileCpp(String dir) {
		File cFile = new File(dir);
		List<File> files = new ArrayList<>();
		files.add(cFile);

		Log.e("==========", "======================================================================");
		CCppEngine.getCppCompiler().compile(files, new CompileCallback() {
			@Override
			public void onSuccess(String outPath) {
				Log.e("==========", "编译完成:" + outPath);
				Toast.makeText(getApplicationContext(), outPath, 2).show();

			}

			@Override
			public void onError(String error) {
				Log.e("==========", "编译失败:" + error);
				Toast.makeText(getApplicationContext(), error, 2).show();

			}
		});
	}

	private void RunCodes(String dir) {
		File file = new File(dir);

		CCppEngine.getCExecutor().exec(file, new ExecuteListener() {
			@Override
			public void onExecuteStart(Process process) {
				Log.e("开始>>>>>>>", " process start");
			}

			@Override
			public void printStderr(Throwable error) {
				Log.e("错误>>>>>>>", error.toString());
				error.printStackTrace();
				Toast.makeText(getApplicationContext(),error.toString(),2).show();
			}

			@Override
			public void printStdout(CharSequence out) {
				Log.e("输出>>>>>>>", out.toString());
				runCode.setText(out);
			}

			@Override
			public void onExecuteFinish(int waitFor, int exitValue) {
				Log.e("完成>>>>>>>", "====================" + waitFor + "================" + exitValue);
			}
		});
	}

}

This code does not compile the code and does not display anything. Gcc is also installed

Android10+ not support exec binary file in data/data/xxxx. You can run the gcc binary file wrap with proot

from ccompileengine.

moderGamer avatar moderGamer commented on July 19, 2024

hi can you send Code for me?

from ccompileengine.

moderGamer avatar moderGamer commented on July 19, 2024

I didn't understand, can you help me more or send me a sample code?

@xiaoyvyv

from ccompileengine.

xiaoyvyv avatar xiaoyvyv commented on July 19, 2024

I didn't understand, can you help me more or send me a sample code?

@xiaoyvyv

There is also a simple method, you can set the targetSdkVersion to less than 30, and then run the demo to test it

from ccompileengine.

moderGamer avatar moderGamer commented on July 19, 2024

I did it, but I got an empty toast. You should edit your code so that android/data is not needed

from ccompileengine.

moderGamer avatar moderGamer commented on July 19, 2024

Screenshot_20230516-182129_CppCo

Majdad, Hello, I solved the compilation problem, but when I click on the third button, which is run, it doesn't do anything. I will send you the code I wrote.

package com.xiaoyv.ccompile;

import android.content.Context;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.xiaoyv.ccompile.CCppEngine;
import com.xiaoyv.ccompile.compiler.listener.ExecuteListener;
import android.util.Log;
import java.io.File;

public class Result {
	public static void exe(Context context) {
		StringBuilder builder = new StringBuilder();
		File file = new File("/data/user/0/com.xiaoyv.ccompileengine/files/c_compiler/temp");
		CCppEngine.getCExecutor().exec(file, new ExecuteListener() {
			@Override
			public void onExecuteStart(Process process) {
				Log.e("开始>>>>>>>", " process");
				builder.append("S" + process.toString() + '\n');
			}

			@Override
			public void printStderr(Throwable error) {
				Log.e("错误>>>>>>>", error.toString());
				error.printStackTrace();
			}

			@Override
			public void printStdout(CharSequence out) {
				Log.e("输出>>>>>>>", out.toString());
				builder.append("S" + out +'\n');
                DialogShow(context,builder);
			}

			@Override
			public void onExecuteFinish(int waitFor, int exitValue) {
				Log.e("完成>>>>>>>", "====================" + waitFor + "================" + exitValue);
				builder.append("[Program End]");
				
			}
			
		});
	}
	private static void DialogShow(Context context,StringBuilder b){
		new MaterialAlertDialogBuilder(context).setTitle("Task Result").setMessage(b).setPositiveButton("ok",null).show();
	}

}

from ccompileengine.

moderGamer avatar moderGamer commented on July 19, 2024

@xiaoyvyv Take it, don't bother yourself, don't bother me, don't bother others, rewrite your code so that it doesn't read anything in Android/data, it can be executed in a public path. I don't think it will take you more than 2 hours

from ccompileengine.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.