Android中级——系统信息与安全机制

系统信息获取

/system/build.prop

存放一些配置信息(需要Google apis的模拟器才能运行su)

在这里插入图片描述

此外,还可以通过 getprop 获取上面的信息

在这里插入图片描述

/proc

存储系统运行时各个程序的相关信息

在这里插入图片描述

android.os.build

包含了系统编译时的设备、配置信息

Log.d(TAG, "主板: " + Build.BOARD);
Log.d(TAG, "Android系统定制商: " + Build.BRAND);
for (int i = 0; i < Build.SUPPORTED_ABIS.length; i++) {
    Log.d(TAG, "CPU指令集" + "[" + i + "]" + Build.SUPPORTED_ABIS[i]);
}
Log.d(TAG, "设备参数: " + Build.DEVICE);
Log.d(TAG, "显示屏参数: " + Build.DISPLAY);
Log.d(TAG, "唯一编号: " + Build.FINGERPRINT);
Log.d(TAG, "硬件序列号: " + Build.SERIAL);
Log.d(TAG, "修订版本列表: " + Build.ID);
Log.d(TAG, "硬件制造商: " + Build.MANUFACTURER);
Log.d(TAG, "版本: " + Build.MODEL);
Log.d(TAG, "硬件名: " + Build.HARDWARE);
Log.d(TAG, "手机产品名: " + Build.PRODUCT);
Log.d(TAG, "描述Build的标签: " + Build.TAGS);
Log.d(TAG, "Builder类型: " + Build.TYPE);
Log.d(TAG, "当前开发代号: " + Build.VERSION.CODENAME);
Log.d(TAG, "源码控制版本号: " + Build.VERSION.INCREMENTAL);
Log.d(TAG, "版本字符串: " + Build.VERSION.RELEASE);
Log.d(TAG, "版本号: " + Build.VERSION.SDK_INT);
Log.d(TAG, "host值: " + Build.HOST);
Log.d(TAG, "User名: " + Build.USER);
Log.d(TAG, "编译时间: " + Build.TIME);

模拟器打印如下

主板: goldfish_x86_64
Android系统定制商: google
CPU指令集[0]: x86_64
CPU指令集[1]: x86
设备参数: generic_x86_64
显示屏参数: sdk_gphone_x86_64-userdebug 9 PSR1.180720.093 5456446 dev-keys
唯一编号: google/sdk_gphone_x86_64/generic_x86_64:9/PSR1.180720.093/5456446:userdebug/dev-keys
硬件序列号: unknown
修订版本列表: PSR1.180720.093
硬件制造商: Google
版本: Android SDK built for x86_64
硬件名: ranchu
手机产品名: sdk_gphone_x86_64
描述Build的标签: dev-keys
Builder类型: userdebug
当前开发代号: REL
源码控制版本号: 5456446
版本字符串: 9
版本号: 28
host值: xpce4.ams.corp.google.com
User名: android-build
编译时间: 1554935165000

SystemProperty

包含了许多系统配置属性值和参数

Log.d(TAG, "OS版本: " + System.getProperty("os.version"));
Log.d(TAG, "OS名称: " + System.getProperty("os.name"));
Log.d(TAG, "OS架构: " + System.getProperty("os.arch"));
Log.d(TAG, "Home属性: " + System.getProperty("user.home"));
Log.d(TAG, "Name属性: " + System.getProperty("user.name"));
Log.d(TAG, "Dir属性: " + System.getProperty("user.dir"));
Log.d(TAG, "时区: " + System.getProperty("user.timezone"));
Log.d(TAG, "路径分隔符: " + System.getProperty("path.separator"));
Log.d(TAG, "行分隔符: " + System.getProperty("line.separator"));
Log.d(TAG, "文件分隔符: " + System.getProperty("file.separator"));
Log.d(TAG, "Java vender Url属性: " + System.getProperty("java.vendor.url"));
Log.d(TAG, "Java Class路径: " + System.getProperty("java.class.path"));
Log.d(TAG, "Java Class版本: " + System.getProperty("java.class.version"));
Log.d(TAG, "Java Vender属性: " + System.getProperty("java.vendor"));
Log.d(TAG, "Java版本: " + System.getProperty("java.version"));
Log.d(TAG, "Java Home属性: " + System.getProperty("java.home"));

模拟器打印如下

