【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: 下载地址

源代码及工具下载:下载地址

------------
微博:http://weibo.com/scue
Github:http://github.com/scue
原文地址:https://www.cnblogs.com/scue/p/3092017.html