'android: unpack and run binary file from java - permission denied

I need to execute a raw binary file from java code of an android application. The binary file is stored in assets, so I unpack it first and then execute.

When I used targetSdkVersion 28 for my project, everything worked OK, but with targetSdkVersion 30 I get error = 13, permission denied when starting the process.

This is my code:

    public String unzipExeFromAsset(String filename, Context context) throws IOException {
        File f = new File(context.getCacheDir() + "/" + filename);
        if (!f.exists()) {
    
            Log.d("unzipExeFromAsset", "not exist try create");
    
            InputStream is = context.getAssets().open(filename);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
    
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(buffer);
            fos.close();
        }
        f.setExecutable(true);
        return f.getPath();
    }

String path = unzipExeFromAsset("myfile", this);
ProcessBuilder processBuilder = new ProcessBuilder(path);
process = processBuilder.start(); // permission denied error

How to fix "permission denied" when starting the process?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source