Is there a preprocessor directive or an editor variable that can be used to determine whether you’re currently building an App Bundle (.aab) or an APK file (.apk) ?
The reason I’m asking, is that for AdMob, you can register your device as a test device when running the app locally, but they suggest you remove the test device IDs when rolling out the app for release. My current workflow is building APKs for testing and building App Bundles for publishing. However, I keep forgetting to add/remove my test IDs when switching between the two.
How would I use this variable? Since it’s available from the UnityEditor package, it doesn’t work properly in the actual builds.
bool includeTestDeviceIds = false;
#if UNITY_EDITOR
includeTestDeviceIds = !EditorUserBuildSettings.buildAppBundle;
#endif
if (includeTestDeviceIds)
{
config = new RequestConfiguration
.Builder()
.SetTestDeviceIds(deviceIds)
.build();
}
else
{
config = new RequestConfiguration
.Builder()
.build();
}
This code would not create a request configuration with test IDs. Trying to use EditorUserBuildSettings.buildAppBundle outside of the UNITY_EDITOR preprocessor would cause compilation errors.
Same issue unfortunately, I’m stuck in the UnityEditor namespace with it, and I’m not sure how I could use those variables during editor-time to set one or the other.
Actually, for some reason I never thought to just generate the code I would use in a partial, .designer.cs
style, and when doing that, EditorUserBuildSettings.buildAppBundle
would actually be available.
I’m going to give that a go real quick, and I’ll let you know if it works out.
EDIT:
Seems to work!
I’ve got the following code samples:
public static partial class DeviceIdManager
{
public static List<string> deviceIds;
}
// Generate second part of above partial class
public class BuildPreProcessHook : IPreprocessBuildWithReport
{
public int callbackOrder { get; }
public void OnPreprocessBuild(BuildReport report)
{
Debug.Log("Pre process build task executing");
StringBuilder sb = new StringBuilder();
sb.AppendLine("public static partial class DeviceIdManager");
sb.AppendLine("{");
if (!EditorUserBuildSettings.buildAppBundle)
{
Debug.Log("Including device IDs");
sb.AppendLine(" static DeviceIdManager() {");
sb.AppendLine(" deviceIds.Add(\"MyDeviceIdHere\");");
sb.AppendLine(" }");
}
else
{
Debug.Log("Excluding Device IDs");
}
sb.AppendLine("}");
// Writing to a .cs file here that's different from DeviceIdManager.cs
}
}
// Generating RequestConfiguration
RequestConfiguration config;
if (DeviceIdManager.deviceIds.Count > 0)
{
config = new RequestConfiguration
.Builder()
.SetTestDeviceIds(DeviceIdManager.deviceIds)
.build();
}
else
{
config = new RequestConfiguration
.Builder()
.build();
}
This works fine as far as I can tell; before the build starts, the second partial class is generated, and includes the test device IDs when not building an app bundle. Haven’t tested the builds themselves yet but it looks promising.
Edit 2: Yup, all good when I actually initialize the list, fix import issues and other things! Thank you for your help!