Skip to content

Commit

Permalink
1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanren1225 committed Apr 26, 2020
1 parent f156117 commit ee5b4e4
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 112 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ ExecuteShell 是一个可以让你在你的 Android 项目上方便快速使用
## 配置要求
ExecuteShell 最低支持到Android [Ice Cream Sandwich](https://developer.android.com/about/versions/android-4.0-highlights.html) (API 14)
## 开始使用

> 版本号请看顶部
### Gradle
1.在项目根目录的 build.gradle 添加如下代码
```Java
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}

}
```
2.在需要使用模块(一般为app)的 build.gradle 中添加依赖
```Java
dependencies {
implementation 'com.github.EndureBlaze:executeshell:1.1'
implementation 'com.github.EndureBlaze:executeshell:$version'
}
```
### Maven
Expand All @@ -35,7 +38,7 @@ dependencies {
<dependency>
<groupId>com.github.EndureBlaze</groupId>
<artifactId>executeshell</artifactId>
<version>1.1</version>
<version>$version</version>
</dependency>
```
## 功能说明
Expand Down Expand Up @@ -68,7 +71,7 @@ shell_str 是转换为 String 类型的 shell 指令
返回其他数字代表执行失败

## 演示
可以在 [smaple](https://github.com/EndureBlaze/executeshell/tree/master/sample) 查看详细使用,或者下载 [apk](https://github.com/EndureBlaze/executeshell/blob/master/sample.apk) 实际查看使用结果
可以在 [sample](https://github.com/EndureBlaze/executeshell/tree/master/sample) 查看详细使用,或者下载 [apk](https://cdn.jsdelivr.net/gh/EndureBlaze/ExecuteShell/sample.apk) 实际查看使用结果

## 关于依赖库
本项目没有使用任何依赖库
Expand Down
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ buildscript {
jcenter()
maven { url "https://maven.google.com" }
maven { url 'https://jitpack.io' }

}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.android.tools.build:gradle:3.6.0-rc01'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
6 changes: 3 additions & 3 deletions executeshell_lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
buildToolsVersion "29.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 29
versionCode 2
versionName "1.1"
versionCode 3
versionName "1.2"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,41 @@
import java.io.DataOutputStream;
import java.io.IOException;

public final class ExecuteShell
{
public final class ExecuteShell {
private static final String TAG = "RootShell";
private static boolean mHaveRoot = false;

/**
* 判断机器Android是否已经root,即是否获取root权限
* 判断机器Android是否已经root,即是否获取root权限
*
* @return 有 root 返回 true,否则返回 false
*/
public static boolean haveRoot()
{
if (!mHaveRoot)
{
public static boolean haveRoot() {
if (!mHaveRoot) {
int ret = execRootShellSilent("echo test"); // 通过执行测试命令来检测
if (ret != -1)
{
if (ret != -1) {
Log.i(TAG, "have root!");
mHaveRoot = true;
}
else
{
} else {
Log.i(TAG, "not root!");
}
}
else
{
} else {
Log.i(TAG, "mHaveRoot = true, have root!");
}
return mHaveRoot;
}

/**
* 执行命令并且输出结果
* @param shell 需要执行的 shell 命令
* @return 返回执行的结果,如果返回空串则执行失败
*/
public static String execRootShell(String shell)
{
public static String execRootShell(String shell) {
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;

try
{
try {
Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
dos = new DataOutputStream(p.getOutputStream());
dis = new DataInputStream(p.getInputStream());
Expand All @@ -56,38 +51,25 @@ public static String execRootShell(String shell)
dos.writeBytes("exit\n");
dos.flush();
String line = null;
while ((line = dis.readLine()) != null)
{
while ((line = dis.readLine()) != null) {
Log.d("result", line);
result += line;
}
p.waitFor();
}
catch (Exception e)
{
} catch (Exception e) {
e.printStackTrace();
}
finally
{
if (dos != null)
{
try
{
} finally {
if (dos != null) {
try {
dos.close();
}
catch (IOException e)
{
} catch (IOException e) {
e.printStackTrace();
}
}
if (dis != null)
{
try
{
if (dis != null) {
try {
dis.close();
}
catch (IOException e)
{
} catch (IOException e) {
e.printStackTrace();
}
}
Expand All @@ -97,14 +79,14 @@ public static String execRootShell(String shell)

/**
* 执行命令但不关注结果输出
* @param shell 需要执行的 shell 命令
* @return 返回 0 代表执行成功 返回其他数字代表执行失败
*/
public static int execRootShellSilent(String shell)
{
public static int execRootShellSilent(String shell) {
int result = -1;
DataOutputStream dos = null;

try
{
try {
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());

Expand All @@ -115,21 +97,13 @@ public static int execRootShellSilent(String shell)
dos.flush();
p.waitFor();
result = p.exitValue();
}
catch (Exception e)
{
} catch (Exception e) {
e.printStackTrace();
}
finally
{
if (dos != null)
{
try
{
} finally {
if (dos != null) {
try {
dos.close();
}
catch (IOException e)
{
} catch (IOException e) {
e.printStackTrace();
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
Binary file added sample.apk
Binary file not shown.
10 changes: 4 additions & 6 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "cn.endureblaze.executeshellsample"
minSdkVersion 14
targetSdkVersion 29
versionCode 2
versionName "1.1"
versionCode 3
versionName "1.2"
}
buildTypes {
release {
Expand All @@ -21,7 +21,5 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
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'
//implementation 'com.github.EndureBlaze:executeshell:1.1'
}
22 changes: 11 additions & 11 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
xmlns:tools="http://schemas.android.com/tools" package="cn.endureblaze.executeshellsample">

<application
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity
android:name=".ExecuteShellActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
android:label="@string/app_name" tools:ignore="AllowBackup">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Loading

0 comments on commit ee5b4e4

Please sign in to comment.