Android - Gappsを導入してビルド


AOSPのソースを取得してビルドしたものにはPlayストア等のGoogleアプリが含まれていないため、
フリーで公開されているGappsを導入してNexus9(flounder)向けビルドを試みる。

・Gapps
http://opengapps.org/

・Gappsを導入したビルド方法
https://github.com/opengapps/aosp_build

#Gapps導入＆ビルド手順(Nexus9向け)
##ソース取得とGapps導入
#####1. <a link="http://qiita.com/popondeli/items/f9b29f53fe161a762a0e">Android OS (7.1.2)のビルドとHello World</a>の「ソースコードのダウンロード」のrepo init～まで終わらせる。

#####2. ${ANDROID_BUILD_TOP}/.repo/manifest/default.xml　の最後に以下を追記する。

```diff
...
+    <remote name="opengapps" fetch="https://github.com/opengapps/"  />
+
+    <project path="vendor/google/build" name="aosp_build" revision="master" remote="opengapps" />
+    <project path="vendor/opengapps/sources/all" name="all" clone-depth="1" revision="master" remote="opengapps" />
+
+    <!-- arm64 depends on arm -->
+    <project path="vendor/opengapps/sources/arm" name="arm" clone-depth="1" revision="master" remote="opengapps" />
+    <project path="vendor/opengapps/sources/arm64" name="arm64" clone-depth="1" revision="master" remote="opengapps" />
+
+    <project path="vendor/opengapps/sources/x86" name="x86" clone-depth="1" revision="master" remote="opengapps" />
+    <project path="vendor/opengapps/sources/x86_64" name="x86_64" clone-depth="1" revision="master" remote="opengapps" />
</manifest>
```

#####3. repo syncでソース取得

#####4. device\htc\flounder\device.mkの最初に以下を追記

```diff:device.mk
#
# Copyright (C) 2013 The Android Open-Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

+GAPPS_VARIANT := <variant>
...
```

variantは<a linl="https://github.com/opengapps/opengapps/wiki/Package-Comparison">package types</a>から選択。

例：

```diff:device.mk
+GAPPS_VARIANT := stock
```


#####5.  device\htc\flounder\device.mkの最後に以下を追記

```diff:device.mk
...
# Add dependency of the proprietary keystore.flounder module.
PRODUCT_PACKAGES += \
    libkeymaster_messages
	
+$(call inherit-product, vendor/google/build/opengapps-packages.mk)
```

##ビルド
#####6. <a link="http://qiita.com/popondeli/items/f9b29f53fe161a762a0e">Android OS (7.1.2)のビルドとHello World</a>「ソースのビルド」を実施

エラー１：

```text
...
No private recovery resources for TARGET_DEVICE flounder
build/core/tasks/vendor_module_check.mk:59: *** Error: Product "aosp_flounder" cannot have overlay in vendor tree: vendor/google/build/overlay/pico vendor/google/build/overlay/webview/24 vendor/google/build/overlay/browser vendor/google/build/overlay/dialer vendor/google/build/overlay/mms.
build/core/ninja.mk:163: ターゲット 'out/build-aosp_flounder.ninja' のレシピで失敗しました
make: *** [out/build-aosp_flounder.ninja] エラー 1

[0;31m#### make failed to build some targets (02:19 (mm:ss)) ####[00m
```

aosp_flounderはベンダーツリーに↑のオーバーレイは持てないとのエラー。
よくわからないため、vendor\google\build\opengapps-packages.mkのDEVICE_PACKAGE_OVERLAYSあたりを
コメントアウトしたらこのエラーはなくなった。