OS版本: 4.4.124+
OS名称: Linux
OS架构: x86_64
Home属性: 
Name属性: root
Dir属性: /
时区: null
路径分隔符: :
行分隔符: 
文件分隔符: /
Java vender Url属性: http://www.android.com/
Java Class路径: .
Java Class版本: 50.0
Java Vender属性: The Android Project
Java版本: 0
Java Home属性: /system

PackageManager

用于获取应用的包信息,如

  • ActivityInfo:Mainifest中<activity>和<receiver>标签之间的信息
  • ServiceInfo:Mainifest中<service>标签之间的信息
  • ApplicationInfo:Mainifest中<Application>标签之间的信息
  • PackageInfo:所有的Activity和Service信息
  • ResolveInfo:<intent>的上一级信息,如ActivityInfo、ServiceInfo,通常用于查找包含特定Intent的信息

如下创建bean类存储App的标签、图标和包名

public class PMAppInfo {

    private String appLabel;
    private Drawable appIcon;
    private String pkgName;

    public PMAppInfo() {
    }

    public String getAppLabel() {
        return appLabel;
    }

    public void setAppLabel(String appLabel) {
        this.appLabel = appLabel;
    }

    public Drawable getAppIcon() {
        return appIcon;
    }

    public void setAppIcon(Drawable appIcon) {
        this.appIcon = appIcon;
    }

    public String getPkgName() {
        return pkgName;
    }

    public void setPkgName(String pkgName) {
        this.pkgName = pkgName;
    }
}

创建适配布局list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_pkg_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

创建适配器

public class PkgAdapter extends BaseAdapter {
    private Context mContext;
    private List<PMAppInfo> mList;

