samedi 25 septembre 2021

ADB Tips & Tricks: ADB commands that every power user should know about!

Android has a neat tool available to developers called the Android Debug Bridge, or ADB for short. It’s used in Android Studio and manually by Android developers to install and test apps on Android smartphones and emulated Android devices.

But there’s a lot ADB can do, and most of it isn’t only useful to developers. Even if you aren’t a developer and you’re reading this article, there’s a good chance you’ve probably used ADB once or twice to install an app or tweak a permission. But that isn’t all ADB can be used for. Keep reading for some tips and tricks for using ADB you might not have known about.

Note: You’ll need to have ADB up and running on your computer. Be sure to check out our article on how to install ADB if you need help.


Shell Access

If you’ve used ADB before, you may be used to running commands all in one line. But you can also use ADB to open a terminal shell on your device and run commands directly. And it’s easy!

In your terminal or command prompt window:

adb shell

You’ll then be greeted with a $ symbol where you can run commands directly on your device.

An ADB Shell


Listing Installed Apps through ADB

To see the installed apps on your device, you can use the following command:

adb shell pm list packages

This will return a list of the package names of the installed apps, with each one on its own line prepended with package:.

ADB Shell listing of installed packages

Options

There are also some options you can use to retrieve more specific lists.

  • -f will include the path to the base APK for each app, along with its package name.
  • -a will make sure all known non-APEX packages are returned.
  • -d will cause the command to only return disabled packages.
  • -e will cause the command to only return enabled packages.
  • -s will cause the command to only return system packages.
  • -3 will cause the command to only return third-party packages.
  • -i will include the installer package name for each package.
  • -U will include the package UID for each package.
  • -u will include uninstalled packages.
  • –show-versioncode will include the version code for each package.
  • –apex-only will only return APEX packages.
  • –uid <UID> will only show packages with the given UID.
  • –user <USER_ID> will only show packages belonging to the given user ID.

Installing & Uninstalling Apps through ADB

This is a relatively common use of ADB, but it’s worth mentioning anyway. Among other ways, you can also make use of ADB to install and uninstall Android apps to your Android device.

Installing an APK

If you have an APK on your computer, you can install it to your device with the following:

adb install -r someapk.apk

Remember to replace someapk.apk with the full path to the APK you want to install.

Options

There are a bunch of options for installing APKs through ADB.

  • The -r option allows ADB to install over an existing app (i.e., update). On Android Pie and later, you don’t have to specify this option.
  • The -R option, for Android Pie and later, will cause the install to fail if the app is already installed.
  • The -i option lets you specify an installer package name. This is what gets returned if Android wants to know what installed the APK.
  • The -t option allows an APK with android:testOnly=”true” in its manifest to be installed.
  • The -d option allows the specified APK to be a downgrade to an already-installed app. This only works if both versions of the app are debuggable.
  • The -g option for Android Marshmallow and later automatically grants all runtime permissions to the installed app.

That’s not all of them. If you want a full list, you can check out the built-in documentation.

Multiple APKs and Bundles

If you have a bunch of APKs you want to install at once, either from multiple apps, or because you’re installing an app bundle, you can use ADB’s install-multiple and install-multi-package features.

If all of your APKs are for one app, use install-multiple:

adb install-multiple apk1.apk apk2.apk ...

Otherwise, use install-multi-package:

adb install-multi-package app1.apk app2.apk ...

The options for these commands are similar to install, but with some limitations. Check out ADB’s built-in documentation for which options are available.

Uninstalling an App

To uninstall using ADB, you’ll need the package name of the app you want to uninstall. Check out the section for Listing Installed Apps if you haven’t already.

Once you have the package name, uninstalling is as simple as:

adb uninstall <packagename>

Note: You generally can’t uninstall system or preinstalled apps using this command. You may be able to disable them with ADB, however. Check out the section Disabling & Enabling almost any App for details.


Extracting APKs with ADB

There are plenty of reasons you might want to extract the APK(s) for an app. Maybe you want to back it up for future use, or maybe it’s no longer available online and you want to transfer it to a different device.

Extracting an app using ADB is pretty simple. First, you’ll want to find the package name of the app you want to extract. There are multiple ways to do this, but the easiest is usually to use your device’s Settings app to view the list of all installed apps, select the one you want, and scroll down until you find the package name or app ID.

