0

How can I write a pure native program in Android which I can execute from command line? The examples I google are all have a Java Activity which just JNI to call some native functions.

For example, how can I write 'hello world' program in C which just print out 'hello world' when I execute it from 'adb shell'?

Thank you.

michael
  • 106,540
  • 116
  • 246
  • 346
  • 3
    Pretty much the same way you do it for any other Linux system... you use gcc (the NDK-provided one in this case) to compile and then link your application into a standard (for the platform the compiler targets) executable. In my (not recent; it's been over a year since I've used the NDK tools) experience, you have to add several command line options to locate / import libraries and header files. – mah Dec 12 '13 at 18:28
  • Any good reason why you want to do this? Android runs on dalvik virtual machine so you need bytecode(machine code) to run it on adb shell – hlim Dec 12 '13 at 18:30
  • 2
    While most of Android uses Dalvik, and interacting with the app framework essentially requires it, there is nothing stopping anyone from writing typical Linux shell commands. For example, Android 4.4 introduced the `screenrecord` command for making videos of the display. It does not use Dalvik at all. (see http://bigflake.com/screenrecord/) The only trick is, as @mah pointed out, that you have to get all the right cross-compiled includes and libs figured out. – fadden Dec 12 '13 at 18:56
  • If you're not going to develop a proper Android app (something that people actually can run on non-rooted Android devices), then why develop for Android in the first place? See the `native-activity` example that comes with the NDK for how to write apps using only native code. @hlim: Not really. Android consists of both native parts and Java parts. – Michael Dec 12 '13 at 18:57
  • 1
    @Michael perhaps the OP needs to port a command line tool that he intends to open within an Android application. Perhaps he's trying to implement a utility having no GUI for use on his own rooted device. There are likely other reasons one would want to do this, but your comment/question/rhetoric seems to judge that there's never a good reason. (I hope that's not your intended message because if so, you would be highly mistaken in your belief.) It's clear that the OP is asking about how to write a command line tool, which makes the native-activity example completely invalid. – mah Dec 12 '13 at 19:01
  • @mah: My impression was that the OP didn't want to learn the Android Java APIs and/or JNI, and was looking for a way to do Android development without them. It's certainly possible ([I wrote on the subject 4-5 years ago](http://jiggawatt.org/badc0de/android/index.html)), I just wanted to point out the limitations of going down this path to the OP. – Michael Dec 12 '13 at 19:12

1 Answers1

0

I'm assuming that you eventually want to be able to execute the program on-device; I can't post a comment to ask this as I don't have enough reputation yet, so feel free to post a comment to this answer if this is not the case.

Depending on how complex of a program you wish to write, there are a few methods you can try. If you want to write code that is several hundred lines or more, then you can add the NDK to your development environment if you have one, as others have mentioned.

If you want to write "hello world" or something equally simple, you can also code on-device...assuming you have an Android based device. There are several development environment options available both on and off the Google Play store.

Hope this helps.