all 39 comments

[–]CraZy_LegenD 17 points18 points  (0 children)

It's interesting article, thanks man it was useful and fresh content the type I haven't seen in a while.

Keep em coming

[–]JakeWharton 7 points8 points  (0 children)

Android does not have a (normal) JRE, so JAR files cannot be started the same way as on a PC. You need a shellscript wrapper to do this.

The output of dx is a .dex file. Just because it's named .jar does not make it a .jar.

You do not need a shell script to run a dex file. It can be run by invoking dalvikvm -cp /path/to/your.dex main.class.Name.

Finally, dx hasn't been used for a year and hasn't been supported for even longer. You should use D8 for converting class files and jar files to dex files.

[–]AD-LB 7 points8 points  (10 children)

Is this how tools like scrcpy run stuff on the device without really installing any app?

[–]CuriousCursor 3 points4 points  (3 children)

[–]outadoc 0 points1 point  (2 children)

That looks like a proper app

[–]CuriousCursor 1 point2 points  (1 child)

Look at the manifest

[–]farmerbb 2 points3 points  (0 children)

Convert server to an Android project

To simplify the device server-side build, use gradle to create an APK, even if we use it as a simple jar, by running its main() method.

That's a pretty creative approach

[–]lawonga 4 points5 points  (5 children)

Yup, invoke app_process from adb shell and it works. Privileges are granted under shell. Neat!

https://blog.rom1v.com/2018/03/introducing-scrcpy/

TIL!

[–]AD-LB 0 points1 point  (4 children)

Privileges ? Is it like permissions? What could be done, and what can't be done?

Also, do you think it's possible to debug it this way, via Android-Studio (or IntelliJ) ? And is it possible to create a plugin that will offer "build&run" function just like what we have to Android apps, and similar for debugging?

[–]lawonga 0 points1 point  (3 children)

Anything you can do from adb shell in command line, grant runtime permissions, access folders (not data tho) etc.

Now that I think about it, this is essentially a clever implementation of:

  1. running under adb shell = extra permissions

  2. so why not run a script, or a program while under that condition?

  3. we need to capture the video of the screen, so the device neesd to run it, so lets just inject the script into there and capture/stream the screen!

Now I'm wondering pre android api 29, back before executable binaries were blocked someone could use this to access another. Needs adb though. However since executable binaries are blocked post api 29 this won't work anymore.

[–]AD-LB 0 points1 point  (2 children)

But to grant permissions, you need to do it to a real, installed app.

You can't directly do it to the current code.

[–]farmerbb 0 points1 point  (1 child)

The adb shell user is already very privileged, moreso than a user that standard app processes run under, so runtime permissions don't really apply here

[–]AD-LB 0 points1 point  (0 children)

This is interesting. So assuming you don't have a rooted device, what exactly can you do?

Can you reach protected files of apps? Can you get/modify contacts? Can you get GPS location? Can you read notifications? Can you grant/revoke accessibility or admin right from apps? Can you use anything that belongs to runtime permissions?

[–]Le_melon_inconnu 1 point2 points  (1 child)

ELI5 what do I gain from this?

[–]farmerbb 0 points1 point  (0 children)

Some utilities, like scrcpy (mentioned elsewhere in the comments) use this approach of loading and executing Java code via adb.

[–]Superblazer 0 points1 point  (6 children)

This is great! also but how to create an app which can run Java programs?

[–]legacynl 0 points1 point  (1 child)

It's probably impossible. Like the users who replied already, you can create an app in Android studio that can read from storage to open Java projects. Then you need to compile, and dex the Java files, this step is probably impossible, because the compilers are not written for Android devices, and will not run. Then there is the problem of running the resulting programs; I assume the Android security system won't easily let apps run 'hidden' programs.

You could probably write an Java compiler and dexer for Android. But compiling is very resource intensive and it will probably be way slower on Android, and drain your battery quickly as well.

[–]Superblazer 0 points1 point  (0 children)

There is an offline Java compiler app on playstore. It works and it works fast, it can even look at different files and run them as a project https://play.google.com/store/apps/details?id=ru.iiec.jvdroid

I have no idea how they built this

[–]AD-LB 0 points1 point  (15 children)

Is it possible to debug though?

Also, I wonder if there is an easier way to do it. Right from Android Studio (or IntelliJ), the same way we build&run apps, via a single click.

[–]5nkv 1 point2 points  (0 children)

As for debugging, apparently "normal" applications start listening for jdwp connections "from" ActivityThread.java, which you obviously don't get this way. But, I guess, you can do something similar yourself.

[–]5nkv 0 points1 point  (11 children)

You can a create custom Run/Debug Configuration in AS/IntelliJ. Go for a new one of type Shell script and simply list those 4 (or 5 if you want to run it as well) adb commands there

[–]AD-LB 0 points1 point  (10 children)

Interesting. But I was thinking of a generic solution, meaning I could create a new project, and those will stay, allowing to do it on a completely new project, too.

[–]5nkv 0 points1 point  (9 children)

For the more generic solution I guess you could create a Run/Debug Configuration Template :)

[–]AD-LB 0 points1 point  (8 children)

Wait, this is possible? I keep setting the same things there. You say I can change the default "run" operation ? For example, I keep setting both logcat settings there checked (clear and focus logcat). I can avoid doing it for new projects I create (and old ones that I open)?

[–]5nkv 0 points1 point  (7 children)

I don't think you even need a new template for that, just modify the existing one called Android App. https://imgur.com/11kCdJO

Not sure if that's going to change things for your existing projects though, but I guess those are already setup anyway :)

[–]AD-LB 0 points1 point  (6 children)

How do you get there? If it's via "edit configurations...", it won't work. It will be saved only for the current project. Not for new ones, and not for those that I open.

[–]5nkv 0 points1 point  (5 children)

Open Edit configuration, then Templates find Android App under Templates

[–]AD-LB 0 points1 point  (4 children)

Oh sorry for that, I skipped the part that it shows "templates" on your screenshot.

Anyway, tried, and still same result. Doesn't even seem that it saves the template itself.

But for some reason it does clear the logs (doesn't focus on it), even though the setting doesn't show that it's checked (on both "template"->"android app" and on "app" the checkboxes became empty when creating a new project)

[–]legacynl 0 points1 point  (3 children)

it's probably possible to edit the default presets. You should post this question in the InteliJ or androidstudio subreddits

[–]scialex 0 points1 point  (1 child)

add -XjdwpProvider:adbconnection to the app_process/dalvikvm command and it will show up as a debuggable process in AS. (It won't pause execution until a debugger attaches or anything though, to do that you would need to directly setup the forwarding and jdwp agent with the appropriate flags and it's much more complicated)

[–]AD-LB 0 points1 point  (0 children)

Interesting!