【Windows】使用adb命令卸載系統應用程序


/*
 * =====================================================================================
 *
 *       Filename:  git text.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2013/5/21 15:00:25
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  linkscue (scue), 
 *   Organization:  ATX風雅組
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* 把字符串尾的.apk替換成.odex */
char *strapk2odex(char *apk, char *odex){
    char apkl[64]="";
    int i=0,len=strlen(apk);
    sprintf(apkl, "%s", apk);
    strncpy(odex, apkl, (len-3));
    sprintf(odex, "%sodex",odex);
    return odex;
}

int main( int argc, char *argv[] )
{

    FILE *fp;
    int i,j,count=0,status=-1,recovery=0,mp3_start=0;
    char device[32]="";
    char cmd[256]="";
    char cmd2[256]="";
    char path[128]="";
    char app_buf[512][128]={"", };
    unsigned char c,c1;

    /* 風雅組信息 */
    printf("\n");
    printf("**************************************************\n");
    printf("*  一鍵清理手機系統應用v2.0 by scue @ATX-風雅組  *\n");
    printf("*  安智|風雅組 榮譽出品 支持論壇:bbs.anzhi.com  *\n");
    printf("**************************************************\n");
    printf("\n");

    /* 獲取已連接手機的信息 */
    printf("<<< 正在獲取設備信息..\n");
    sprintf(cmd, "adb devices | findstr /E device >nul");
    sprintf(cmd2, "adb devices | findstr /E device");
    status=system(cmd);
    if (status != 0) {
        sprintf(cmd, "adb devices | findstr /E recovery >nul");
        sprintf(cmd2, "adb devices | findstr /E recovery");
        status=system(cmd);
        if (status != 0) {
            printf(">>> 未發現有連接設備,請檢測是否已有設備連接.\n");
            system("pause");
            exit(1);
        }
        else{
            recovery=1;
        }
    }
    fp = popen(cmd2, "r");
    if (fp == NULL) {
        printf(">>> 無法執行Windows Command Shell的命令.\n" );
        getchar();
        exit;
    }
    fgets(path, sizeof(path)-1, fp);
    pclose(fp);
    for (i = 0; *(path + i) != 9 ; i++) {       /* ASCII碼9是tab鍵 */
        device[i]=*(path+i);
    }
    printf("\n");
    printf(">>> 連接設備:%s\n", device);
    if (recovery) {
        printf(">>> 手機狀態:恢復模式\n");
    }
    else{
        printf(">>> 手機狀態:正常開機\n");
    }

    /* 初始化環境,必須的工具上傳至手機中 */
    system("adb shell mount /system 1>nul 2>nul");
    system("adb shell mount /data 1>nul 2>nul");
    system("adb shell mkdir -p /data/local/tmp 1>nul 2>nul");
    system("adb shell chmod -R 777 /data/local/tmp >nul");
    system("adb push aapt /data/local/tmp/aapt 1>nul 2>nul");
    system("adb shell chmod 755 /data/local/tmp/aapt >nul");
    system("adb shell mount -o remount,rw /system 1>nul 2>nul");

    /* 獲取手機中的程序列表 */
    printf("\n");
    printf(">>> 手機信息檢測完畢,按任意鍵進入下一步操作:");
    getchar();
    printf("\n");
    printf("<<< 正在獲取設備上的系統應用程序..\n");
    printf("\n");
    printf("##################################################\n");
    fp = popen("adb shell ls /system/app/*.apk", "r");                  /* 顯示apk文件列表 */
    i=0;
    while(fgets(path, sizeof(path)-1, fp) != NULL ){
        sprintf(app_buf[i], "%s", path);
        printf("[%03d] %s", i+1, app_buf[i]);
        i++;count++;
    }
    pclose(fp);
    mp3_start=count;
    printf("##################################################\n");
    fp = popen("adb shell ls /system/customize/resource/*.mp3", "r");   /* 顯示mp3文件列表 */
    while(fgets(path, sizeof(path)-1, fp) != NULL ){
        sprintf(app_buf[i], "%s", path);
        printf("[%03d] %s", i+1, app_buf[i]);
        i++;count++;
    }
    pclose(fp);
    printf("##################################################\n");

    /* 提示用戶卸載以執行卸載命令 */
    printf("\n");
    printf(">>> 請輸入您需要卸載的系統應用程序的序號[多選|空格分開]:");
    scanf("%[^\n]", path);
    getchar();                                  /* 接收回車字符 */
    printf("\n");
    int select=0;                               /* 檢測輸入的變量 */
    char *start,*end;                           /* 用於從字符串中取出程序包名 */
    char apkl[64]="";
    char odex[64]="";
    char mp3l[64]="";
    char package[64]="";
    for (i = 0; i < strlen(path)+1; i++) {
        c=*(path+i);
        if ( (c == 32 || c == 0) && (select!=0) ) {
            if ( select > count ) {
                printf("溫馨提示:您選擇的數值過大,忽略此項操作。\n\n");
                continue;
            }
            /* 執行卸載/刪除動作 */
            if (select < mp3_start) {           /* 卸載系統應用 */

                /* 變量初始化 */
                strncpy(odex, "", sizeof(odex)); /* 清空odex變量 */
                strncpy(apkl, app_buf[select-1], (strlen(app_buf[select-1])-2)); /* 獲取apk名稱 */
                sprintf(odex, "%s", strapk2odex(apkl,odex));    /* 獲取odex名稱 */

                /* 獲取得應用程序的包名信息 */
                sprintf(cmd, "adb shell /data/local/tmp/aapt d badging %s | findstr package", apkl);
                fp = popen(cmd, "r");
                fgets(cmd, sizeof(cmd)-1, fp);
                pclose(fp);
                start=strchr(cmd, 39)+1;
                for (j = 0; *(start+j) != 39; j++) {
                    package[j]=*(start+j);
                }
                package[j+1]=0;

                /* 執行卸載操作 */
                printf("正在卸載:%s\n", package);
                sprintf(cmd, "adb shell pm clear %s 1>nul 2>nul", package); /* 清空應用數據 */
                sprintf(cmd2, "adb shell pm clear %s", package);
                printf(" >%s\n",cmd2 );
                system(cmd);
                sprintf(cmd, "adb shell rm %s 1>nul 2>nul", apkl); /* 刪除apk文件 */
                sprintf(cmd2, "adb shell rm %s", apkl);
                printf(" >%s\n",cmd2 );
                system(cmd);
                sprintf(cmd, "adb shell rm %s 1>nul 2>nul", odex); /* 刪除odex文件 */
                sprintf(cmd2, "adb shell rm %s", odex);
                printf(" >%s\n",cmd2 );
                system(cmd);
            }
            else{                               /* 刪除mp3文件 */
                printf("正在清理:%s\n", app_buf[select-1]);
                strncpy(mp3l, app_buf[select-1], (strlen(app_buf[select-1])-2));
                sprintf(cmd, "adb shell rm %s 1>nul 2>nul", mp3l);
                sprintf(cmd2, "adb shell rm %s", mp3l);
                printf(" >%s\n",cmd2 );
                system(cmd);
            }
            printf("\n");
            select=0;                           /* 重置已選擇的為零 */
        }
        else if ( c >= 48 && c <= 57 ){
            select = select*10+(c-'0'); 
        }
    }

    /* 后續操作 */
    system("adb shell rm /data/local/tmp/aapt 1>nul 2>nul");
    system("adb shell mount -o remount,ro /system 1>nul 2>nul");

    /* 暫停*/
    printf("感謝大家對安智網風雅組的支持,作者:scue@ATX。");
    getchar();                                  /* 接收任意鍵 */
    printf("\n");

    return 0;
}

 aapt for Android: 下載地址

源代碼及工具下載:下載地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM