LoginSignup
2
1

【備忘録】UnityのAndroidビルドでAPI level 33にしてエラーが出た時はこれをやってみろ!

Last updated at Posted at 2024-04-12

はじめに

Google様の「今度からTargetAPILevelを〇〇以上にしてね。古いの使ってたらアプリリリースさせないから」に対応する際、役に立つかもしれない情報を書き留めておく。

環境

OS: macOS Ventura 13.0
CPU: M1
Unity: 2021.3.0f1 (2020.3.41f1でも確認済み)

やったこと

  • ProjectSettings > Player > Other Settings for Android にある「Target API level」を 33 に変更
    スクリーンショット 2024-01-19 1.41.22.png
  • /Applications/Unity/Hub/Editor/2021.3.0f1/PlaybackEngines/AndroidPlayer/SDK/platforms内に、他のエディタ等から引っ張ってきたSDK(android-33)をディレクトリごとコピー
    (SDKがどこにもない場合は、AndroidStudioなどを利用してDLできる)
  • ビルド

エラー

java.io.IOException: Can't read [/Applications/Unity/Hub/Editor/2021.3.0f1/PlaybackEngines/AndroidPlayer/SDK/platforms/android-33/optional/android.car.jar] (Can't process class [android/car/Car$CarServiceLifecycleListener.class] (Unsupported version number [55.0] (maximum 54.0, Java 10))) See the Console for details.
> Configure project :launcher
WARNING: The option setting 'android.enableR8=false' is deprecated.
It will be removed in version 5.0 of the Android Gradle plugin.
You will no longer be able to disable R8

他にも、gradleが読み込めないみたいなことを言われるかもしれない

解決方法

  • ProjectSettings > Player > Publishing Settings for Android にある「Custom Launcher Gradle Template」と「Custom Base Gradle Template」にチェックを入れる
    スクリーンショット 2024-01-19 2.03.23.png

  • Assets/Plugin/Android 内に生成されたlauncherTemplate.gradlebaseProjectTemplate.gradleをそれぞれ次のように書き換える

launcherTemplate.gradle
apply plugin: 'com.android.application'

dependencies {
    implementation project(':unityLibrary')
    }

android {
    compileSdkVersion **APIVERSION**
    buildToolsVersion '**BUILDTOOLS**'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion **MINSDKVERSION**
        targetSdkVersion **TARGETSDKVERSION**
        applicationId '**APPLICATIONID**'
        ndk {
            abiFilters **ABIFILTERS**
        }
        versionCode **VERSIONCODE**
        versionName '**VERSIONNAME**'
    }

+    packagingOptions {
+        exclude "META-INF/kotlinx_coroutines_core.version"
+    }

    aaptOptions {
        noCompress = **BUILTIN_NOCOMPRESS** + unityStreamingAssets.tokenize(', ')
        ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
    }**SIGN**

    lintOptions {
        abortOnError false
    }

    buildTypes {
        debug {
            minifyEnabled **MINIFY_DEBUG**
            proguardFiles getDefaultProguardFile('proguard-android.txt')**SIGNCONFIG**
            jniDebuggable true
        }
        release {
            minifyEnabled **MINIFY_RELEASE**
            proguardFiles getDefaultProguardFile('proguard-android.txt')**SIGNCONFIG**
        }
    }**PACKAGING_OPTIONS****PLAY_ASSET_PACKS****SPLITS**
**BUILT_APK_LOCATION**
    bundle {
        language {
            enableSplit = false
        }
        density {
            enableSplit = false
        }
        abi {
            enableSplit = true
        }
    }
}**SPLITS_VERSION_CODE****LAUNCHER_SOURCE_BUILD_SETUP**
baseProjectTemplate.gradle
allprojects {
    buildscript {
        repositories {**ARTIFACTORYREPOSITORY**
            google()
-            jcenter()
+            mavenCentral()
        }

        dependencies {
            // 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
-            classpath 'com.android.tools.build:gradle:4.0.1'
+            classpath 'com.android.tools.build:gradle:4.2.2'
+            classpath 'com.guardsquare:proguard-gradle:7.1.0'
            **BUILD_SCRIPT_DEPS**
        }
    }

    repositories {**ARTIFACTORYREPOSITORY**
        google()
-        jcenter()
+        mavenCentral()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
  • Gradle6.7.1をこちらからダウンロードし、適切な場所に配置した後、Preferences > External Tools 内の「Gradle Installed with Unity (recommended)」のチェックを外してパスを設定する
    • 例:/Applications/Unity/Hub/Editor/2021.3.0f1/PlaybackEngines/AndroidPlayer/Tools/gradle/gradle-6.7.1
      スクリーンショット 2024-01-19 2.27.14.png

再ビルド!

上記の設定が終わったら、もう一度ビルドしてみる。
エラーなく完了したら成功。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1