If you want to dynamically set the android:value of the com.google.android.gms.ads.APPLICATION_ID in your AndroidManifest.xml based on the value you obtain from AdHelper.AppId in your Flutter code, you can follow these steps:

  1. Fetch the Ad ID Dynamically:

    In your Flutter code, retrieve the Ad ID using the AdHelper.AppId method. Make sure to await the result:

    dart
    String appid = await AdHelper.AppId; // Fetch Ad ID dynamically
  2. Use a Placeholder in AndroidManifest.xml:

    In your AndroidManifest.xml file, use a placeholder for the android:value attribute of the com.google.android.gms.ads.APPLICATION_ID. You can use any placeholder, for example, @string/app_id_placeholder:

    xml
    <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="@string/app_id_placeholder" />
  3. Define the Placeholder in strings.xml:

    Open your android/app/src/main/res/values/strings.xml file and define the placeholder with a default value (it can be any valid string value):

    xml
    <resources> <string name="app_id_placeholder">default_value</string> </resources>
  4. Update the Placeholder Dynamically:

    In your Dart code, after fetching the Ad ID, update the placeholder value in strings.xml using platform-specific code (using platform channels). Create a method in your Android code (Kotlin) to update the string resource:

    In your MainActivity.kt file:

    kotlin
    import android.content.Context import android.content.res.Resources import io.flutter.embedding.android.FlutterActivity import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel class MainActivity : FlutterActivity() { private val CHANNEL = "com.example/app_config" override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> if (call.method == "updateAppId") { val appId = call.argument<String>("appId") ?: "default_value" updateStringResource(appId) result.success(null) } else { result.notImplemented() } } } private fun updateStringResource(appId: String) { val res: Resources = resources val packageName: String = packageName val resourceId: Int = res.getIdentifier("app_id_placeholder", "string", packageName) if (resourceId != 0) { res.getString(resourceId) val resEntryName: String = res.getResourceEntryName(resourceId) val resTypeName: String = res.getResourceTypeName(resourceId) val resPackageName: String = res.getResourcePackageName(resourceId) val appResources = resources val resId = appResources.getIdentifier(resEntryName, resTypeName, resPackageName) val appStringResourceField = appResources.javaClass.getDeclaredField(resEntryName) appStringResourceField.isAccessible = true appStringResourceField.set(appResources, appId) } } }

    In the code above, we create a method updateStringResource that takes the appId as an argument and updates the app_id_placeholder string resource with this value.

  5. Invoke the Platform Method:

    In your Flutter code, after fetching the Ad ID, invoke the platform method to update the placeholder with the dynamically fetched value:

    dart
    // After fetching the Ad ID String appid = await AdHelper.AppId; // Fetch Ad ID dynamically // Update the placeholder in AndroidManifest.xml const platform = MethodChannel('com.example/app_config'); await platform.invokeMethod('updateAppId', {'appId': appid});

With these changes, the android:value of the com.google.android.gms.ads.APPLICATION_ID in your AndroidManifest.xml will be updated dynamically based on the value fetched from AdHelper.AppId in your Flutter code.


It appears you would like to make changes in Java to update the string resource in Android. Here's the Java version of the code for updating the string resource dynamically based on the value fetched from Flutter:

java
import android.content.Context; import android.content.res.Resources; import android.os.Bundle; import io.flutter.embedding.android.FlutterActivity; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; public class MainActivity extends FlutterActivity { private static final String CHANNEL = "com.example/app_config"; @Override public void configureFlutterEngine(FlutterEngine flutterEngine) { super.configureFlutterEngine(flutterEngine); new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL) .setMethodCallHandler( (call, result) -> { if (call.method.equals("updateAppId")) { String appId = call.argument("appId"); if (appId != null) { updateStringResource(appId); } result.success(null); } else { result.notImplemented(); } } ); } private void updateStringResource(String appId) { Resources appResources = getResources(); int resourceId = appResources.getIdentifier("app_id_placeholder", "string", getPackageName()); if (resourceId != 0) { String resEntryName = appResources.getResourceEntryName(resourceId); String resPackageName = appResources.getResourcePackageName(resourceId); try { Class<?> resClass = R.string.class; int resId = appResources.getIdentifier(resEntryName, "string", resPackageName); java.lang.reflect.Field field = resClass.getDeclaredField(resEntryName); field.set(appResources, appId); } catch (Exception e) { e.printStackTrace(); } } } }

This Java code is equivalent to the previously provided Kotlin code for updating the string resource. It allows you to dynamically update the string resource in the strings.xml file based on the value obtained from your Flutter code using a platform channel.