6

I have:

void android_main(struct android_app* state)

Is it possible to get command line arguments used to start the application from android_app structure? If not, is there any other mean to get them using C++ calls(I can't use Java code)?

4
  • 1
    Why the downvote? On every platform main function has argv/argc in its signature. When the application is started arguments can be passed from IDEs or other tools. Android is linux, it should support similar features. Commented Nov 25, 2013 at 15:53
  • There are no command line arguments for Android apps. The closest to command line arguments is the "Intent" object used to start an activity or service of your app.
    – tiguchi
    Commented Nov 25, 2013 at 15:54
  • @NobuGames Why VisualGDB has the options to pass additional params to "am start" then? And why downvote instead of just answering that Android apps don't have command line arguments? Commented Nov 25, 2013 at 15:55
  • Android apps still depend on Dalvik VM as runtime environment. A Java activity named "NativeActivity" will be started first that sets up and calls your native code main function and which also routes all activity life cycle events to your native code. Technically your C++ code is just a JNI library for a Java app, not a standalone command line program. The way Android passes around parameters is through intents. If at all you might be able to read additional dalvikvm command line parameters that are meant for the Java app.
    – tiguchi
    Commented Nov 25, 2013 at 16:12

1 Answer 1

8

Android application processes do not have application-unique command line arguments, because no exec() call is made to start them.

Normally, when a shell launches a program, it forks itself to create a new process and then in that process immediately calls exec() to replace the shell program image with the one being executed. The argc & argv variables are derived from the parameters passed to exec().

But on Android, for a variety of reasons including sharing code pages system wide, all normal app processes are specialized children of a process called zygote. No exec() call is made, instead after the fork the code implementing the application is simply loaded into the process, and it's userid demoted to that of the app. Because there is no exec() call to establish new command line arguments, the only ones which are available to the process are those used to start the original zygote process during the startup of the Android runtime. Those might include vm run mode options, but nothing about the specific app.

App processes, and the Activity or Service objects within, are started in response to Intents. As has been pointed out in comments, the Intent object has a number of parameters visible to the receiver, some of which play roles very much like command line arguments would in a more traditional system. Functionally, it is those fields of the Intent object where it would be useful to look.

And as Nobu Games points point in comments, a "Native Activity" is not functionally that different - it is still a regular VM-based app with a lot of critical Java code, it's just that the 3rd party developer only writes jni libraries, with the necessary java code already packaged with the system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.