All Projects β†’ jaredrummler β†’ KtSh

jaredrummler / KtSh

Licence: Apache-2.0 license
Execute shell commands on Android or the JVM

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to KtSh

getroot
πŸ› οΈ Tool to bypass my school's security system to get sudo privileges on MacOS
Stars: ✭ 34 (-94.47%)
Mutual labels:  root
GJZS
ζžζœΊεŠ©ζ‰‹Β·RοΌˆεŽŸγ€ŒζžζœΊεŠ©ζ‰‹ι‡εˆΆη‰ˆγ€οΌ‰
Stars: ✭ 584 (-5.04%)
Mutual labels:  root
root painter
RootPainter: Deep Learning Segmentation of Biological Images with Corrective Annotation
Stars: ✭ 28 (-95.45%)
Mutual labels:  root
tenda-reverse
Reverse engineering, getting root access to Tenda MW6 wifi mesh router
Stars: ✭ 90 (-85.37%)
Mutual labels:  root
Droid-Explorer
Cross-platform root file explorer for Android phones.
Stars: ✭ 34 (-94.47%)
Mutual labels:  root
A71-Hidden-Mods
A magisk module adding some mods to your Galaxy A71 systemlessly.
Stars: ✭ 16 (-97.4%)
Mutual labels:  root
isu
An app that help understanding safety net verification fail reason
Stars: ✭ 39 (-93.66%)
Mutual labels:  root
jobofferbackend
This project is a real-world example of DDD in a backend application It applies the concept of Entity, Value Object, Root, Aggregate, Services, Repositories and Ubiquitous Language.
Stars: ✭ 15 (-97.56%)
Mutual labels:  root
avd-root
Root permissions for Android Virtual Devices
Stars: ✭ 112 (-81.79%)
Mutual labels:  root
root-docker
Docker recipes for ROOT
Stars: ✭ 23 (-96.26%)
Mutual labels:  root
AutoAirplaneMode
✈️ Automatic Aiplane Mode for Android
Stars: ✭ 15 (-97.56%)
Mutual labels:  root
chromiumUpdater
Downloads Chromium for Android and keeps it updated
Stars: ✭ 69 (-88.78%)
Mutual labels:  root
BatteryCalibration
not maintained - [NEEDS ROOT] Calibrate your battery
Stars: ✭ 20 (-96.75%)
Mutual labels:  root
aev
Android library to verify the safety of user devices. Make sure that API calls from your app can be trusted. Instantly detect rooted devices, emulators, cloned apps, and other risk factors.
Stars: ✭ 64 (-89.59%)
Mutual labels:  root
spark-root
Apache Spark Data Source for ROOT File Format
Stars: ✭ 28 (-95.45%)
Mutual labels:  root
Instant-Face-Unlock
Xposed Module's InstantFaceUnlock Code
Stars: ✭ 23 (-96.26%)
Mutual labels:  root
MTS
Automation Tools for PHP
Stars: ✭ 111 (-81.95%)
Mutual labels:  root
Latest-adb-fastboot-installer-for-windows
A Simple Android Driver installer tool for windows (Always installs the latest version). Sponsored by https://sendletter.org, the Cheapest way to Send Letters
Stars: ✭ 242 (-60.65%)
Mutual labels:  root
DIRT
Digital Imaging of Root Traits: Extract trait measurements from images of monocot and dicot roots.
Stars: ✭ 20 (-96.75%)
Mutual labels:  root
Root-Checker
Displays all root related info(Device Rooted, Root Available, Root Path, Root given, Busy Box installation) of an Android Device. Fully implemented in Jetpack compose using Material 3 dynamic theming and also has a separate implementation in xml with MDC 3.
Stars: ✭ 21 (-96.59%)
Mutual labels:  root

KtSh

An open source library to execute shell commands on Android or the JVM, written in Kotlin.

License Build Status Maven Central

Downloads

Download the latest JAR or grab via Gradle:

implementation 'com.jaredrummler:ktsh:1.0.0'

Alternatively, you can simply copy Shell.kt file to your project.

Usage

Basic usage:

val shell = Shell("sh")                         // create a shell
val result = shell.run("echo 'Hello, World!'")  // execute a command
if (result.isSuccess) {                         // check if the exit-code was 0
    println(result.stdout())                    // prints "Hello, World!"
}

Construct a new Shell instance:

// Construct a new shell instance with additional environment variables
val shell = Shell("sh", "USER" to "Chuck Norris", "ENV_VAR" to "VALUE")

// Construct a new shell instance with path to the shell:
val bash = Shell("/bin/bash")

Note: If the shell does not exist a Shell.NotFoundException is thrown as a RuntimeException.

Execute a command and get the result:

val shell = Shell.SH
val result: Shell.Command.Result = shell.run("ls")

A Shell.Command.Result contains the following:

  • stdout: A list of lines read from the standard input stream.
  • stderr: A list of lines read from the standard error stream.
  • exitCode: The exit status from running the command.
  • details: Additional information (start, stop, elapsed time, id, command)

Add a callback when stdout or stderr is read:

shell.addOnStderrLineListener(object : Shell.OnLineListener {
  override fun onLine(line: String) {
      // do something
  }
})

Add a callback that is invoked each time a command completes:

shell.addOnCommandResultListener(object : Shell.OnCommandResultListener {
  override fun onResult(result: Shell.Command.Result) {
    // do something with the result
  }
})

Execute a command with custom options:

Optionally, you can configure how each command executes by setting a timeout, redirecting stderr to stdout, add callbacks for when the command reads a line from stdout/stderr or is cancelled.

// NOTE: all of these are optional
val result = Shell.SH.run(command) {
  // Kill the command after 1 minute
  timeout = Shell.Timeout(1, TimeUnit.MINUTES)
  // Redirect STDOUT to STDERR
  redirectErrorStream = false
  // Callbacks:
  onCancelled = {
    // The command was cancelled
  }
  onStdErr = { line: String ->
    // Do something when reading a line from standard error stream
  }
  onStdOut = { line: String ->
    // Do something when reading a line from standard output stream
  }
  // Do not notify any listeners added via Shell.addOnStderrLineListener and Shell.addOnStdoutLineListener
  notify = false
}

Check the state of the shell:

if (shell.isRunning()) {
  // The shell is running a command
} else if (shell.isShutdown()) {
  // The shell has been killed
} else if (shell.isIdle()) {
  // The shell is open and not running any commands
}

or

when (shell.state) {
  State.Idle -> TODO()
  State.Running -> TODO()
  State.Shutdown -> TODO()
}

Shutdown the shell

shell.shutdown()

Interrupt waiting for a command to complete:

shell.interrupt()

Background processing on Android

Creating a new instance of a Shell or executing commands should be done on a separate thread other than the UI thread. This is up to the library user. An example of this can be found in the demo project using Kotlin coroutines and AndroidX libraries:

fun run(
    shell: Shell,
    command: String,
    callback: (result: Shell.Command.Result) -> Unit
) = viewModelScope.launch {
    val result = withContext(Dispatchers.IO) { shell.run(command) }
    withContext(Dispatchers.Main) { callback(result) }
}

Structure

  • buildSrc - contains dependencies, plugins, versions for Gradle build logic
  • build.gradle.kts - root gradle config file
  • settings.gradle.kts - root gradle settings file
  • library - the ktsh library
  • library/src/test - unit tests for the library
  • demo - Android demo project using ktsh
  • scripts - scripts to publish library to maven central
  • .github - any files for the github page

Similar projects:

License

Copyright (C) 2021 Jared Rummler

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].