title: Running binary files
body: <p>I was able to download a binary file from internet and saved it at my Android device programmatically, the problem is selecting the proper location to be able to execute it, I picked both <code>app internal</code> and <code>data directory</code> but both gave me:</p>
  <blockquote>
  <p>Cannot run program &quot;/data/user/0/com.example.dl/app_mounted/server&quot;:
  error=13, Permission denied</p>
  </blockquote>
  <p>My code is:
  <code>MainActivity.kt</code></p>
  <pre class="lang-kotlin prettyprint-override"><code>package com.example.dl
  
  import android.content.Context
  import android.content.ContextWrapper
  import android.net.Uri
  import android.os.Bundle
  import android.os.Environment
  import android.util.Log
  import android.view.View
  import androidx.appcompat.app.AppCompatActivity
  import androidx.lifecycle.lifecycleScope
  import com.example.dl.databinding.ActivityMainBinding
  import kotlinx.coroutines.*
  import java.io.*
  import java.lang.RuntimeException
  import java.net.URL
  import java.util.*
  
  
  class MainActivity : AppCompatActivity() {
      private lateinit var binding: ActivityMainBinding
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          binding = ActivityMainBinding.inflate(layoutInflater)
          val view = binding.root
          setContentView(view)
          val context = this
         val urlFile:URL = URL(&quot;https://drive.google.com/uc?export=download&amp;id=&quot; +
                  &quot;1kRtYw3_Yd7he0HjbgNlAAl9we9tQEGvm&quot;)
  
          binding.tvDownload.text = urlFile.toString()
          val tag = &quot;Main Activity&quot;
          val targetFileName = &quot;server&quot;
  
          val wrapper = ContextWrapper(context)
          /* ******************************** */
          /** What Directory Should I Select **/
          var directory = wrapper.getDir(&quot;${Environment.getExternalStorageState()}&quot;, Context.MODE_PRIVATE)
          /* ******************************** */
          var file = File(directory, targetFileName)
  
          binding.button.setOnClickListener { button -&gt;
              lifecycleScope.launch {
                  if (file.exists()) {
                      Log.i(tag, &quot;$file does exist.&quot;)
                      // Runtime.getRuntime().exec(&quot;/system/bin/rm /$file&quot;)
  
                      Log.i(tag, &quot;Setting file executable&quot;)
                      file?.setExecutable(true)
                      Log.i(tag, &quot;Running executable file&quot;)
                      Runtime.getRuntime().exec(file.toString())
  
                      //   Runtime.getRuntime().exec(&quot;/bin/sh -c $file&quot;)
  
                      binding.tvSaved.text = file.toString()
  
                      button.isEnabled = true // enable button
                      binding.progressBar.visibility = View.INVISIBLE
                  } else {
                      Log.i(tag, &quot;$file does not exist.&quot;)
                      binding.tvSaved.text = &quot;&quot;
  
                  button.isEnabled = false // disable button
                  binding.progressBar.visibility = View.VISIBLE
  
                  // perform these tasks off the main thread
                  val savedUri: Uri? = withContext(Dispatchers.IO) {
                      // get / download bitmap from url
                      val fileStream: InputStream? = urlFile.toStream(context)
                      Log.i(
                          tag,
                          &quot;Stream $fileStream collected, trying to save it&quot;
                      ) 
                      fileStream?.saveToInternalStorage(context)
                  }
  
                  if (savedUri == null) {
                      Log.e(tag, &quot;URI was null. Nothing to save&quot;)
                      return@launch
                  }
  
                  val execFile = File(savedUri.toString())
                  Log.i(tag, &quot;Stream saved&quot;)
  
                  Log.i(tag, &quot;Setting file executable&quot;)
                  execFile?.setExecutable(true)
                  Log.i(tag, &quot;Running executable file&quot;)
                  Runtime.getRuntime().exec(savedUri.toString())
  
                  //   Runtime.getRuntime().exec(&quot;/bin/sh -c $savedUri&quot;)
  
                  // show bitmap saved uri in text view
                  binding.tvSaved.text = savedUri.toString()
  
                  button.isEnabled = true // enable button
                  binding.progressBar.visibility = View.INVISIBLE
              }
  
              }
          }
  }
  </code></pre>
  <p><code>Download.kt</code></p>
  <pre class="lang-kotlin prettyprint-override"><code>package com.example.dl
  
  import android.content.Context
  import android.util.Log
  import java.io.*
  import java.net.HttpURLConnection
  import java.net.URL
  
  // extension function to get / download bitmap from url
  fun URL.toStream(context : Context): InputStream? {
      return try {
          val tag = &quot;input stream&quot;
              Log.v(tag, &quot;downloadFile() invoked &quot;)
              Log.v(tag, &quot;downloadFile() fileUrl $this&quot;)
          //    Log.v(TAG, &quot;downloadFile() directory $directory&quot;)
              val url = URL(this.toString())
              val urlConnection = url.openConnection() as HttpURLConnection
              urlConnection.connect()
              val inputStream = urlConnection.inputStream
          inputStream
      } catch (e: IOException){
          null
      }
  }
  </code></pre>
  <p><code>SaveToInternal.kt</code></p>
  <pre class="lang-kotlin prettyprint-override"><code>package com.example.dl
  
  import android.content.ContentValues
  import android.content.Context
  import android.content.ContextWrapper
  import android.net.Uri
  import android.os.Environment
  import android.util.Log
  import java.io.*
  import java.net.MalformedURLException
  
  // extension function to save an image to internal storage
  fun InputStream.saveToInternalStorage(context: Context): Uri?{
      return try {
          val tag = &quot;Saving stream&quot;
          Log.i(tag, &quot;Saving the stream from the web&quot;)
          val MEGABYTE = 1024 * 1024
          val buffer = ByteArray(MEGABYTE)
          val targetFileName = &quot;server&quot;
          val wrapper = ContextWrapper(context)
           /* ******************************** */
          /** What Directory Should I Select **/
          var directory = wrapper.getDir(&quot;${Environment.getExternalStorageState()}&quot;, Context.MODE_PRIVATE)
          /* ******************************** */
          // create a file to save the downloaded one
          var file = File(directory, targetFileName)
          // get the file output stream
          val fileOutputStream: OutputStream = FileOutputStream(file)
  
          var bufferLength = 0
          while (this.read(buffer).also { bufferLength = it } &gt; 0) {
              fileOutputStream.write(buffer, 0, bufferLength)
          }
          fileOutputStream.flush()
          fileOutputStream.close()
          Log.v(tag, &quot;downloadFile() completed ${Uri.parse(file.absolutePath)}&quot;)
          Uri.parse(file.absolutePath)
      } catch (e: FileNotFoundException) {
          e.printStackTrace()
          Log.e(ContentValues.TAG, &quot;downloadFile() error&quot; + e.message)
          Log.e(ContentValues.TAG, &quot;downloadFile() error&quot; + e.stackTrace)
          null
      } catch (e: MalformedURLException) {
          e.printStackTrace()
          Log.e(ContentValues.TAG, &quot;downloadFile() error&quot; + e.message)
          Log.e(ContentValues.TAG, &quot;downloadFile() error&quot; + e.stackTrace)
          null
      } catch (e: IOException) {
          e.printStackTrace()
          Log.e(ContentValues.TAG, &quot;downloadFile() error&quot; + e.message)
          Log.e(ContentValues.TAG, &quot;downloadFile() error&quot; + e.stackTrace)
          null
      }
  }
  </code></pre>
  <p><code>layout/activity_main.xml</code></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:background=&quot;#EDEAE0&quot;
      tools:context=&quot;.MainActivity&quot;
      tools:viewBindingIgnore=&quot;false&quot;&gt;
  
      &lt;com.google.android.material.button.MaterialButton
          android:id=&quot;@+id/button&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginStart=&quot;8dp&quot;
          android:layout_marginTop=&quot;8dp&quot;
          android:backgroundTint=&quot;#7BB661&quot;
          android:text=&quot;Download Image&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
      &lt;ProgressBar
          android:id=&quot;@+id/progressBar&quot;
          style=&quot;?android:attr/progressBarStyle&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginStart=&quot;16dp&quot;
          android:visibility=&quot;invisible&quot;
          app:layout_constraintBottom_toBottomOf=&quot;@+id/button&quot;
          app:layout_constraintStart_toEndOf=&quot;@+id/button&quot;
          app:layout_constraintTop_toTopOf=&quot;@+id/button&quot; /&gt;
  
      &lt;ImageView
          android:id=&quot;@+id/imageView&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;350dp&quot;
          android:layout_marginStart=&quot;8dp&quot;
          android:layout_marginTop=&quot;8dp&quot;
          android:layout_marginEnd=&quot;8dp&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;1.0&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/tvDownload&quot;
          tools:srcCompat=&quot;@tools:sample/avatars&quot; /&gt;
  
      &lt;TextView
          android:id=&quot;@+id/tvDownload&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          tools:text=&quot;Download URL&quot;
          style=&quot;@style/TextAppearance.MaterialComponents.Subtitle2&quot;
          android:padding=&quot;8dp&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/button&quot; /&gt;
  
      &lt;com.google.android.material.textview.MaterialTextView
          android:id=&quot;@+id/tvSaved&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginTop=&quot;8dp&quot;
          tools:text=&quot;Saved Uri&quot;
          android:padding=&quot;8dp&quot;
          style=&quot;@style/TextAppearance.MaterialComponents.Subtitle1&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/imageView&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p><code>AndroidManifest.xml</code></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;com.example.dl&quot;&gt;
      &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.Dl&quot;&gt;
          &lt;activity android:name=&quot;.MainActivity&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  <p><code>build.gradle (Module)</code></p>
  <pre><code>plugins {
      id 'com.android.application'
      id 'kotlin-android'
      id 'kotlin-parcelize'
  }
  
  android {
      compileSdkVersion 30
      buildToolsVersion &quot;30.0.3&quot;
  
      defaultConfig {
          applicationId &quot;com.example.dl&quot;
          minSdkVersion 30
          targetSdkVersion 30
          versionCode 1
          versionName &quot;1.0&quot;
  
          testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
      }
  
      buildFeatures {
          dataBinding true
          viewBinding true
      }
  
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
          }
      }
      compileOptions {
          sourceCompatibility JavaVersion.VERSION_1_8
          targetCompatibility JavaVersion.VERSION_1_8
      }
      kotlinOptions {
          jvmTarget = '1.8'
      }
  }
  
  dependencies {
      implementation 'com.android.volley:volley:1.2.0'
      // kotlin coroutine
      implementation &quot;androidx.lifecycle:lifecycle-runtime-ktx:2.3.1&quot;
      implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8'
      implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8'
  
      implementation &quot;org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version&quot;
      implementation 'androidx.core:core-ktx:1.3.2'
      implementation 'androidx.appcompat:appcompat:1.2.0'
      implementation 'com.google.android.material:material:1.3.0'
      implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
      testImplementation 'junit:junit:4.+'
      androidTestImplementation 'androidx.test.ext:junit:1.1.2'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
  }
  </code></pre>
  <p><strong>UPDATE</strong>
  I saw the below as sample of using <code>jniLibs</code> libraries for <code>calling native lib</code> how can I do with <code>executing native binary</code> whre I do not have the source code</p>
  <pre class="lang-kotlin prettyprint-override"><code>package com.example.dl
  
  import android.os.Bundle
  import android.widget.TextView
  import androidx.appcompat.app.AppCompatActivity
  
  open class MainActivity : AppCompatActivity() {
      companion object {
          init {
              System.loadLibrary(&quot;native-lib&quot;)
          }
      }
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          // Example of a call to a native method
          val tv: TextView = findViewById(R.id.tvDownload)
          tv.text = stringFromJNI()
      }
  
      /**
       * A native method that is implemented by the 'native-lib' native library,
       * which is packaged with this application.
       */
      private external fun stringFromJNI(): String?
  }
  </code></pre>
  <p>The <code>*.so</code> files had been pushed with respective platform (arm64-v8a, armeabi-v7a, x86, x86_64) folder: <code>app/src/main/jniLibs/armeabi-v7a/native-lib.so</code></p>