Files
Obsidian-Main/20.01. Android/Android programming.md

164 lines
6.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## Build AOSP
### Build compile environment
1. Install Ubuntu 18.04
2. Install packages: `sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig`
- https://source.android.com/setup/build/initializing
3. Install Repo
```
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo ;\
chmod a+x ~/bin/repo ;\
gpg --recv-key 8BB9AD793E8E6153AF0F9A4416530D5E920F5C65 ;\
curl https://storage.googleapis.com/git-repo-downloads/repo.asc | gpg --verify - ~/bin/repo
```
- https://source.android.com/setup/develop#installing-repo
4. Download AOSP source
1. Create folder for AOSP
```
mkdir -p ~/codes/aosp ; cd ~/codes/aosp
```
2. Setup git
```
git config --global user.name AwinHuang ;\
git config --global user.email awinhuang@gmail.com
```
3. Download source code
```
repo init -u https://android.googlesource.com/platform/manifest ;\
repo sync -j8
```
- 如果要切換某一個特定版本可以使用`-b`,例如:`repo init -u https://android.googlesource.com/platform/manifest -b android-10.0.0_r47`。
- 要知道版本tag可以查看https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds
5. Build code
```
source build/envsetup.sh ;\
lunch aosp_arm-eng ;\
make clobber ;\
make -j16
```
- `make clobber`用來刪除build資料夾
### Reference
- [GitHub - henrymorgen/android-knowledge-system: Android应用开发最强原创知识体系](https://github.com/henrymorgen/android-knowledge-system)
- [Android AOSP基础AOSP源码和内核源码下载 | BATcoder - 刘望舒](http://liuwangshu.cn/framework/aosp/2-download-aosp.html)
- [Android AOSP基础Android系统源码的整编和单编 | BATcoder - 刘望舒](http://liuwangshu.cn/framework/aosp/3-compiling-aosp.html)
## Build kernel
1. Download the code
```
mkdir -p ~/codes/kernel ;\
cd ~/codes/kernel ;\
repo init -u https://android.googlesource.com/kernel/manifest ;\
repo sync -j16
```
2. Compile
```
build/build.sh
```
- 如果遇到`Command 'java' not found, but can be installed with:`
- 依序安裝
- `sudo apt install default-jre`
- `sudo apt install openjdk-11-jre-headless`
- `sudo apt install openjdk-8-jre-headless`
- 執行 `sudo update-alternatives --config java`
- 選擇 `/usr/lib/jvm/java-11-openjdk-amd64/bin/java`
- 再次compile
- `source build/envsetup.sh`
- `mm idegen`
3. 產生android.iml和android.ipr
在source code跟目錄下執行`development/tools/idegen/idegen.sh`
### Reference
- [Android kernel源码下载与编译](https://blog.csdn.net/u010164190/article/details/106561022)
## Android App programming
- R的全名`<PACKAGE_NAME> + .R`例如package name是`com.awin.testapp`,那全名是`com.awin.testapp.R`。
- AndroidX = Android eXtension
- Layout
- layout_margin: 物件與其他物件的距離
- layout_gravity: 物件在容器內的位置(靠左、靠右、置中...
- textApperance: 字型大小
- Extensions
- Android 4.1 沒有自動加入的extension
- 打開build.gradle在`plugins`區塊中加入:
```
id 'kotlin-kapt'
id 'kotlin-android-extensions'
```
- 使用ViewModel & LiveData
- 確認有 `kotlin-kapt` 這個plugin。
![[Pasted image 20210330102148.png]]
- [Android jetpack所有library](https://developer.android.com/jetpack/androidx/explorer)
- [Android jetpack - Lifecycle](https://developer.android.com/jetpack/androidx/releases/lifecycle)
- 從`Declaring dependencies`這區塊複製必要的module
![[Pasted image 20210330110411.png]]
```
def lifecycle_version = "2.3.1"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Annotation processor
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
```
- Create a ViewModel
```kotlin
viewModel = ViewModelProvider(this).get(GuessViewModel::class.java)
```
- Observe a live data in ViewModel
```kotlin
viewModel.counter.observe(this, Observer {
counter.setText(it.toString())
})
```
counter這個變數是包含在ViewModel裡面的live data我們的資料則是放在counter裡面的「value」。
所以如果要取用我們的data則是`viewModel.counter.value`。
- 使用LiveData
- `val counter = MutableLiveData<Int>()`
### Use ViewBinding
ViewBinding is used to replace Kotlin Synthetics.
1. 在`app/build.gradle`中加入:
```
plugins {
...
id 'kotlin-parcelize' <-- Add this
}
android {
...
buildFeatures { <-- Add this
viewBinding true
}
...
}
```
2. 在你的activity裡面
1. `import <APP_DOMAIN_NAME>.databinding.<ACTIVITY_NAME>Binding`
假如APP_DOMAIN_NAME是`com.example.testmultisectioncyclerview`ACTIVITY_NAME是`ActivityMain`,那就是:
`import com.example.testmultisectioncyclerview.databinding.ActivityMainBinding`
2. 用`lateinit`宣告一個變數變數名稱是activity的名字加上binding例如`ActivityMain`就是:
`private lateinit var activityBinding: ActivityMainBinding`
3. 在`onCreate()`中,就可以用`activityBinding`來取得view與其他元件了
```
activityBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(activityBinding.root) <-- root就是view
```
## ADB usage
- [如何透過 adb command line 指令啟動 Android App](https://kkboxsqa.wordpress.com/2014/08/20/%E5%A6%82%E4%BD%95%E9%80%8F%E9%81%8E-adb-command-line-%E6%8C%87%E4%BB%A4%E5%95%9F%E5%8B%95-android-app/)
# MISC
## 教學文
- [Android Template 小技巧 及 寫程式常見的問題](https://www.eeaseries.com/2021/01/android-template.html?m=1)
- [Jetpack Compose 基础知识](https://developers.google.com/codelabs/jetpack-compose-basics?hl=zh-cn#0)
- [一文带你了解适配Android 11分区存储](https://zhuanlan.zhihu.com/p/354632087)