```diff:opengapps-packages.mk
...
-DEVICE_PACKAGE_OVERLAYS += \
-    $(GAPPS_DEVICE_FILES_PATH)/overlay/pico
...
-ifeq ($(GAPPS_FORCE_WEBVIEW_OVERRIDES),true)
-ifneq ($(filter-out $(call get-allowed-api-levels),24),)
-DEVICE_PACKAGE_OVERLAYS += \
-    $(GAPPS_DEVICE_FILES_PATH)/overlay/webview/21
-else
-DEVICE_PACKAGE_OVERLAYS += \
-    $(GAPPS_DEVICE_FILES_PATH)/overlay/webview/24
-endif
PRODUCT_PACKAGES += \
    WebViewGoogle
-endif

-ifeq ($(GAPPS_FORCE_BROWSER_OVERRIDES),true)
-ifneq ($(filter $(call get-allowed-api-levels),23),)
-DEVICE_PACKAGE_OVERLAYS += \
-    $(GAPPS_DEVICE_FILES_PATH)/overlay/browser
-endif
PRODUCT_PACKAGES += \
    Chrome
-endif

-ifneq ($(filter $(call get-allowed-api-levels),23),)
-ifeq ($(GAPPS_FORCE_DIALER_OVERRIDES),true)
-DEVICE_PACKAGE_OVERLAYS += \
-    $(GAPPS_DEVICE_FILES_PATH)/overlay/dialer

PRODUCT_PACKAGES += \
    GoogleDialer
-endif
-endif

-ifeq ($(GAPPS_FORCE_MMS_OVERRIDES),true)
-DEVICE_PACKAGE_OVERLAYS += \
-   $(GAPPS_DEVICE_FILES_PATH)/overlay/mms

PRODUCT_PACKAGES += \
    PrebuiltBugle
-endif

ifeq ($(GAPPS_FORCE_PIXEL_LAUNCHER),true)
GAPPS_EXCLUDED_PACKAGES += \
    GoogleHome

-ifneq ($(filter $(call get-allowed-api-levels),25),)
-DEVICE_PACKAGE_OVERLAYS += \
-    $(GAPPS_DEVICE_FILES_PATH)/overlay/pixelicons

PRODUCT_PACKAGES += \
    PixelLauncherIcons
-endif
```

エラー２：

```text
...
No private recovery resources for TARGET_DEVICE flounder
build/core/tasks/vendor_module_check.mk:64: *** Error: vendor PRODUCT_COPY_FILES file "vendor/opengapps/sources/all/etc/sysconfig/google.xml:system/etc/sysconfig/google.xml" has unknown owner.
build/core/ninja.mk:163: ターゲット 'out/build-aosp_flounder.ninja' のレシピで失敗しました
make: *** [out/build-aosp_flounder.ninja] エラー 1

[0;31m#### make failed to build some targets (40 seconds) ####[00m
```

https://github.com/opengapps/aosp_build/issues/23
https://stackoverflow.com/questions/17333191/building-aosp-vendor-module-check-mk-fail
↑記載のPRODUCT_RESTRICT_VENDOR_FILESをfalseにする方法でビルドは通るようになった。

device\htc\flounder\aosp_flounder.mk


```diff:aosp_flounder.mk
...
PRODUCT_NAME := aosp_flounder
PRODUCT_DEVICE := flounder
PRODUCT_BRAND := Android
PRODUCT_MODEL := AOSP on Flounder
PRODUCT_MANUFACTURER := HTC
-PRODUCT_RESTRICT_VENDOR_FILES := owner path
+PRODUCT_RESTRICT_VENDOR_FILES := false
```


```text
...
[0;32m#### make completed successfully (02:44:51 (hh:mm:ss)) ####[00m
```

Gapps入りのビルドではsystem.imgが1GB程大きくなった。

#Nexus9へ書き込み

```bash
$ adb reboot bootloader
$ fastboot flash boot <ビルドしたboot.imgのあるフォルダパス>/boot.img
$ fastboot flash system <ビルドしたsystem.imgのあるフォルダパス>/system.img
$ fastboot reboot
```

この後初期化を行う。

##Gapps導入前
<img width="768" alt="Screenshot_20170627-123118.png" src="https://qiita-image-store.s3.amazonaws.com/0/38382/0abd9beb-647c-a89d-1568-ef58d43f299a.png">
<img width="768" alt="Screenshot_20170627-123125.png" src="https://qiita-image-store.s3.amazonaws.com/0/38382/8729cd97-296b-96df-23e5-80adad2641d5.png">

##Gapps導入後
<img width="768" alt="Screenshot_20170628-181854.png" src="https://qiita-image-store.s3.amazonaws.com/0/38382/1b77e2e6-a2ec-8772-e006-ce40b55dc4a3.png">
<img width="768" alt="Screenshot_20170629-031955.png" src="https://qiita-image-store.s3.amazonaws.com/0/38382/51dc284b-1cf9-6fb1-8f65-5e1f4624a0f6.png">
<img width="768" alt="Screenshot_20170629-032001.png" src="https://qiita-image-store.s3.amazonaws.com/0/38382/a02ace8e-4f5d-76f1-17ee-23cb43957acd.png">

Googleアプリがインストールされていることが確認できました。