enter image description here

android studio version 2023.1.1 generated binary executable file by following steps from this repo.

I'm facing an issue with executing the iperf3 binary file stored in the assets folder. Despite following the steps provided in the android-iperf3 repository to generate the binary executable, I encounter a persistent "permission denied" error when attempting to execute the iperf3 command within my application.

i have given all possible permissions and different ways to access the assets, but is always says

Error: Cannot run program "/data/user/0/ com.example.myapplication/files/iperf3.16": error=13, Permission denied.

I've thoroughly searched for solutions but haven't found one that addresses my specific problem. How to overcome this "permission denied" issue? Any insights, alternative methods, or specific steps to resolve this dilemma would be greatly appreciated.

IperfManager.kt

package com.example.myapplication

import android.content.Context
import android.content.res.AssetManager
import android.util.Log
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader


class IperfManager(private  val context: Context) {
    companion object {
        private const val IPERF_FILENAME = "iperf3.16"
        private const val TAG = "IperfManager"

    }
    @OptIn(DelicateCoroutinesApi::class)
    fun startIperfCommand(ipaddress: String): String{
        // Copy the iperf3 executable from assets to the app's data directory
        var result = ""
        val iperfFile = copyIperfFileToDataDir()

        // Check if the file copy was successful
        if (iperfFile != null && iperfFile.exists()) {
            // Execute iperf command using the iperf3 file
            iperfFile.setExecutable(true)

            Runtime.getRuntime().exec("chmod a+x"+iperfFile.absolutePath)

            GlobalScope.launch(Dispatchers.IO) {  }
            result =  executeIperfCommand(iperfFile.absolutePath, ipaddress)
        }
        else {
            Log.e(TAG, "Failed to copy iperf3 file.")
        }

        return result

    }

    private fun copyIperfFileToDataDir(): File? {
        val dataDir = context.filesDir
        val iperfFile = File(dataDir, IPERF_FILENAME)

        if (!iperfFile.exists()) {
            try {
                val assetManager: AssetManager = context.assets
                val inputStream: InputStream = assetManager.open(IPERF_FILENAME)
                val outputStream = FileOutputStream(iperfFile)

                inputStream.use { input ->
                    outputStream.use { output ->
                        input.copyTo(output)
                    }
                }

                Log.d("hello", "iperf3 file copied successfully.")

            } catch (e: IOException) {
                Log.e(TAG, "Error copying iperf3 file: ${e.message}")
                e.printStackTrace()
                return null
            }

        }

        return iperfFile
    }

    private fun executeIperfCommand(iperfPath: String,ipAddress: String): String{
        return try {
            val process = Runtime.getRuntime().exec("$iperfPath -c $ipAddress")
            val reader = BufferedReader(InputStreamReader(process.inputStream))

            val result = StringBuilder()
            var line: String
            while (reader.readLine().also { line = it } != null) {
                result.append(line + "\n")
            }
            reader.close()
            process.destroy()
            result.toString()
        } catch (e: IOException) {
            e.printStackTrace()
            "Error: " + e.message
        } catch (e: InterruptedException) {
            e.printStackTrace()
            "Error: " + e.message
        }
    }
}

MainActivity.kt

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.TextView
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    private lateinit var ipEditText: EditText
    private lateinit var iperfButton: Button
    private lateinit var resultTextView: TextView
    private lateinit var progress: ProgressBar
 @OptIn(DelicateCoroutinesApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        ipEditText = findViewById(R.id.ipEditText)
        iperfButton = findViewById(R.id.iperfButton)
        resultTextView = findViewById(R.id.resultTextView)
        progress = findViewById(R.id.progressBar)

        iperfButton.setOnClickListener {
            val iperfManager = IperfManager(this)
            GlobalScope.launch(Dispatchers.Main){
                resultTextView.text = ""
                progress.visibility = View.VISIBLE
                val ipaddress = ipEditText.text.toString()
                val result = iperfManager.startIperfCommand(ipaddress)
                progress.visibility = View.GONE
                resultTextView.text = result
            }


        }
    }
}
0

There are 0 best solutions below