Once you have the package name, run the following command:

adb shell pm path <packagename>

This command will return the path of all APKs for that package name.

An image showing the results of retrieving the APK paths for an installed package

You can then use the following command to pull each APK to your computer:

adb pull /path/to/apk.apk


Listing App Components

An app’s components are things like its Activities, BroadcastReceivers, Services, and so on. Sometimes it’s useful to know the names of these components in a specific app, especially if you want to launch hidden Activities or send a broadcast with specific data.

Unfortunately, ADB doesn’t have a very clean way of listing an app’s components. But it is possible. Run the following command:

adb shell dumpsys package <packagename>

A whole bunch of text will be returned.

  • Scroll until you find the Activity Resolver Table title to see the Activities.
  • Look under Receiver Resolver Table for BroadcastReceivers.
  • Check the Service Resolver Table for Services.
  • And so on.

Each component will show the action needed to launch it, the name of the component, and possibly some extra information.

Activity Resolver Table for a dumpsys package

Alternatively, if you want an easier way to see Activities, Services, and Receivers, you can use my Root Activity Launcher app. It will show you those components for each app, along with a bunch of other handy features.


Launching Activities, Services, and BroadcastReceivers

ADB can also be used to launch Activities, start Services, and notify BroadcastReceivers. You can even specify data URIs and Intent extras if needed.

To launch components, you’ll need the component name of what you want to launch. You can see how to get that from the Listing App Components section.

The command syntax for launching an Activity is something like this:

am start -a <action> -n <component>

The command syntax for starting a Service is something like this:

am startservice -a <action> -n <component>

The command syntax for notifying a BroadcastReceiver is something like this:

am broadcast -a <action> -n <component>

In most cases, for Activities and Services, you don’t need to explicitly specify an action. You’ll usually only need it if the component uses one other than android.intent.action.MAIN.

On top of the basic syntax, here’s how to specify more data to pass. In general, all data values should be enclosed in double quotes.

  • -d allows you to specify a data URI.
  • -e <key> <value> or –es <key> <value> allows you to specify a String extra.
  • –esn <key> allows you to specify a null String extra.
  • –ez <key> <value> is used to specify a boolean extra.
  • –ei <key> <value> is used to specify an integer extra.
  • –el <key> <value> is for specifying a long extra.
  • –ef <key> <value> will pass a float extra.
  • –eu <key> <value> passes a URI extra.
  • –ecn <key> <value> can be used to specify a component name extra.
  • –eia <key> <value1>,<value2>,… will pass the values as an Integer[] extra.
  • –eial <key> <value1>,<value2>,… will pass the values as a List<Integer>.
  • The same array and list arguments also work for longs, floats, and Strings. Just replace the i with the appropriate letter.
  • -f allows you to specify a flag.

There are even more behavior options you can use, so check out the build in documentation for details.


Disabling & Enabling almost any App

System apps in Android can’t be uninstalled, and a lot of them also can’t be disabled through Settings. While ADB won’t let you uninstall them, it may help you disable them.

First, make sure to get the package name of the app you want to disable. Then, try these commands. If one fails, try the next option.

  • pm disable <package>
    • To re-enable, use pm enable <package>
  • pm disable-user –user 0 <package>
    • To re-enable, use pm enable <package>
  • pm hide <package>
    • To re-enable, use pm unhide <package>
  • pm suspend <package>
    • To re-enable, use pm unsuspend <package>
  • pm uninstall -k –user 0 <package>
    • To re-enable, use pm install-existing <package>
    • Note: This one effectively uninstalls the application from your user profile. While the command to re-enable should work, there’s no guarantee it will. You may need to factory reset to restore the app.

If you’re using multiple user profiles on your device, make sure to replace 0 in the commands above with the actual user ID you have.


ADB is an incredibly powerful tool, and it can do so much more than just what’s above. The commands in this article are just a useful starting point. For more advanced usage, check out commands like cmd -l to see different services you might be able to interact with or ls -l /system/bin to see the different command executables available.

The post ADB Tips & Tricks: ADB commands that every power user should know about! appeared first on xda-developers.



from xda-developers https://ift.tt/2WcaRhz
via IFTTT

Aucun commentaire:

Enregistrer un commentaire