# 2020-07-13-Anroid-KillApp

In some cases, developer might want to close app forcefully.

* e.g. App must update to latest version
* e.g. App can not run on rooted device
* e.g. App doesn't meet the hardware requirement - Gyroscope required.

```
finishAffinity()
```

Here is one of the example. Here we're putting a check - if system is rooted, kill the app after showing a dialog.

## Utilities/RootChecker.kt

```kotlin
import java.io.File

class RootChecker {
    fun isRooted(): Boolean {
        return findBinary("su")
    }

    fun findBinary(binaryName: String): Boolean {
        var found = false
        if (!found) {
            val places = arrayOf(
                "/sbin/", "/system/bin/", "/system/xbin/", "/data/local/xbin/",
                "/data/local/bin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/"
            )
            for (where in places) {
                if (File(where + binaryName).exists()) {
                    found = true
                    break
                }
            }
        }
        return found
    }
}
```

## MainActivity.kt

```kotlin
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (RootChecker().isRooted()) {
            AlertDialog.Builder(this)
                .setTitle("Found rooted system")
                .setMessage("Please install on an android which isn't rooted")
                .setPositiveButton("Okay.") { _, _ ->
                    finishAffinity()
                }
                .setOnDismissListener {
                    finishAffinity()
                }
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show()
        }
    }

}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sagar-r-kothari.gitbook.io/blog/_posts/2020-07-13-anroid-killapp.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
