Support passing metadata-defined arguments into Android application main function 45/325545/2
authorRichard Huang <r.huang@samsung.com>
Wed, 11 Jun 2025 12:55:18 +0000 (13:55 +0100)
committerRichard Huang <r.huang@samsung.com>
Wed, 11 Jun 2025 13:07:30 +0000 (14:07 +0100)
Change-Id: I66e354206be8f90a1ef1bc21cd9a914b3774b25e

README.md
build/android/app/src/main/AndroidManifest.xml
build/android/app/src/main/cpp/main.cpp

index d42681b6ef9cbabe70f2df34ffc3d1eed3a665cf..d745504f15a2ff66fc209d7b81728f3717d3b057 100644 (file)
--- a/README.md
+++ b/README.md
@@ -131,6 +131,10 @@ A demo can be launched directly via the ADB command. For example, to launch the
 
          $ adb shell am start -n com.sec.dalidemo/.DaliDemoNativeActivity --es start "benchmark.example"
 
+Optional arguments can be added while launching the demo. For example:
+
+         $ adb shell 'am start -n com.sec.dalidemo/.DaliDemoNativeActivity --es start "benchmark.example" --es arguments "-r40 -c40"'
+
 ## 4. Building for MS Windows
 
 Third party dependencies are built using vcpkg. Instructions on how to install vcpkg can be found in the
index f841bf9a067b4bd0c18bd06f83681e953606478e..a7b516d656a86c423048b873847c122dab10a8ef 100644 (file)
@@ -25,6 +25,8 @@
           android:value="native-activity" />
       <meta-data android:name="start"
           android:value="blocks.example" />
+      <meta-data android:name="arguments"
+          android:value="" />
       <intent-filter>
         <action android:name="android.intent.action.RUN" />
       </intent-filter>
@@ -44,6 +46,8 @@
                  android:value="native-activity" />
       <meta-data android:name="start"
                  android:value="dali-demo" />
+      <meta-data android:name="arguments"
+                 android:value="" />
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
@@ -63,6 +67,8 @@
           android:value="native-activity" />
       <meta-data android:name="start"
           android:value="dali-examples" />
+      <meta-data android:name="arguments"
+          android:value="" />
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
@@ -82,6 +88,8 @@
           android:value="native-activity" />
       <meta-data android:name="start"
           android:value="dali-tests" />
+      <meta-data android:name="arguments"
+          android:value="" />
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
index 0e1ac1452a1175508a63f38c70a6c71252bd9b24..18bd25325b83c25b18fc0e0042e995e34ffb56f0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2025 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -216,11 +216,40 @@ void android_main(struct android_app* state)
 
   dlerror(); /* Clear any existing error */
 
+  std::string argumentsParam = nativeActivity.GetIntentStringExtra("arguments");
+  if(argumentsParam.empty())
+  {
+    argumentsParam = nativeActivity.GetMetaData("arguments");
+  }
+
   int (*main)(int, char**) = (int (*)(int, char**))dlsym(handle, "main");
   LOGV("lib=%s handle=%p main=%p", libpath.c_str(), handle, main);
   if(main)
   {
-    status = main(0, nullptr);
+    if(!argumentsParam.empty())
+    {
+      // Tokenize `arguments` string into argc and argv
+      std::vector<std::string> argTokens;
+      std::istringstream       iss(argumentsParam);
+      std::string              token;
+      while(iss >> token)
+      {
+        argTokens.push_back(token);
+      }
+
+      // Convert to char** argv
+      std::vector<char*> argv;
+      for(std::string& arg : argTokens)
+      {
+        argv.push_back(const_cast<char*>(arg.c_str()));
+      }
+
+      status = main(static_cast<int>(argv.size()), argv.empty() ? nullptr : argv.data());
+    }
+    else
+    {
+      status = main(0, nullptr);
+    }
   }
   else
   {