Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Cant build my unity project as .apk

Discussion in 'Android' started by Kernkraft, Feb 29, 2024.

  1. Kernkraft

    Kernkraft

    Joined:
    Oct 26, 2020
    Posts:
    32
    Hello Community,
    since days I have the problem to build my Unity project to an .apk file. When I try to build an empty project it works fine. But the buildsettings are the same.
    Unity Version: 2022.3.20f1 LTS
    Here are the ErrorMessages I receive:
    Error 1:
    -----------------------------------------------
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

    FAILURE: Build failed with an exception.

    * Where:
    Build file 'C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\mobilenotifications.androidlib\build.gradle' line: 3

    * What went wrong:
    Could not compile build file 'C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\mobilenotifications.androidlib\build.gradle'.
    > startup failed:
    build file 'C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\mobilenotifications.androidlib\build.gradle': 3: only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed

    See https://docs.gradle.org/7.2/userguide/plugins.html#sec:plugins_block for information on the plugins {} block

    @ line 3, column 1.
    plugins {
    ^

    1 error


    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

    * Get more help at https://help.gradle.org

    BUILD FAILED in 2s

    UnityEngine.GUIUtility:processEvent (int,intptr,bool&)
    -----------------------------------------------

    Error2:
    -----------------------------------------------
    CommandInvokationFailure: Gradle build failed.
    C:\Program Files\Unity\Hub\Editor\2022.3.20f1\Editor\Data\PlaybackEngines\AndroidPlayer\OpenJDK\bin\java.exe -classpath "C:\Program Files\Unity\Hub\Editor\2022.3.20f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-7.2.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"

    Environment Variables:
    SHIM_MCCOMPAT = 0x810000001
    PROCESSOR_ARCHITECTURE = AMD64
    AMPLITUDE_API_KEY_YELLOWBUCKET_DEV = 48835b4d1f5e342aefe5f016324d296a
    BURST_ANDROID_MIN_API_LEVEL = 22
    DriverData = C:\Windows\System32\Drivers\DriverData
    USERDOMAIN = Comp
    CommonProgramFiles(x86) = C:\Program Files (x86)\Common Files
    COMPUTERNAME = FFNB
    DEBUG_ENV_VAR = Comp
    LOGONSERVER = \\Comp
    AMPLITUDE_TOKEN_GREENBUCKET_PROD = yyzPzaIJDwrQcTNuOZsm0bhKb67WyyhK
    PUBLIC = C:\Users\Public
    ProgramData = C:\ProgramData
    SESSIONNAME = Console
    AMPLITUDE_TOKEN_YELLOWBUCKET_PROD = Ph-837Q6GNadAkyc2MRkxVcpcjlITT4d
    NVIDIAWHITELISTED = 0x01
    APPDATA = C:\Users\Comp\AppData\Roaming
    SystemRoot = C:\Windows
    windir = C:\Windows
    AMPLITUDE_API_KEY_REDBUCKET_DEV = 46b8b65825e7632fc443504b72e05fd0
    ProgramFiles(x86) = C:\Program Files (x86)
    ProgramW6432 = C:\Program Files
    OS = Windows_NT
    ANDROID_NDK_ROOT = C:\Program Files\Unity\Hub\Editor\2022.3.20f1\Editor\Data\PlaybackEngines\AndroidPlayer\NDK
    PROCESSOR_IDENTIFIER = Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
    HEAP_CLIENT_TOKEN_STAGING = 3786794399
    HOMEPATH = \Users\Comp
    FPS_BROWSER_USER_PROFILE_STRING = Default
    AMPLITUDE_TOKEN_REDBUCKET_DEV = U4PSN77ppuYV9ODIvvdcgwbu9R6IHGqa
    CHROME_CRASHPAD_PIPE_NAME = \\.\pipe\crashpad_8776_ZGOXCGFGMGZKZEVB
    PROCESSOR_LEVEL = 6
    EFC_8352 = 1
    JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF-8
    CommonProgramFiles = C:\Program Files\Common Files
    GOPATH = C:\Users\Comp\go
    JAVA_HOME = C:\Program Files\Unity\Hub\Editor\2022.3.20f1\Editor\Data\PlaybackEngines\AndroidPlayer\OpenJDK
    USERDOMAIN_ROAMINGPROFILE = Comp
    TEMP = C:\Users\Comp\AppData\Local\Temp
    CommonProgramW6432 = C:\Program Files\Common Files
    AMPLITUDE_TOKEN_YELLOWBUCKET_DEV = wKi8foVigDditGrrTmZewRFWsEbX9Y0YO
    AMPLITUDE_API_KEY_GREENBUCKET_DEV = 3ded2a762169854391a425f688f760c8
    AMPLITUDE_API_KEY_REDBUCKET_PROD = ca12bb461ea9999bdf5df4ded7d52994
    HOMEDRIVE = C:
    AMPLITUDE_API_KEY_STAGING = 83ee04cv56ere4fcc7dd527c4c6f1e21
    USERNAME = Comp
    OneDrive = C:\Users\Comp\OneDrive
    AMPLITUDE_API_KEY_YELLOWBUCKET_PROD = e03a9bbezuzc38a88cdb878d24c65b46
    PATHEXT = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    AMPLITUDE_TOKEN_REDBUCKET_PROD = QqyoFpcWui45E17p80_46CF6-sNGRGbHo
    NUMBER_OF_PROCESSORS = 12
    AMPLITUDE_TOKEN_GREENBUCKET_DEV = 84H2gMm_FJzhr3mBaX09OIjVukmCM7d9
    AMPLITUDE_DEPLOYMENT_KEY_RED_BUCKET_DEV = client-wbjHYrmeSR87GmwO89tzrpf7sUySOIKHm
    PSModulePath = C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules
    AMPLITUDE_API_KEY_GREENBUCKET_PROD = c86fea37a9e1d3443fftsb96e15b111c7c
    ComSpec = C:\Windows\system32\cmd.exe
    ORIGINAL_XDG_CURRENT_DESKTOP = undefined
    PROCESSOR_REVISION = 9e0a
    AMPLITUDE_DEPLOYMENT_KEY_RED_BUCKET_PROD = client-zH8Y7OK1i331OPzT4327C6UxN8ygcM6LzS
    TMP = C:\Users\Comp\AppData\Local\Temp
    SystemDrive = C:
    AMPLITUDE_API_KEY = 28f3cae0b33b8b3702120c9ed1a935aa
    FPS_BROWSER_APP_PROFILE_STRING = Internet Explorer
    OneDriveConsumer = C:\Users\Comp\OneDrive
    SENTRY_ACCESS_TOKEN = 3df78d9e53f845dda78fbaezuzce1a6261421ab1d0999974bc2e933477289017
    HEAP_CLIENT_TOKEN = 1717354662
    ALLUSERSPROFILE = C:\ProgramData
    Path = C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\dotnet\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\Git\cmd;C:\Program Files\PuTTY\;C:\Program Files\WireGuard\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Go\bin;C:\Users\Comp\AppData\Local\Microsoft\WindowsApps;;C:\Users\Comp\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Comp\.dotnet\tools;C:\Users\mcfis\go\bin
    ProgramFiles = C:\Program Files
    LOCALAPPDATA = C:\Users\Comp\AppData\Local
    USERPROFILE = C:\Users\Comp
    WOOTRIC_CLIENT_TOKEN = NPS-aa8gd4c3


    stderr[
    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8

    FAILURE: Build failed with an exception.

    * Where:
    Build file 'C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\mobilenotifications.androidlib\build.gradle' line: 3

    * What went wrong:
    Could not compile build file 'C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\mobilenotifications.androidlib\build.gradle'.
    > startup failed:
    build file 'C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\mobilenotifications.androidlib\build.gradle': 3: only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed

    See https://docs.gradle.org/7.2/userguide/plugins.html#sec:plugins_block for information on the plugins {} block

    @ line 3, column 1.
    plugins {
    ^

    1 error


    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

    * Get more help at https://help.gradle.org

    BUILD FAILED in 2s
    ]
    stdout[

    ]
    exit code: 1
    UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <350bddde343340be9c3b2ea115fe87e9>:0)
    UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <350bddde343340be9c3b2ea115fe87e9>:0)
    UnityEditor.Android.Command.Run (System.String command, System.String args, System.String workingdir, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <350bddde343340be9c3b2ea115fe87e9>:0)
    UnityEditor.Android.AndroidJavaTools.RunJava (System.String args, System.String workingdir, System.Action`1[T] progress, System.String error) (at <350bddde343340be9c3b2ea115fe87e9>:0)
    UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, Unity.Android.Gradle.AndroidGradle androidGradle, System.String workingdir, System.String task, System.Action`1[T] progress) (at <350bddde343340be9c3b2ea115fe87e9>:0)
    Rethrow as GradleInvokationException: Gradle build failed
    UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, Unity.Android.Gradle.AndroidGradle androidGradle, System.String workingdir, System.String task, System.Action`1[T] progress) (at <350bddde343340be9c3b2ea115fe87e9>:0)
    UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <350bddde343340be9c3b2ea115fe87e9>:0)
    UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <350bddde343340be9c3b2ea115fe87e9>:0)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr, Boolean&)
    -----------------------------------------------

    I have already deleted the gradel folder and compared the gradle.build files of both look the same.

    While building it always crashes at this step:
    Calling IPostGenerateGradleAndroidProjects callbacks

    I hope someone can help me with this.
    Thanks in advance.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,757
    Could you show how the problematic build.gradle files looks like?
     
  3. Kernkraft

    Kernkraft

    Joined:
    Oct 26, 2020
    Posts:
    32
    Hey Aurimas,
    sure, here ist the content of the build.gradl file.

    C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\build.gradl :
    --------------------------
    plugins {
    // If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity
    // See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html
    // See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
    // To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
    id 'com.android.application' version '7.1.2' apply false
    id 'com.android.library' version '7.1.2' apply false

    }

    task clean(type: Delete) {
    delete rootProject.buildDir
    }
    --------------------------

    C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\mobilenotifications.androidlib\build.gradle :
    --------------------------
    apply plugin: 'com.android.application'

    plugins {
    id 'com.android.library'
    }

    def getCompileSdk(unityLib) {
    def unityCompileSdk = unityLib.compileSdkVersion
    def version = unityCompileSdk.find('\\d+').toInteger()
    if (version < 33) {
    return 33
    }
    return version
    }

    android {
    // Checking if namespace exists is needed for 2021.3 (AGP 4.0.1)
    // When 2021.3 is dropped, remove this check and package="com.unity.androidnotifications" from AndroidManifest
    if (project.android.hasProperty("namespace")) {
    namespace "com.unity.androidnotifications"
    }

    def unityLib = project(':unityLibrary').extensions.getByName('android')
    compileSdkVersion getCompileSdk(unityLib)
    buildToolsVersion unityLib.buildToolsVersion

    defaultConfig {
    minSdkVersion 22
    consumerProguardFiles "proguard-rules.pro"
    }

    buildTypes {
    release {
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    }
    compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    }
    }

    dependencies {
    api files('../libs/unity-classes.jar')
    }
    --------------------------

    C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\unityLibrary\build.gradle :
    --------------------------
    apply plugin: 'com.android.library'


    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project('mobilenotifications.androidlib')

    }

    android {
    ndkPath "C:/Program Files/Unity/Hub/Editor/2022.3.20f1/Editor/Data/PlaybackEngines/AndroidPlayer/NDK"

    compileSdkVersion 34
    buildToolsVersion '32.0.0'

    compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
    }

    defaultConfig {
    minSdkVersion 22
    targetSdkVersion 34
    ndk {
    abiFilters 'armeabi-v7a'
    }
    versionCode 1
    versionName '1.0'
    consumerProguardFiles 'proguard-unity.txt'
    }

    lintOptions {
    abortOnError false
    }

    aaptOptions {
    noCompress = ['.unity3d', '.ress', '.resource', '.obb', '.bundle', '.unityexp'] + unityStreamingAssets.tokenize(', ')
    ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:!CVS:!thumbs.db:!picasa.ini:!*~"
    }

    packagingOptions {
    doNotStrip '*/armeabi-v7a/*.so'
    }
    }
    --------------------------

    C:\ProjectFolder\Library\Bee\Android\Prj\Mono2x\Gradle\launcher\build.gradle :
    --------------------------
    apply plugin: 'com.android.application'

    dependencies {
    implementation project(':unityLibrary')
    }

    android {
    ndkPath "C:/Program Files/Unity/Hub/Editor/2022.3.20f1/Editor/Data/PlaybackEngines/AndroidPlayer/NDK"

    compileSdkVersion 34
    buildToolsVersion '32.0.0'

    compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
    }

    defaultConfig {
    minSdkVersion 22
    targetSdkVersion 34
    applicationId 'com.Project.Project'
    ndk {
    abiFilters 'armeabi-v7a'
    }
    versionCode 1
    versionName '1.0'
    }

    aaptOptions {
    noCompress = ['.unity3d', '.ress', '.resource', '.obb', '.bundle', '.unityexp'] + unityStreamingAssets.tokenize(', ')
    ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:!CVS:!thumbs.db:!picasa.ini:!*~"
    }

    lintOptions {
    abortOnError false
    }

    buildTypes {
    debug {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt')
    signingConfig signingConfigs.debug
    jniDebuggable true
    }
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt')
    signingConfig signingConfigs.debug
    }
    }

    packagingOptions {
    doNotStrip '*/armeabi-v7a/*.so'
    }

    bundle {
    language {
    enableSplit = false
    }
    density {
    enableSplit = false
    }
    abi {
    enableSplit = true
    }
    }
    }
    --------------------------
     
    Last edited: Feb 29, 2024
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,757
  5. Kernkraft

    Kernkraft

    Joined:
    Oct 26, 2020
    Posts:
    32
    Thats a good question. My package version of Mobile Notifications is 2.3.1.
    upload_2024-3-1_10-37-42.png

    Should I try to delete this line?
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,757
    Start by navigating to the package itself and see the file in there. When I checked this version of the package, the line is not there. Could be that some bogus post processing adds it later on.
     
    Kernkraft likes this.
  7. Kernkraft

    Kernkraft

    Joined:
    Oct 26, 2020
    Posts:
    32
    Your guess with the mobile notification package was right. I have checked the packages for unity.mobile and after unlocking the mobile.notification package I was able to update it to a newer recommended version. With the newer version the build runs successfully.

    Thank you very much for your help.