From 16300cacb663d9ba64ce6981c8a0fe31ee78f44c Mon Sep 17 00:00:00 2001 From: endureblaze Date: Wed, 3 Jul 2019 14:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- executeshell_lib/.gitignore | 1 + executeshell_lib/build.gradle | 22 +++ executeshell_lib/proguard-rules.pro | 21 +++ executeshell_lib/src/main/AndroidManifest.xml | 5 + .../executeshell/ExecuteShell.java | 139 ++++++++++++++++++ .../src/main/res/values/strings.xml | 3 + sample/build.gradle | 2 +- 7 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 executeshell_lib/.gitignore create mode 100644 executeshell_lib/build.gradle create mode 100644 executeshell_lib/proguard-rules.pro create mode 100644 executeshell_lib/src/main/AndroidManifest.xml create mode 100644 executeshell_lib/src/main/java/cn/endureblaze/executeshell/ExecuteShell.java create mode 100644 executeshell_lib/src/main/res/values/strings.xml diff --git a/executeshell_lib/.gitignore b/executeshell_lib/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/executeshell_lib/.gitignore @@ -0,0 +1 @@ +/build diff --git a/executeshell_lib/build.gradle b/executeshell_lib/build.gradle new file mode 100644 index 0000000..e636f55 --- /dev/null +++ b/executeshell_lib/build.gradle @@ -0,0 +1,22 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 29 + buildToolsVersion "29.0.0" + defaultConfig { + minSdkVersion 15 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) +} diff --git a/executeshell_lib/proguard-rules.pro b/executeshell_lib/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/executeshell_lib/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/executeshell_lib/src/main/AndroidManifest.xml b/executeshell_lib/src/main/AndroidManifest.xml new file mode 100644 index 0000000..ea25028 --- /dev/null +++ b/executeshell_lib/src/main/AndroidManifest.xml @@ -0,0 +1,5 @@ + + + + diff --git a/executeshell_lib/src/main/java/cn/endureblaze/executeshell/ExecuteShell.java b/executeshell_lib/src/main/java/cn/endureblaze/executeshell/ExecuteShell.java new file mode 100644 index 0000000..5880047 --- /dev/null +++ b/executeshell_lib/src/main/java/cn/endureblaze/executeshell/ExecuteShell.java @@ -0,0 +1,139 @@ +package cn.endureblaze.executeshell; + +import android.util.Log; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public final class ExecuteShell +{ + private static final String TAG = "RootCmd"; + private static boolean mHaveRoot = false; + /** + * 判断机器Android是否已经root,即是否获取root权限 + */ + public static boolean haveRoot() + { + if (!mHaveRoot) + { + int ret = execRootCmdSilent("echo test"); // 通过执行测试命令来检测 + if (ret != -1) + { + Log.i(TAG, "have root!"); + mHaveRoot = true; + } + else + { + Log.i(TAG, "not root!"); + } + } + else + { + Log.i(TAG, "mHaveRoot = true, have root!"); + } + return mHaveRoot; + } + + /** + * 执行命令并且输出结果 + */ + public static String execRootCmd(String cmd) + { + String result = ""; + DataOutputStream dos = null; + DataInputStream dis = null; + + try + { + Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令 + dos = new DataOutputStream(p.getOutputStream()); + dis = new DataInputStream(p.getInputStream()); + + Log.i(TAG, cmd); + dos.writeBytes(cmd + "\n"); + dos.flush(); + dos.writeBytes("exit\n"); + dos.flush(); + String line = null; + while ((line = dis.readLine()) != null) + { + Log.d("result", line); + result += line; + } + p.waitFor(); + } + catch (Exception e) + { + e.printStackTrace(); + } + finally + { + if (dos != null) + { + try + { + dos.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + if (dis != null) + { + try + { + dis.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + return result; + } + + /** + * 执行命令但不关注结果输出 + */ + public static int execRootCmdSilent(String cmd) + { + int result = -1; + DataOutputStream dos = null; + + try + { + Process p = Runtime.getRuntime().exec("su"); + dos = new DataOutputStream(p.getOutputStream()); + + Log.i(TAG, cmd); + dos.writeBytes(cmd + "\n"); + dos.flush(); + dos.writeBytes("exit\n"); + dos.flush(); + p.waitFor(); + result = p.exitValue(); + } + catch (Exception e) + { + e.printStackTrace(); + } + finally + { + if (dos != null) + { + try + { + dos.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + } + return result; + } +} \ No newline at end of file diff --git a/executeshell_lib/src/main/res/values/strings.xml b/executeshell_lib/src/main/res/values/strings.xml new file mode 100644 index 0000000..f2fb39b --- /dev/null +++ b/executeshell_lib/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + executeshell + diff --git a/sample/build.gradle b/sample/build.gradle index 67ee926..308b61d 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -20,7 +20,7 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation project(':executeshell') + implementation project(':executeshell_lib') implementation 'androidx.appcompat:appcompat:1.1.0-beta01' implementation 'com.google.android.material:material:1.1.0-alpha07' implementation 'androidx.legacy:legacy-support-v4:1.0.0'