I have tried to execute logcat -b radio programmatically, but failed. The command can be executed successfully from a PC terminal while the device is connected.
So I want to connect adbd service directly on the phone. Is there any method to connect adbd? Perhaps with a socket or usb driver? Who can give some advice on this?
The READ_LOGS permission control is changed since Android 4.1. Normal application cannot read the logs generated by other process (with different UID).
However, you can still get this permission manually by execute
"pm grant com.yourpackage.name android.permission.READ_LOGS"
on Adb shell. You might need to reboot your device after this.
Back to you question, you cannot connect to ADB daemon on your device with your application running on the same one. Another workaround for your application to read the logs is to write a daemon application to start and be daemonized on ADB shell, which will communicate with your application via IPC to get the log for you.
Related
Problem
My App.apk is currently set as a custom Launcher and also set as a Device Owner to have access to the Lock Task Mode.
Command to be a Device Owner
adb shell dpm set-device-owner com.package.name/.MyDeviceAdminReceiver
Previous to that I could just press Run App and the old app.apk process would have been killed and the new one would appear.
Now I have to manually reinstall it by pressing build and then using adb
adb install -t -r <path/app-debug.apk>
I have already tried the attaching to debugger solution provided here which doesn't work.
According to Googles Issue Tracker this is the reason why it happens.
It looks like we don't recognize when an app features BIND_DEVICE_ADMIN permission which protects them from being force-stoped.
My goal
I don't want to manually write adb install I want to achieve the same fluent workflow without this workaround stuff. Pressing Run App should have the same effect as before.
Im pretty confident there is a custom run configuration that can help with this problem but this is my first android project so any ideas or help would be appreciated.
What I have tried
I tried to setup a before launch shell script on the Run/Debug configuration but all adb commands are not working to stop my app.
adb shell pm disable com.package.name
Security exception: Cannot disable a protected package: com.package.name
adb uninstall com.package.name
Failure [DELETE_FAILED_DEVICE_POLICY_MANAGER]
My goal is to write an app which runs on a handset and lets the user choose from a list of APK's, then installs the one selected to an Android Things device on the same network.
We can actually forget Android Things because the same code would work between two handsets, it's just that there it would be pointless because the target can just receive the APK in many other ways such as an attachment, BT etc. AT devices only have ADB for this, apart from the recently announced Android Things Console which is overkill for regular local development. I'm therefore looking to replicate the sequence a PC would go through to install it, i.e. "adb connect, adb install ..." etc but from the handset itself. We can assume all devices involved are rooted.
It seems to me this means my app has to issue these commands as a process, but I'm having a hard time getting it fully working. When I issue "adb help" I get back the help message, and when I issue "adb reboot" the device reboots, so I think I'm on the right lines. It's when I try anything apart from those I get nothing back - the example of "adb shell ping -c 1 192.168.62.40" fails, but is OK from ADB on the PC. Also, it's very curious "adb version" fails which again is OK from the terminal.
At first I thought the handsets might only have an "adb lite" installed which has just enough to get them to work as an adb target, but using a shell from a PC showed that's not the case.
Here's the code I'm trying:
Process process = Runtime.getRuntime().exec("su adb help");
//Process process = Runtime.getRuntime().exec("su adb reboot");
//Process process = Runtime.getRuntime().exec("su adb version");
//Process process = Runtime.getRuntime().exec("su adb shell ping -c 1 192.168.62.40");
process.waitFor();
Log.d("PROCESS", "Status: "+process.exitValue());
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder everything = new StringBuilder();
String line;
while( (line = bufferedReader.readLine()) != null) {
everything.append(line);
}
Log.d("PROCESS", "Process output: "+everything.toString());
Toast.makeText(MainActivity.this, everything.toString(), Toast.LENGTH_SHORT).show();
The problem is that the adb client (which you use to communicate to the ADB server which communicates to and from a device on a development machine) is not packaged on an Android device. The adbd daemon, however, (used to communicate between the adb client and the device) can and will be found on an Android system.
There are a few options to help use the adb client on an Android device:
Try and build the adb client for an Android device from the adb sources on GitHub.
Try and make a makeshift adb "client". Since the adb client and adbd daemon communicate via USB or TCP, you could try emulating the communication protocols to open read/write streams on the device. More on the client-daemon communication protocols here. This library that I am working on might help you: eviltak/adb-nmap
The quick and dirty option is to build the adb client from sources and push to the device. That is, if you can get it to build on an Android device.
The second option is probably the most time consuming, but can be the most "clean" choice. You will, however, have to emulate the adb authentication system, properly handle streams and so on, which can be cumbersome. The source will help you.
In either case, the GitHub adb source directory should have everything you need for the long road ahead.
Four years later...
I was looking for the same situation.
Adb requires a network connection as it is a client-server connection where the Android device is the server and the computer the client.
To work around this, you need Termux (an Android terminal with package manager) and a local VPN such as Netguard. Root access is not required.
In Termux you can install a native Android version of adb which is step 1 by apt install android-tools.
But adb should make a network connection to the device itself. Then a VPN can be useful. VPNs operate locally, where the device acts as a VPN server such as for adblocking like Netguard. Then your device gets another network with another IP address, e.g. Netguard makes 10.1.10.1 which I use as an example (your VPN might issue a different address).
Run adb connect 10.1.10.1
The device might prompt (only once) with 'allow connections from computer ..blabla...' and consent this.
Then run adb connect 10.1.10.1:5555 and you are connected. Now adb commands can be run from the Termux command shell regardless on the device itself or via an SSH connection.
Note: Plugging in USB is still needed after reboot, unless you have Android 11+.
What is the default built in shell in Android phones and can I run runtime.exec Java method without installing a shell ?
You can call runtime.exec(String) in Android but it won't get you very far, because the runtime doesn't really offer commands to consume. However, if you've got a rooted phone with busybox and su installed, it is possible to call those embedded commands. It's even possible to create a superuser session by executing the su binary and consuming the streams of it.
I have an application where I create a process and call the dumpsys telephony.registry command to get information about the mobile network status.
String[] cmds={"dumpsys telephony.registry"};
Process p = Runtime.getRuntime().exec(cmds [0]+"\n");
and then after that I parse the result of the command. For "ls" or other commands it works fine. For dupmsys I get Permission Denial: can't dump telephony.registry from pid-953, uid=10090. I get the same error results for dumpsys power or other dumpsys commands.
I have set DUMP permissions android.permission.DUMP in the android Manifest like suggested here
I think that I am doing this right since Android offers this feature here
I have also done the step described here to force eclipse to allow me to give my application DUMP permission in the manifest.
When I execute the dumpsys command I always get the same result
Permission Denial: can't dump telephony.registry from pid-953, uid=10090
Am I doing something wrong? Why does android OS still deny me access to the dump service ?
PS I have set min API 8 and I am testing the application on device running (ICS)
API 15
Why does android OS still deny me access to the dump service ?
Because that permission is flagged as android:protectionLevel="signature|system|development" (or signatureOrSystem using the old syntax) on Android 2.3+, and therefore cannot be held by ordinary Android SDK applications.
There's another (hacky) way to access dumpsys without rooting your device - through adb shell.
This will require allowing USB debugging, and finding the port of the adb service.
Enable USB debugging on your device. This option is found under Settings -> Developer Options.
Connect your device to a PC, and run the following command from the PC's shell/command line:
adb tcpip 12345. Then, from your devices shell, issue the command adb connect localhost:12345 from your application. You can now disconnect the device from USB.
Alternatively, you can scan the ports on your device one by one without USB connection, using adb connect localhost:<portnum> and find the port adb service is listening to.
Authorize USB debugging from the pop up confirmation dialog, if prompted. Check the "always" checkbox to do not require this step again.
Now, when you have access to the adb service, use adb shell dumpsys ... from your application code to get whatever service dump you need.
NOTE: You don't need the DUMP permission for that to work.
The android dev team decided to stop granting these permissions to third-party apps. Only system apps can now get them.
more details:https://code.google.com/p/acra/issues/detail?id=100
It reports private values of core Android services that you would never be able to typically obtain. Official document says "Not for use by third-party applications".
Add permission on your manifest "android.permission.DUMP". I have not tried it yet but it shows on adb shell that it is missing that permission
I keep reading tutorials where I'm supposed to enter something into the ADB command line and that it's in my Android sdk/platform-tools. So I find it, click on it, and a black screen comes up for about 2 seconds and while it's up, it scrolls through a bunch of text. So how am I supposed to use this "adb"?
It is called the Android Debug Bridge, and the Android Developers Site documentation does a better job of explaining it than I can:
http://developer.android.com/guide/developing/tools/adb.html
If you are looking for the adb command line, navigate to <sdk>/platform-tools/ and run
adb.exe shell
from the command line.
Pretty sure that is well documented since day 1 on the Android Debug Bridge
Android Debug Bridge (adb) is a versatile command line tool that lets
you communicate with an emulator instance or connected Android-powered
device. It is a client-server program that includes three components:
A client, which runs on your development machine. You can invoke a
client from a shell by issuing an adb command. Other Android tools
such as the ADT plugin and DDMS also create adb clients. A server,
which runs as a background process on your development machine. The
server manages communication between the client and the adb daemon
running on an emulator or device. A daemon, which runs as a background
process on each emulator or device instance.
So plain old English, ADB can be found on %ANDROID_HOME%/platform-toos/, and it's this magical command line that allows you to comunicate with your mobile device, either a physical or a Virtual device (AVD), so whenever you deploy you are passing the application through the device thanks to the ADB on a specific client port on your computer to the daemon port on the device.
Interesting things you can do with it?
Logcat: ./adb logcat allows you to see the log trace of each proces.
Install: ./adb install allows you to install apk to the device.
Killing:./adb kill-sever
Starting:./adb stat-server
Enter SQLite3: adb -s your_device shell
Use the monkey: adb shell monkey -v -p your.app.package 500 to
generate random events
And a lot more! Read the documentation it's beatiful and self-explanatory.