One day I was overcome by a strange desire - to use all the power of my android smartphone in the console. A very convenient way for all sorts of utilitarian tasks. The console is the key to my story.
As such, the development for Android (if we are talking about the standard Android SDK, and not the NDK) does not imply console programming. Activations, services, intents - in general, not one dozen articles were written on this topic, but mine was about something else.
As you probably know (or maybe not), Android programs, although written in Java, must be converted to a completely different Java bytecode before execution. Because the Dalvik virtual machine, the heart of Android, is different than the Java SE VM. For this is a special utility in the Android SDK called dx (Dalvik eXecutable).
It works devilishly simple:
java -jar dx.jar --dex --output=dexed.jar java.jar
where java.jar is a java package with java-classes, and dexed.jar is a suitable java-package for uploading and launching on Android.
What to do if you really want to run a console jarik right "in the field"? The conclusion suggests itself - convert dx.jar using yourself, pour it onto the device, and use it right there. The idea, of course, is risky, but we will try.
java -jar dx.jar --dex --output=dx.apk dx.jar adb push dx.apk /sdcard/dx.apk adb push helloworld.jar /sdcard/helloworld.jar
')
here I will take a short pause to give some clarification.
Although the Android SDK does not imply the development of console programs, Dalvik runs and executes them quietly. Naturally, you should do this in some terminal program. Dalvik has one more trouble - he does not support the -jar option. Well, that is, the option itself is there, but it ignores it. At least for my Milestone 2 with Android 2.3.4, this is true. Therefore, the package with the program must be specified with the -cp key, and the main launch class with the main method must be specified manually.
adb shell cd /sdcard dalvikvm -cp dx.apk com.android.dx.command.Main --dex --output=helloworld.apk helloworld.jar dalvikvm -cp helloworld.apk helloworld.Main hello Hello, hello!
So, you can, having “dexed” dx, run java-programs directly on the device.
I hope this information was useful for you, habrauzer.
Happy New Year!