15
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Unity] 最新のNDKでgradleビルドが通らない問題に対処する

Last updated at Posted at 2018-11-28

UnityのAndroidビルドで、Build systemgradleにした際、以下のビルドエラーが発生する件

No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android

このエラーでググると、Unityに限らずAndroidのビルドで同じ問題が発生している記事等が色々出てきます。
エラーの原因としては、メッセージの通り、toolchainsの "mips64el-linux-android" ディレクトリが最新のNDKでは削除されたことのようです。
Unityのbuild.gradleでは ndk.abiFilters でそもそもmipsは含まれていませんが、abiFiltersに関わらず発生します。

対処方法はいくつかあります。なお、2017.4.13f1で確認しています。

1. Gradle plugin のバージョンを上げる(要Export)

Unityが使う build.gradleでは、Gradle pluginのバージョンが、2.3.0 になっています。

buildscript {
...
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
    }
}

これを3.1.3以上に上げるとビルドが通るようになります。

classpath 'com.android.tools.build:gradle:3.1.3'

ただし、一旦プロジェクトをエクスポートしてからビルドする必要があります。
Unityで直接ビルドした場合は以下のビルドエラーが発生しました。

A problem occurred evaluating root project 'gradleOut'.
> Failed to apply plugin [id 'com.android.application']
   > Minimum supported Gradle version is 4.4. Current version is 4.0.1. If using the gradle wrapper, try editing the distributionUrl in <ProjectDir>/Temp/gradleOut/gradle/wrapper/gradle-wrapper.properties to gradle-4.4-all.zip

Unity 2017.4.13f1 が使うGradleのバージョンは 4.0.1 のようですが、4.4以上が必要になります。
なお、2018.2.17f1 でも4.2.1のようなので、こちらも直接ビルドは無理そうです。

2. ndk.dirのパスを指定する

Unityが出力する local.properties ファイルには ndk.dirが指定されていませんが、このndk.dirを指定すると
AndroidStudioにバンドルされているNDKを参照しなくなるようで、ビルドが通るようになります。
UnityのIL2CPPでは古めのNDK(2017だとr13b)を使うよう指定されているので、もしIL2CPPを使っている場合は
このNDKのディレクトリを指定すると良いです。
(なお、ndk.dir= という感じでパスが空でもビルドが通りました)

とはいえ、Unityは local.properties をビルド時に自動で生成するので、直接編集することはできません。
そのため以下のコードを mainTemplate.gradle の一番上に追記することで、ビルド時に値を設定するという方法をとります。

def properties = new Properties()
def propFile = project.rootProject.file('local.properties')
properties.load(propFile.newDataInputStream())
properties.setProperty('ndk.dir', '<NDK_PATH>/android-ndk-r13b')
properties.store(propFile.newWriter(), null)

...

なお、mainTemplate.gradle は、AndroidのPlayerSettingsで、Custom Gradle Template にチェックを入れると生成されます。

Android PlayerSettings

これが build.gradle のベースとなります。

3. SDK ToolsからNDKを削除する

image.png

NDKのチェックを外し、アンインストールします。これだけでビルドが通るようになります。
IL2CPPでビルドしている場合でも、Unityは別途インストールしたNDKを使うため問題ありません。
Unity以外のAndroidプロジェクトでNDKを使っている場合は困りますが、そうでない場合は一番手っ取り早いです。

4. mips64el-linux-android ディレクトリをでっちあげる

mips64el-linux-android ディレクトリがないのが問題なのだから、ディレクトリ作れば良いだろうという方法です。
以下のように、他のディレクトリにシンボリックリンクを張るか、丸ごとコピーするとかします。

cd ~/Library/Android/sdk/ndk-bundle/toolchains/
ln -s x86_64-4.9 mips64el-linux-android

どれか好きな方法で対処してください。そのうちUnityのバージョンアップで対応されると思います。

15
8
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
15
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?