facebook twitter hatena line email

Unity/実機/Android

提供: 初心者エンジニアの簡易メモ
2019年5月30日 (木) 15:16時点におけるAdmin (トーク | 投稿記録)による版 (64bit対応)

移動: 案内検索

Android実機で動かす

  • File/BuildSettingsを選択
  • Androidを選択
  • OpenDownloadPageからプレイヤーをDL
  • Unityを一度再起動
  • Unity/Preference/ExternalTools/Android/SDK・JDK・NDKのpathを入れる
SDK=/Users/[user]/Library/Android/sdk
JDK=/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/bin/java # 確認 $ locate jdk | grep bin/java$
NDK=/Users/[user]/Library/Android/sdk/ndk-bundle
  • PlayerSettingsを選択
  • Inspector/端末マーク/OtherSetting
  • Bundle Identifierの"com.company.ProjectName"を適宜変更する(デフォから変えないとbuild時にエラーとなる)
  • buildボタンを押しapkを作成する
  • build&runボタンを押しandroidで動作させる。
  • 縦向き・横向きで確認する

向きを固定

  • File/BuildSettings
  • OriantationをAutoRotationからportrait(縦)へ

ビルドエラー

Bundle Identifier has not been set up correctly

  • 上記のBundle Identifierがデフォルトの"com.company.ProjectName"だと起こるエラー。適宜変更する

NDK 13.1.3345770 is incompatible with IL2CPP. IL2CPP requires r10e (64-bit).エラーとなる場合

  1. NDK欄隣のDownloadボタンを押し、
  2. android-ndk-r10e-darwin-x86_64.binをDownload
  3. 適当な場所に移動し、NDKのpathへ入力する

Unable to list target platforms. Please make sure the android sdk path is correct. See the Console for details.エラーの場合

最新の Android Studio に付属している SDK Tools がまずいようなので、25バージョンを使うように

https://dl.google.com/android/repository/tools_r25.2.5-macosx.zip

~/Library/Android/sdk/toolsを差し替える

AndroidBuildエラー(2018.3.0f2で発生)

Android SDK is outdated
SDK Build Tools version 28.0.0 < 28.0.3.
Note: when building with Gradle, "Use Highest Installed" option will update SDK Build Tools as well.

Use Highest Installedボタンを選択するとbuildできた。

transformClassesWithMultidexlistForReleaseエラー

com.android.build.api.transform.TransformException: Error while generating the main dex list.

Assets/Plugins/Android/mainTemplate.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:multidex:1.0.3'
}

classpath 'com.android.tools.build:gradle:3.2.0'が2系じゃなくて3系の場合は、 implementationがcompileになってる可能性があるので治す。

remote No space left on deviceエラー

CommandInvokationFailure: Unable to install APK to device. Please make sure the Android SDK is installed and is properly configured in the Editor. See the Console for more details. 
remote No space left on device

端末内に容量がないので、アプリをいくつか削除する

gradleで動かす方法

  1. file/build setting/build systemを"internal"から"gradle"にする

gradleでプロガード設定のないlibsを動かす方法

  1. file/build setting/build systemを"internal"から"gradle"にする
  2. file/build setting/PlayerSetting/PublisingSetting/minifyのreleaseを"progurad"から"none"に変更する

gradleのカスタムファイルを作る

  1. file/build setting/PlayerSetting/PublisingSetting/Build/CustomGradleTemplateにチェックを入れる
  2. Assets/Plugins/AndroidにmainTemplate.gradleができる

2018.3.0f2のmainTemplate.gradleのテンプレ

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
**BUILD_SCRIPT_DEPS**}
}

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

