Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Preprocessor / variable for building App Bundle vs building APK

Discussion in 'Scripting' started by NickVst, Aug 17, 2020.

  1. NickVst

    NickVst

    Joined:
    Feb 17, 2017
    Posts:
    25
    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.
     
  2. Seraphim-Whiteless

    Seraphim-Whiteless

    Joined:
    Jun 23, 2014
    Posts:
    197
  3. NickVst

    NickVst

    Joined:
    Feb 17, 2017
    Posts:
    25
    How would I use this variable? Since it's available from the UnityEditor package, it doesn't work properly in the actual builds.

    Code (CSharp):
    1.         bool includeTestDeviceIds = false;
    2. #if UNITY_EDITOR
    3.         includeTestDeviceIds = !EditorUserBuildSettings.buildAppBundle;
    4. #endif
    5.  
    6.         if (includeTestDeviceIds)
    7.         {
    8.             config = new RequestConfiguration
    9.                     .Builder()
    10.                 .SetTestDeviceIds(deviceIds)
    11.                 .build();
    12.         }
    13.         else
    14.         {
    15.             config = new RequestConfiguration
    16.                     .Builder()
    17.                 .build();
    18.         }
    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.
     
  4. Seraphim-Whiteless

    Seraphim-Whiteless

    Joined:
    Jun 23, 2014
    Posts:
    197
  5. NickVst

    NickVst

    Joined:
    Feb 17, 2017
    Posts:
    25
    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:

    Code (CSharp):
    1. public static partial class DeviceIdManager
    2. {
    3.         public static List<string> deviceIds;
    4. }
    Code (CSharp):
    1.     // Generate second part of above partial class
    2.     public class BuildPreProcessHook : IPreprocessBuildWithReport
    3.     {
    4.         public int callbackOrder { get; }
    5.  
    6.         public void OnPreprocessBuild(BuildReport report)
    7.         {
    8.             Debug.Log("Pre process build task executing");
    9.             StringBuilder sb = new StringBuilder();
    10.             sb.AppendLine("public static partial class DeviceIdManager");
    11.             sb.AppendLine("{");
    12.  
    13.             if (!EditorUserBuildSettings.buildAppBundle)
    14.             {
    15.                 Debug.Log("Including device IDs");
    16.              
    17.                 sb.AppendLine("    static DeviceIdManager() {");
    18.                 sb.AppendLine("        deviceIds.Add(\"MyDeviceIdHere\");");
    19.                 sb.AppendLine("    }");
    20.             }
    21.             else
    22.             {
    23.                 Debug.Log("Excluding Device IDs");
    24.             }
    25.  
    26.             sb.AppendLine("}");
    27.  
    28. // Writing to a .cs file here that's different from DeviceIdManager.cs
    29.         }
    30.     }

    Code (CSharp):
    1.     // Generating RequestConfiguration
    2.         RequestConfiguration config;
    3.  
    4.         if (DeviceIdManager.deviceIds.Count > 0)
    5.         {
    6.             config = new RequestConfiguration
    7.                     .Builder()
    8.                 .SetTestDeviceIds(DeviceIdManager.deviceIds)
    9.                 .build();
    10.         }
    11.         else
    12.         {
    13.             config = new RequestConfiguration
    14.                     .Builder()
    15.                 .build();
    16.         }
    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!
     
    Last edited: Aug 17, 2020