Skip to content

Commit

Permalink
修改了命名
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanren1225 committed Jul 3, 2019
1 parent cdfcb96 commit 16300ca
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 1 deletion.
1 change: 1 addition & 0 deletions executeshell_lib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
22 changes: 22 additions & 0 deletions executeshell_lib/build.gradle
Original file line number Diff line number Diff line change
@@ -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'])
}
21 changes: 21 additions & 0 deletions executeshell_lib/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions executeshell_lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manifest
package="cn.endureblaze.executeshell_lib">

<application />
</manifest>
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions executeshell_lib/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">executeshell</string>
</resources>
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 16300ca

Please sign in to comment.