Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android native application command line arguments

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)?

like image 804
Mircea Ispas Avatar asked Nov 25 '13 15:11

Mircea Ispas


1 Answers

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.

like image 141
Chris Stratton Avatar answered Sep 23 '22 14:09

Chris Stratton