Android OS (7.1.2)のビルドとHello World

#行うこと
1. Android 7.1.2のソースを取得＆ビルドしてNexus9へ書き込み
2. 起動時「Hello, world!」のダイアログを表示するようカスタマイズ

#準備
JDK8のインスール
http://qiita.com/niusounds/items/1f32dcd6fa1f57ade98a

#ソースコードのダウンロード
```bash
$ curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo
$ mkdir -p aosp/7.1.2
$ cd aosp
$ ~/bin/repo init -u https://android.googlesource.com/platform/manifest -b android-7.1.2_r17
$ ~/bin/repo sync
```
```text
...
Checking out files: 100% (6377/6377), done. out files:  26% (1719/6377)   
Checking out files: 100% (1717/1717), done.
Checking out files: 100% (44431/44431), done.
Checking out files: 100% (62/62), done.king out files:  14% (9/62)   
Checking out files: 100% (1756/1756), done.
Checking out files: 100% (8484/8484), done.
Checking out files: 100% (539/539), done.ng out files:  28% (151/539)   
Checking out files: 100% (15774/15774), done.ut files:   7% (1128/15774)   
Checking out files: 100% (25/25), done.
Syncing work tree: 100% (529/529), done. 
```

#ソースのビルド
Javaの設定

```bash
$ export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
$ export PATH=$PATH:$JAVA_HOME/bin
```
ビルドの設定

```bash
$ source build/envsetup.sh 
$ lunch
```

```text
Lunch menu... pick a combo:
     1. aosp_arm-eng
     2. aosp_arm64-eng
     3. aosp_mips-eng
     4. aosp_mips64-eng
     5. aosp_x86-eng
     6. aosp_x86_64-eng
     7. full_fugu-userdebug
     8. aosp_fugu-userdebug
     9. mini_emulator_arm64-userdebug
     10. m_e_arm-userdebug
     11. m_e_mips-userdebug
     12. m_e_mips64-eng
     13. mini_emulator_x86-userdebug
     14. mini_emulator_x86_64-userdebug
     15. aosp_dragon-userdebug
     16. aosp_dragon-eng
     17. aosp_marlin-userdebug
     18. aosp_sailfish-userdebug
     19. aosp_flounder-userdebug
     20. aosp_angler-userdebug
     21. aosp_bullhead-userdebug
     22. hikey-userdebug
     23. aosp_shamu-userdebug

Which would you like? [aosp_arm-eng] aosp_flounder-eng ← ビルドしたい端末に合わせて選択(https://forum.xda-developers.com/wiki/Google/Device_Codenames)

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.2
TARGET_PRODUCT=aosp_flounder
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=denver64
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=denver
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.4.0-78-generic-x86_64-with-debian-stretch-sid
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=NJH47B
OUT_DIR=out
============================================
```

ビルド

```bash
$ make -j8 &> makelog_`date "+%Y%m%d_%H%M%S"`.txt
```

```text:makelog_20170627_010549.txt
...
[ 99% 49532/49533] host C++: primitives_tests_32 <= system/media/audio_utils/tests/primitives_tests.cpp
[100% 49533/49533] host Executable: primitives_tests_32 (out/host/linux-x86/obj32/EXECUTABLES/primitives_tests_intermediates/primitives_tests32)

[0;32m#### make completed successfully (02:12:53 (hh:mm:ss)) ####[00m
```

#AndroidのFactoryImageをNexus9へ書き込み
1. 対象端末のFactoryImageを以下からDLしてきて解凍
https://developers.google.com/android/images
(Nexus9(flounder)の7.1.2がなかったため7.1.1をDLした)
2. コマンドプロンプトから「adb reboot bootloader」を実行
3. 1で解凍したファイル内にあるflash-all.batを実行

#ビルドしたsystem.img、boot.imgを書き込み
以下ビルド生成物を書き込み。
/out/target/product/flounder/system.img
/out/target/product/flounder/boot.img

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

<img width="768" alt="Screenshot_20170627-123118.png" src="https://qiita-image-store.s3.amazonaws.com/0/38382/98b7b524-8d13-ba3c-45b3-5b51edc1c4ef.png">
<img width="768" alt="Screenshot_20170627-123125.png" src="https://qiita-image-store.s3.amazonaws.com/0/38382/6a5ab673-2c0f-0ab1-ca2b-d73457316b36.png">

#Hello, World!
##ソースの修正
起動時に「Hellor, World!」のダイアログを表示するようカスタマイズする。

ブート完了時に呼ばれるActivityManagerService#finishBootingの最後に以下の修正を入れる。

https://android.googlesource.com/platform/frameworks/base/+/android-7.1.2_r17/services/core/java/com/android/server/am/ActivityManagerService.java#finishBooting

```diff:ActivityManagerService.java
...
                mUserController.sendBootCompletedLocked(
                        new IIntentReceiver.Stub() {
                            @Override
                            public void performReceive(Intent intent, int resultCode,
                                    String data, Bundle extras, boolean ordered,
                                    boolean sticky, int sendingUser) {
                                synchronized (ActivityManagerService.this) {
                                    requestPssAllProcsLocked(SystemClock.uptimeMillis(),
                                            true, false);
                                }
                            }
                        });
                scheduleStartProfilesLocked();
            }
         }

+        AlertDialog.Builder builder = new AlertDialog.Builder(mContext)
+            .setTitle("Hello, World!");
+        Dialog dialog = builder.create();
+        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
+        dialog.show();
    }
```

"ビルド
修正した箇所をピンポイントでビルド

```bash
~/AOSP/7.1.2$ mmm frameworks/base/services
```

```text
...
#### make completed successfully (32 seconds) ####
```

ビルド生成物は以下に出力先

```text
out/target/product/flounder/system/framework/services.jar
```

#端末へ書き込み
端末内のservices.jarをビルドしたservices.jarで差し替え

```bash
$ adb remount
$ adb push <services.jarのあるフォルダパス>/services.jar /system/framework/.
$ adb reboot
```

<img width="768" alt="Screenshot_20170627-124524.png" src="https://qiita-image-store.s3.amazonaws.com/0/38382/38d3fd70-1884-95ae-bcb0-4d1d272f2918.png">