apply plugin: 'com.android.application'
**APPLY_PLUGINS**

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
**DEPS**}
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**'
    }

    lintOptions {
        abortOnError false
    }

    aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**]
    }**SIGN**
    buildTypes {
        debug {
            minifyEnabled **MINIFY_DEBUG**
            useProguard **PROGUARD_DEBUG**
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
            jniDebuggable true
        }
        release {
            minifyEnabled **MINIFY_RELEASE**
            useProguard **PROGUARD_RELEASE**
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD****SIGNCONFIG**
        }
    }**PACKAGING_OPTIONS****SPLITS**
**BUILT_APK_LOCATION**
    bundle {
        language {
            enableSplit = false
        }
        density {
            enableSplit = false
        }
        abi {
            enableSplit = true
        }
    }
}**SPLITS_VERSION_CODE****REPOSITORIES****SOURCE_BUILD_SETUP**

AndroidStudio用ファイルを出力する

  1. file/build setting/ExportProjectにチェックを入れる
  2. exportボタンを押す

Cannot read packageName AndroidManifest.xmlエラーの場合

manifestにpackage名を登録する

<manifest package="jp.co.example.hogehoge"
以下略

buildsystemがinternalだとpackage名を省略できたがGradleだと省略できなかった。

gradleのPackageNameについて

  • 2018.1.1f1だとOtherSettings側のPackageNameでなくAndroidManifest.xml側のPackageNameに書いたほうで上書掛かれた。
  • 2018.3.0f2だとOtherSettings側のPackageNameに記述したものが、AndroidManifest.xml側のPackageNameよりも優先された。

AndroidManifest.xmlを作成する

  1. 一度ビルドするとTemp/StagingArea/AndroidManifest.xmlができる。
  2. それをAssets/Plugins/Androidの下にコピー

参考:https://qiita.com/peroon/items/ba55d583a68c58f0faa5

Unity2018.1.1f1で作成されたAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" xmlns:tools="http://schemas.android.com/tools" android:installLocation="preferExternal">
 <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
 <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:isGame="true" android:banner="@drawable/app_banner">
   <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="false">
     <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
       <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
     </intent-filter>
     <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
   </activity>
   <meta-data android:name="unity.build-id" android:value="05a8f86c-bb43-431c-9b29-a73d7c23160d" />
   <meta-data android:name="unity.splash-mode" android:value="0" />
   <meta-data android:name="unity.splash-enable" android:value="True" />
 </application>
 <uses-feature android:glEsVersion="0x00020000" />
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
 <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
 <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
</manifest>

multidex必須と出たとき

Execution failed for task ':transformClassesWithDexForRelease'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: 
java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files

multidexを追加するかライブラリ数orライブラリメソッド数を減らす

multidexのバージョンが存在しないと出たとき

A problem occurred configuring root project 'gradleOut'.
> Could not resolve all dependencies for configuration ':_debugApkCopy'.
   > Could not find com.android.support:multidex:1.0.3.

multidexが適切にインストールされているか確認する

ls ~/Library/Android/sdk/extras/android/m2repository/com/android/support/multidex/1.0.3/multidex-1.0.3.jar

なければsdktoolのandroidSupportRegistryをアンインストールしてインストールする。

androidのbackキー

if (Application.platform == RuntimePlatform.Android)
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
    }
}

64bit対応

buildSetting/Android/Configures

  1. scriptingBackendをmonoからIL2CPPへ
  2. ARM64にチェック

上記設定時の設定ファイル差分 ProjectSettings/ProjectSettings.asset

-  AndroidTargetArchitectures: 5
+  AndroidTargetArchitectures: 7
-  scriptingBackend: {}
+  scriptingBackend:
+    Android: 1

公式androidサポート:https://developer.android.com/distribute/best-practices/develop/64-bit#test_your_app_on_64-bit_hardware

  1. build時にndkが存在しない時はndkのDLダイアログがでるのでDLする
  2. DLダイアログが出ない場合はこちらから(https://developer.android.com/ndk/downloads/older_releases.html?hl=ja)
  3. android-ndk-r16b-darwin-x86_64.zip
  4. 解凍して設置して、ndkを指定する