Search code examples
android-studiocrashdifferentiationsymja

App crashes when using Symja library for symbolic differentiation of a user input function. (Using Android Studio)


I have used Symja library for symbolic differentiation in Java. When I use it as a seperate java file, it executes normally and prints the output in the console. However, when I integrate it to my app in Android Studio, my app crashes.

Here is the code:

import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.physicsmate.R;
import org.matheclipse.core.eval.ExprEvaluator;
import org.matheclipse.core.interfaces.IExpr;

public class DerivativeCalcOffline extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2et_3tv_2btn_1graph_dynamic_calc);

        TextView tv1 = findViewById(R.id.textView59);
        TextView tv2 = findViewById(R.id.textView60);
        TextView tvRes = findViewById(R.id.textView61);
        EditText et1 = findViewById(R.id.editTextNumber4);
        EditText et2 = findViewById(R.id.editTextNumber5);
        Button btnCalc = findViewById(R.id.button44);
        Button btnGraph = findViewById(R.id.btnGraph_2et_3tv_2btn_1graph_dynamic_calc);
        Button btnCopy = findViewById(R.id.button45);
        ImageView img = findViewById(R.id.ImageView_2et_3tv_2btn_1graph_dynamic_calc);

        ScrollView sv = findViewById(R.id.SV_2et_3tv_2btn_1graph_dynamic_calc);

        btnCopy.setVisibility(View.GONE);
        tvRes.setVisibility(View.GONE);
        btnGraph.setVisibility(View.GONE);
        img.setVisibility(View.GONE);

        tv1.setText("Enter the function to differentiate");
        tv2.setText("Enter the variable which is used to differentiate");
        btnCalc.setText("Differentiate");
        et1.setHint("sin(x)");
        et2.setHint("x");

        btnCalc.setOnClickListener(v -> new Thread(() -> {
            
            //_______________________________________________________________
            //This I tested seperately as a Java file and it gave me correct results
            
            String expression = tv1.getText().toString(); //Modified for android studio

            try {
                ExprEvaluator util = new ExprEvaluator();
                IExpr result = util.eval("D(" + expression + ", x)"); //Later I will replace "x" with the differentiation variable entered by the user in tv2
                String derivativeExpression = result.toString();

                tvRes.setText("Derivative: " + derivativeExpression);
            } catch (Exception ignored) {
                
            }
            
            //_______________________________________________________________
            
            sv.post(() -> sv.smoothScrollTo(0, btnCopy.getBottom())); //To smooth scroll to the result
        }).start());
    }
}

I have first tried running the code in the main thread. As that didnt work, I also tried using it in a seperate thread (as in the above code). Also there are no errors during project build. Is there any way to fix this issue? (If I have done any silly mistakes, please forgive me, as I am still a learner)


Solution

  • Please check the example from https://github.com/axkr/symja-example

    There is a simple hack to make Apache Log4J works on Android.