'if Unity will include the chunk of code in apk if I write it in "UNITY_EDITOR"

Example: If I write following code in a Script file.

#if UNITY_EDITOR
  Debug.Log( "I'll print in Editor only.." );
#endif

My question is that when creating an apk build will this chuck of code will be added into apk build although it will not run once installed on Android, or unity will not include this code in the apk build.

I'm expecting one of following answer.

  1. No, unity will not include this code in APK build.
  2. Yes, unity will include it in apk but it will never run there and will just sit in somewhere in apk forever uselessly.

P.S

The question is about including not compiling. I couldn't get a clear answer elsewhere.



Solution 1:[1]

The code inside the #if-statement will only be compiled on the platform specified.

Unity includes a feature called Platform Dependent Compilation. This consists of some preprocessor directives that let you partition your scripts to compile and execute a section of code exclusively for one of the supported platforms.

https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

Solution 2:[2]

No, Unity will not include code in #if UNITY_EDITOR in builds.

You can test this out with the following example:

void Start ()
{
    #if UNITY_EDITOR
    Debug.Log("Unity Editor");
    #else
    // note that this block of code is not included while working in the Editor,
    // but it will be included when building to Android
    // (or any other build target outside the editor)
    Debug.Log("Not Unity Editor");
    #endif

    #if UNITY_ANDROID
    Debug.Log("Android");
    #endif

    #if UNITY_STANDALONE_WIN
    Debug.Log("Stand Alone Windows");
    // Including a garbage line of code below to show
    // that code really isn't included with the build
    // when the build target doesn't match, e.g. set to Android
    fkldjsalfjdasfkldjsafsdk;
    #endif
}

If your build target is set to Android, your application should build despite the compilation error because the UNITY_STANDALONE_WIN code is completely removed (and your IDE will likely gray out the code block). Once you change your build target to Windows, the code will fail to compile.


(Personally, I prefer to use Application.isEditor over #if UNITY_EDITOR macro as a habit whenever possible because of this, as using #if UNITY_EDITOR with #else can cause you to break the build without realizing until later for any code that fails to compile in the #else block. I am usually more concerned about this than extra useless code being included in my build. Of course, when using UnityEditor classes, using #if UNITY_EDITOR is unavoidable.)

Solution 3:[3]

No, unity will not include this code in APK build.

Solution 4:[4]

Unity never includes codes wrapped under the UNITY_EDITOR preprocessor directive.

Unity supports preprocessor directives which help to selectively include or exclude code from compilation or build.

#if UNITY_EDITOR
  Debug.Log( "I'll print in Editor only.." );
#endif

No, unity will not include this code in APK build.

For more: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fredrik Widerberg
Solution 2
Solution 3 Wojtek Wencel
Solution 4 Codemaker