LoginSignup
4
2

More than 5 years have passed since last update.

Unityでfastlane androidのビルド対応する

Last updated at Posted at 2016-11-23

手順

fastlaneのinstall

Gemfileを作成

Gemfile
source 'https://rubygems.org'

ruby '2.3.3'
gem 'fastlane'

bundler をinstall

$ gem install bundler

fastlaneをinstall

$ bundle install

fastlane の設定

$ bundle exec fastlane
... いろいろ答える

下記ファイルが出来る

  • fastlane/Appfile
  • fastlane/Fastfile

fastlaneにbuild laneを追加

unityでビルドを叩くレーンを記述

fastlane/Fastfile
platform :android
# 中略...
  desc "Build application"
  lane :build do
    CMD         = '/Applications/Unity/Unity.app/Contents/MacOS/Unity'
    LOGFILE     = '/tmp/unity.log'
    PROJECT_DIR = File.expand_path('..', Dir.pwd)

    begin
      sh "#{CMD} -quit -batchmode -buildTarget android -logfile #{LOGFILE} -executeMethod App.Builder.ExportAndroid -projectPath #{PROJECT_DIR}"
    rescue
      sh "cat #{LOGFILE}"
      raise 'Unity biuld failed'
    end
  end
# ...
end

Unityのbuildスクリプトを記述する

build laneから叩くビルドスクリプトを記述する.
keystoreなどの設定は適宜おこなう。

Assets/Editor/Builder.cs
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace App
{
    public class Builder : MonoBehaviour
    {
        public static void ExportAndroid()
        {
            // KeyStore設定
            PlayerSettings.Android.keystoreName = "FIXME";
            PlayerSettings.Android.keystorePass = "FIXME";
            PlayerSettings.Android.keyaliasName = "FIXME";
            PlayerSettings.Android.keyaliasPass = "FIXME";

            // オプション設定
            EditorUserBuildSettings.exportAsGoogleAndroidProject = false;
            BuildOptions options = BuildOptions.Development;

            // 出力先
            var apkPath = string.Format("{0}-{1}.apk", PlayerSettings.bundleIdentifier, Application.version);

            Export(apkPath, BuildTarget.Android, options);
        }

        private static void Export(string path, BuildTarget target, BuildOptions options)
        {
            var error = BuildPipeline.BuildPlayer(GetAllScenes(), path, target, options);

            if (string.IsNullOrEmpty(error))
            {
                Debug.Log("Build Success");
            }
            else
            {
                Debug.Log("Build Failure: " + error);
                throw new Exception("Unity pipeline build failure: " + error);
            }
        }

        private static string[] GetAllScenes()
        {
            return EditorBuildSettings.scenes.Select(it => it.path).ToArray();
        }

        private static string ReadAll(string path)
        {
            FileInfo fileInfo = new FileInfo(path);
            using (StreamReader reader = new StreamReader(fileInfo.OpenRead(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }
}

実行

$ bundle exec fastlane android build

これで <package_name>-<version>.apk が生成されているはず!

4
2
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
4
2