    public PkgAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
    }

    public void setData(List<PMAppInfo> list) {
        mList.clear();
        mList.addAll(list);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public PMAppInfo getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        PMAppInfo item = getItem(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
            holder.mIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.mLabel = (TextView) convertView.findViewById(R.id.tv_label);
            holder.mPkgName = (TextView) convertView.findViewById(R.id.tv_pkg_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mIcon.setImageDrawable(item.getAppIcon());
        holder.mLabel.setText(item.getAppLabel());
        holder.mPkgName.setText(item.getPkgName());
        return convertView;
    }

    static class ViewHolder {
        ImageView mIcon;
        TextView mLabel;
        TextView mPkgName;
    }
}

activity_main.xml布局如下,根据点击的按钮加载ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_all"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="All APP"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_system"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="System App"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_third"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Other App"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn_sdcard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sdcard App"
        android:textAllCaps="false" />

    <ListView
        android:id="@+id/app_info"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

根据PackageManager获取所有App,利用ApplicationInfo的flag判断应用类型

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private PackageManager pm;
    private ListView mListView;
    private PkgAdapter mAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pm = getPackageManager();
        mListView = (ListView) findViewById(R.id.app_info);
        mAdapter = new PkgAdapter(this);
        mListView.setAdapter(mAdapter);
        findViewById(R.id.btn_all).setOnClickListener(this);
        findViewById(R.id.btn_third).setOnClickListener(this);
        findViewById(R.id.btn_system).setOnClickListener(this);
        findViewById(R.id.btn_sdcard).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        mAdapter.setData(getAppInfo(v.getId()));
        mAdapter.notifyDataSetChanged();
    }

    private List<PMAppInfo> getAppInfo(int flag) {
        List<ApplicationInfo> applicationInfoList = pm.getInstalledApplications(
                PackageManager.GET_UNINSTALLED_PACKAGES);
        List<PMAppInfo> pmAppInfoList = new ArrayList<>();
        switch (flag) {
            case R.id.btn_all:
                pmAppInfoList.clear();
                for (ApplicationInfo info : applicationInfoList) {
                    pmAppInfoList.add(makeAppInfo(info));
                }
                break;
            case R.id.btn_system:
                pmAppInfoList.clear();
                for (ApplicationInfo appInfo : applicationInfoList) {
                    if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {   //系统预装应用
                        pmAppInfoList.add(makeAppInfo(appInfo));
                    }
                }
                break;
            case R.id.btn_third:
                pmAppInfoList.clear();
                for (ApplicationInfo appInfo : applicationInfoList) {
                    if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {   //第三方应用
                        pmAppInfoList.add(makeAppInfo(appInfo));
                    } else if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {//系统预装应用升级后变成第三方应用
                        pmAppInfoList.add(makeAppInfo(appInfo));
                    }
                }
                break;
            case R.id.btn_sdcard:
                pmAppInfoList.clear();
                for (ApplicationInfo appInfo : applicationInfoList) {
                    if ((appInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { //安装在Sdcard的应用
                        pmAppInfoList.add(makeAppInfo(appInfo));
                    }
                }
                break;
        }
        return pmAppInfoList;
    }

    private PMAppInfo makeAppInfo(ApplicationInfo applicationInfo) {
        PMAppInfo appInfo = new PMAppInfo();
        appInfo.setAppLabel(applicationInfo.loadLabel(pm).toString());
        appInfo.setAppIcon(applicationInfo.loadIcon(pm));
        appInfo.setPkgName(applicationInfo.packageName);
        return appInfo;
    }
}

效果如下

在这里插入图片描述

ActivityManager

用于获取运行时的应用程序信息,如

  • ActivityManager.MemoryInfo:全局内存信息(可用内存、总内存、是否处于低内存)
  • Debug.MemoryInfo:进程下内存信息
  • RunningAppProcessInfo:运行时进程信息(进程名、pid、uid)
  • RunningServiceInfo:运行时服务信息(服务是否在后台执行)
private void getRunningProcessInfo() {
    ActivityManager mActivityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager.getRunningAppProcesses();
    for (int i = 0; i < appProcessList.size(); i++) {
        ActivityManager.RunningAppProcessInfo info = appProcessList.get(i);
        Log.d(TAG, "getRunningProcessInfo: pid = " + info.pid);
        Log.d(TAG, "getRunningProcessInfo: uid = " + info.uid);
        Log.d(TAG, "getRunningProcessInfo: processName = " + info.processName);
        Debug.MemoryInfo[] memoryInfo = mActivityManager.getProcessMemoryInfo(new int[]{info.uid});
        int memorySize = memoryInfo[0].getTotalPss();
        Log.d(TAG, "getRunningProcessInfo: memorySize = " + memorySize);
    }
}

如上打印

pid = 5593
uid = 10091
processName = com.demo.demo0
memorySize = 0

packages.xml

Apk安装、删除、升级等状态,会记录到/data/system/packages.xml中

permissions标签

定义系统中的所有权限,根据package属性分为2类

  • 为android的是系统定义权限
  • 为包名的是Apk定义权限

package标签

  • name:包名
  • codePath:安装路径,/system/app存放预装应用,/data/app存放第三方应用
  • userId:用户ID
  • version:版本号

perms标签

对应Apk的AndroidManifest中的<uses-permission>标签,记录权限信息

安全机制

  • 代码安全机制:代码混淆可使代码无法阅读,同时也可压缩代码,优化字节码
  • 权限控制:App使用系统受限资源时,都需要在AndroidMainifest声明权限,并接受系统检查
  • 数字证书:即App的签名,未签名的App无法安装,相同数字签名的App在升级时才会被认为是同一App
  • Linux内核安全机制:文件系统权限控制,如文件的rwx或selinux权限
  • Android虚拟机沙箱机制:沙箱可以让应用之间互相隔离,互不影响

Apk反编译

Apk本质是一个压缩文件,可通过解压缩软件获取里面的内容,如下

在这里插入图片描述

但解压后的文件无源码文件src,同时xml文件乱码,只能查看res目录下的图片资源

apktool

apktool 用于反编译apk中的xml文件,运行下面命令,会生成一个对应名字的文件夹,可正常查看里面的xml文件(多用于汉化)

在这里插入图片描述

利用参数b可重新打包,会生成build和dist文件夹,Apk位于dist文件夹内

在这里插入图片描述

dex2jar

dex2jar 用于将apk中的.dex文件反编译成.jar文件,将.dex放到根目录运行如下命令,生成对应名字的.jar文件

在这里插入图片描述

jd-gui

jd-gui 用于查看.jar文件,如下打开上面反编译的.jar文件,查看MainAcitivity,内容一模一样

在这里插入图片描述

Apk加密

ProGuard可对Apk进行混淆处理

  • 用无意义的字母来重命名类、字段、方法和属性
  • 此外还可以删除无用的类、字段、方法和属性、注释等以优化字节码

在Gradle Scripts / build.gradle(Module: app)文件中

  • minifyEnabled:是否启用ProGuard
  • proguardFiles:配置混淆文件,一个是系统默认混淆文件(位于SDK/tool/proguard/),一个是自定义混淆文件
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

CrashHandler

CrashHandler可在程序crash时获取异常信息

  • Thread.setDefaultUncaughtExceptionHandler()设置异常处理器,发生未捕获异常时,会回调uncaughtException()方法
  • 如下将异常信息输出到文件
public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static final String TAG = "CrashHandler";
    private static final boolean DEBUG = true;

    private static final String FILE_NAME = "crash";

    private static final String FILE_NAME_SUFFIX = ".trace";

    private String mPath;
    private Thread.UncaughtExceptionHandler mDefaultCrashHandler;
    private PackageInfo mPackageInfo;

    private static CrashHandler sInstance = new CrashHandler();

    private CrashHandler() {
    }

    public static CrashHandler getInstance() {
        return sInstance;
    }

    public void init(Context context) {
        mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
        mPath = context.getFilesDir().getPath();
        Log.d(TAG, "init: mPath = " + mPath);
        Context applicationContext = context.getApplicationContext();
        PackageManager packageManager = applicationContext.getPackageManager();
        try {
            mPackageInfo = packageManager.getPackageInfo(applicationContext.getPackageName(), PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void uncaughtException(@NonNull Thread thread, @NonNull Throwable throwable) {
        try {
            dumpExceptionToSDCard(throwable);
            uploadExceptionToServer();
        } catch (IOException e) {
            e.printStackTrace();
        }

        throwable.printStackTrace();

        //如果系统提供了默认的异常处理器,则交给系统去结束我们的程序,否则就由我们自己结束自己
        if (mDefaultCrashHandler != null) {
            mDefaultCrashHandler.uncaughtException(thread, throwable);
        } else {
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

    private void dumpExceptionToSDCard(Throwable throwable) throws IOException {
        File dir = new File(mPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        long current = System.currentTimeMillis();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date(current));
        //以当前时间创建log文件
        File file = new File(mPath,  FILE_NAME + time + FILE_NAME_SUFFIX);

        try {
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            //导出发生异常的时间
            pw.println(time);

            //导出手机信息
            dumpPhoneInfo(pw);

            pw.println();
            //导出异常的调用栈信息
            throwable.printStackTrace(pw);

            pw.close();
        } catch (Exception e) {
            Log.e(TAG, "dump crash info failed");
        }
    }

    private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException {
        //应用的版本名称和版本号
        pw.print("App Version: ");
        pw.print(mPackageInfo.versionName);
        pw.print('_');
        pw.println(mPackageInfo.versionCode);

        //android版本号
        pw.print("OS Version: ");
        pw.print(Build.VERSION.RELEASE);
        pw.print("_");
        pw.println(Build.VERSION.SDK_INT);

        //手机制造商
        pw.print("Vendor: ");
        pw.println(Build.MANUFACTURER);

        //手机型号
        pw.print("Model: ");
        pw.println(Build.MODEL);

        //cpu架构
        pw.print("CPU ABI: ");
        pw.println(Build.CPU_ABI);
    }


    private void uploadExceptionToServer() {

    }
}

在Application中初始化

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        CrashHandler.getInstance().init(this);
    }
}

MainActivity抛出异常

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button crash = findViewById(R.id.btn_crash);
        crash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                throw new RuntimeException("test CrashHandler");
            }
        });
    }
}

在/data/user/0/com.demo.demo0/files/会生成文件,打开后可以看到异常信息

在这里插入图片描述

Multidex

单个dex文件所包含的最大方法数为65536,超过后编译报错,可以通过配置Multidex,当方法数越界时,打包成多个dex文件

在build.gradle(app)中开启及添加依赖

defaultConfig {
    ......
    multiDexEnabled true
}

dependencies {
    .......
    implementation 'com.android.support:multidex:1.0.3'
}

需要在Applcation继承MultiDexApplication中初始化Multidex

public class App extends MultiDexApplication {
    @Override
    public void onCreate() {
        super.onCreate();
    }
}

或在attachBaseContext()中初始化

public class App extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

此前Apk只有一个dex文件

在这里插入图片描述

使用MultixDex后有两个dex文件

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值