基于Android源碼的應(yīng)用開發(fā)

第一部分:搭建編譯環(huán)境

系統(tǒng)要求

  1. Android源碼比較大,加上編譯后的文件大概在200G左右,保險起見,有必要預(yù)留至少200G的磁盤空間。
  2. 編譯需要至少8G的內(nèi)存,I5及以上的CPU, 64位系統(tǒng)。
  3. Linux系統(tǒng)(Mac據(jù)說也可以,但不在本教程討論的范圍),我使用的是Ubuntu 16.04。

具體步驟

這一部分主要是安裝編譯所需要的軟件,按照官方的指導(dǎo),根據(jù)自己的環(huán)境,一步一步進(jìn)行,如果編譯原生Android,按照官方的教程進(jìn)行。如果編譯LineageOS,在LineageOS Wiki選擇對應(yīng)的機(jī)器進(jìn)行

第二部分:源碼下載

Android 源代碼樹位于由 Google 托管的 Git 代碼庫中,由幾百個Git庫組成。為了方便代碼同步,谷歌提供了Repo工具。詳情參考官方介紹

Repo

Repo的安裝詳情見下載源代碼

安裝 Repo 說明

按照官方的指導(dǎo)進(jìn)行即可,這里是一些補(bǔ)充

  1. 確保Home目錄下有一個 .bin/ 目錄,并且該目錄包含在路徑中:
    第一行是創(chuàng)建文件夾,第二步可以通過編輯~/.bashrc完成
$ gedit ~/.bashrc
$ source ~/.bashrc
  1. 下載 Repo 工具,并確保它可執(zhí)行
    這個地址可能無法直接訪問,可以從其它地方拷貝,Repo是一個用Python編寫的封裝Git命令的工具,只是一個文件。

初始化 Repo 客戶端說明

這一步,如果是編譯原生Android,按照官方的指導(dǎo)進(jìn)行。如果是想要編譯運(yùn)行在特定機(jī)型上的代碼,可以去lineageOS 查找,比如本次演示的Samsung Galaxy Note Pro 12.2 SM-P905 LTE

第三部分: 源碼編譯

這一部分主要也是按官方的教程進(jìn)行。不能只通過純源代碼來使用 AOSP,還需要運(yùn)行與硬件相關(guān)的其他專有庫。

專有二進(jìn)制文件

在github上找到了Samsung Galaxy Note Pro 12.2 SM-P905 LTE 的硬件相關(guān)庫。但并不是所有目標(biāo)設(shè)備的相關(guān)庫都能在網(wǎng)上找到,所以,遇到找不到的情況,就需要從已有的build中提取,可以在這里查找對應(yīng)機(jī)器的build,按這個步驟進(jìn)行。

編譯運(yùn)行刷機(jī)

具體機(jī)器具體對待,這一環(huán)節(jié)比較麻煩,后面有詳細(xì)的補(bǔ)充。

以上是源碼下載,編譯,運(yùn)行的大致流程,這一部分需要花費(fèi)大量的時間完成,過程中可能遇到各種各樣的問題,需要有一定的經(jīng)驗(yàn)才能解決。

源碼目錄:

huihg@huihg-ThinkPad-T440p:~/work/code/smp905$ ls
abi         autoload        build   developers   external    libcore          ndk       platform_testing  syntax     vendor
android     bionic          colors  development  frameworks  libnativehelper  out       plugin            system
Android.bp  bootable        cts     device       hardware    lineage          packages  prebuilts         toolchain
art         bootstrap.bash  dalvik  doc          kernel      Makefile         pdk       sdk               tools

系統(tǒng)應(yīng)用例如相機(jī),日歷在packages/apps目錄,供應(yīng)商的應(yīng)用可以放在vender下面,但這不是必須的。示例托管在[碼云]xxx上(xxx代表私有項目,下同),放到vendor/xxx下面。

第四部分:應(yīng)用開發(fā)

編譯方式

和Android Studio使用Gradle打包不同,Android 源碼中的應(yīng)用是基于GNU make編譯打包的。這里以上面的demo為例進(jìn)行剖析。
代碼目錄

huihg@huihg-ThinkPad-T440p:~/work/code/smp905/vendor/xxx/OpenSDKDemo$ ls
AndroidManifest.xml  Android.mk  bin  CleanSpec.mk  libs  MODULE_LICENSE_APACHE2  NOTICE  README.md  res  src

文件以早期ADT的方式組織,Android源碼中很多APP都是這樣的,但現(xiàn)在也有不少新項目以Gradle的方式組織。原理是一樣的,通過Android.mk指定需要用到的代碼文件,資源文件,依賴的jar/aar等文件。與gradle處理依賴不同的是,所有的jar/aar都要下載到本地,不能自動下載。

Makefile 說明

這里有一篇文章可以參考Makefile && Android.mk(Android6.0)

示例文件:
vendor/xxx/OpenSDKDemo/Android.mk

應(yīng)用編譯

注意,編譯應(yīng)用之前,應(yīng)該完整地編譯過一遍源碼。
這一環(huán)節(jié)完全按照三星Pad SM-P905進(jìn)行演示

編譯步驟

進(jìn)入工作platform

$ cd ~/work/code/smp905

初始化編譯環(huán)境

$ source build/envsetup.sh

選擇編譯選項

$ lunch lineage_viennalte-userdebug
$ export USE_CCACHE=1

進(jìn)入應(yīng)用目錄

$ cd vendor/xxx/OpenSDKDemo/

執(zhí)行編譯

$ mm

mm 命令是定義在build/envsetup.sh中的命令,意思是編譯當(dāng)前目錄的所有modules,但不編譯它們的依賴。類似的,還有其它命令如下:

- croot:     Changes directory to the top of the tree.
- m:         Makes from the top of the tree.
- mm:        Builds all of the modules in the current directory, but not their dependencies.
- mmm:       Builds all of the modules in the supplied directories, but not their dependencies.
             To limit the modules being built use the syntax: mmm dir/:target1,target2.
- mma:       Builds all of the modules in the current directory, and their dependencies.
- mmma:      Builds all of the modules in the supplied directories, and their dependencies.

編譯結(jié)果

Rom編譯中,so文件和apk文件分開存放,首先進(jìn)入app目錄

$ cd out/target/product/viennalte/system/app/OpenSDKDemo/

就能看到剛剛編譯生成的apk文件OpenSDKDemo.apk。同樣,進(jìn)入lib目錄查看編譯生成的so文件

$ croot
$ cd out/target/product/viennalte/system/lib/
$ ls | grep kaola

就能看到剛剛編譯生成的so文件

libkaolafmffmpeg.so
libkaolafmplayer.so
libkaolafmsdl.so
libkaolafmutil.so

第五部分:運(yùn)行應(yīng)用

通過這種方式編譯出來的apk,可以通過adb install 安裝,但apk中是沒有so庫的,所以一般會通過adb push 的方式,把a(bǔ)pk和so推到對應(yīng)的目錄。

注意,當(dāng)前命令行的adb指向的是源碼中的adb,如果要用系統(tǒng)的adb,可以新起一個命令行。
$ adb devices 查看設(shè)備,如果出現(xiàn)下面的錯誤,加sudo執(zhí)行。

List of devices attached
* daemon not running; starting now at tcp:5037
* daemon started successfully
1686620c    no permissions (user in plugdev group;

加sudo方式,然后adb devices就能看見可用設(shè)備了。

$ sudo $(which adb) kill-server
$ sudo $(which adb) start-server

因?yàn)槲覀兙幾g的是userdebug版本,自帶root權(quán)限,所以可以通過下面的命令重新加載

$ adb root
$ adb remount

驗(yàn)證是否成功,進(jìn)入adb shell,如果是shell以#開始,說明是成功了,例如:

huihg@huihg-ThinkPad-T440p:~/work/code/smp905/out/target/product/viennalte/system/lib$ adb shell
viennalte:/ # 

先push so文件

huihg@huihg-ThinkPad-T440p:~/work/code/smp905/out/target/product/viennalte/system/lib$ adb push libkaolafmffmpeg.so /system/lib/
libkaolafmffmpeg.so: 1 file pushed. 8.8 MB/s (2286180 bytes in 0.246s)
huihg@huihg-ThinkPad-T440p:~/work/code/smp905/out/target/product/viennalte/system/lib$ adb push libkaolafmplayer.so /system/lib/
libkaolafmplayer.so: 1 file pushed. 2.3 MB/s (136920 bytes in 0.057s)
huihg@huihg-ThinkPad-T440p:~/work/code/smp905/out/target/product/viennalte/system/lib$ adb push libkaolafmsdl.so /system/lib/
libkaolafmsdl.so: 1 file pushed. 2.9 MB/s (161236 bytes in 0.052s)
huihg@huihg-ThinkPad-T440p:~/work/code/smp905/out/target/product/viennalte/system/lib$ adb push libkaolafmsdl.so /system/lib/
libkaolafmsdl.so: 1 file pushed. 17.1 MB/s (161236 bytes in 0.009s)
huihg@huihg-ThinkPad-T440p:~/work/code/smp905/out/target/product/viennalte/system/lib$ adb push libkaolafmutil.so /system/lib/
libkaolafmutil.so: 1 file pushed. 0.4 MB/s (9364 bytes in 0.022s)

然后push apk

注意,要先在pad的system/app/下面建一個和apk文件名相同的文件夾OpenSDKDemo

viennalte:/ # mkdir system/app/OpenSDKDemo

push:

huihg@huihg-ThinkPad-T440p:~/work/code/smp905/out/target/product/viennalte/system/app$ adb push OpenSDKDemo/OpenSDKDemo.apk /system/app/OpenSDKDemo/
OpenSDKDemo/OpenSDKDemo.apk: 1 file pushed. 7.9 MB/s (3962620 bytes in 0.477s)

push 完apk和so后,桌面上并沒有應(yīng)用圖標(biāo),這時候可以重啟一下設(shè)備,簡單起見,可以重啟桌面或system_server,例如:

huihg@huihg-ThinkPad-T440p:~/work/code/smp905/out/target/product/viennalte/system/app$ adb shell ps | grep system_server
system    835   407   1794128 132620 sys_epoll_ b6297054 S system_server
huihg@huihg-ThinkPad-T440p:~/work/code/smp905/out/target/product/viennalte/system/app$ adb shell kill 835

就能在桌面看到應(yīng)用了,點(diǎn)擊啟動即可。

如何缷載

push的應(yīng)用是被當(dāng)成是系統(tǒng)應(yīng)用,不能直接缷載,可以進(jìn)入shell,執(zhí)行remove 把a(bǔ)pk 刪除了即可。

第六部分:補(bǔ)充LineageOS 編譯

這是關(guān)于第一,第二部分的補(bǔ)充,因?yàn)榍懊娴牟襟E主要以應(yīng)用開發(fā)為主,源碼的編譯運(yùn)行沒有詳細(xì)展開,所以這里補(bǔ)充 三星Galaxy Note 12.2平板SM-P905LineageOS 編譯,刷機(jī)。

下載LineageOS

前面提到了,光有純粹的LineageOS源碼還不足以支持ROM的編譯運(yùn)行,還需要硬件相關(guān)的專有庫才能進(jìn)行。

  1. 創(chuàng)建代碼下載目錄

我們稱這個目錄為平臺目錄

huihg@huihg-ThinkPad-T440p:~/work/code/smp905$

2. 將repo的源修改為清華的源,提升下載速度

打開~/.bashrc, 在結(jié)尾加入下面的配置。(其中第一行是修改源,其余兩行后面要用,一并加好。)

export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
export USE_CCACHE=1
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"

3. 初始化repo

Repo的安裝及介紹可以參考這篇文章。確保下面的命名執(zhí)行成功:

$ mkdir ~/.bin
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/.bin/repo
$ chmod a+x ~/.bin/repo

并將~/.bin/添加到~/.bashrc中的PATH中,例如:``

Linux中.開頭的文件是隱藏文件,并沒有其它意思,所以這里只要文件夾名稱一致即可,命名可以隨意的。

PATH=~/.bin:$PATH

進(jìn)入平臺目錄,執(zhí)行

$ repo init -u git://github.com/LineageOS/android.git -b cm-14.1

4. 將lineageaosp的源換成清華的源

這個命令執(zhí)行后,會在平臺目錄生成一個.repo目錄,這是一個隱藏目錄,里面有一個manifest.xml軟鏈接,打開這個文件,就能發(fā)現(xiàn)它記錄了源碼所包含的具體工程,以及源碼的遠(yuǎn)程地址。想要知道這個文件的具體含意,閱讀這篇文章,我們要做的只是修改.repo/manifest.xml, 將相關(guān)的下載源改為清華的鏡像,可以參考這篇文章,也可以不用參考,將.repo/manifest.xml中的幾個remote改成如下的樣子

<remote  name="github"
           fetch="https://github.com/" />

  <remote  name="lineage"
           fetch="https://mirrors.tuna.tsinghua.edu.cn/git/lineageOS/"
           review="review.lineageos.org" />


  <remote  name="private"
           fetch="ssh://git@github.com" />

    <remote  name="aosp"
           fetch="https://aosp.tuna.tsinghua.edu.cn"
           review="android-review.googlesource.com"
           revision="refs/tags/android-7.1.2_r36" />
  1. 增加硬件相關(guān)庫的代碼

創(chuàng)建.repo/local_manifests/roomservice.xml文件,把下面的內(nèi)容保存到這個文件中

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
  <project name="Valera1978/android_device_samsung_viennalte" path="device/samsung/viennalte" remote="github" revision="cm14.0_alt" />
  <project name="Valera1978/android_kernel_samsung_msm8974" path="kernel/samsung/msm8974" remote="github" />
  <project name="Valera1978/android_vendor_samsung_viennalte" path="vendor/samsung/viennalte" remote="github" revision="cm14.0_alt" />
  <project name="LineageOS/android_external_sony_boringssl-compat" path="external/sony/boringssl-compat" remote="github" />
  <project name="LineageOS/android_device_samsung_msm8974-common" path="device/samsung/msm8974-common" remote="github" />
  <project name="LineageOS/android_device_qcom_common" path="device/qcom/common" remote="github" />
  <project name="LineageOS/android_device_samsung_qcom-common" path="device/samsung/qcom-common" remote="github" />
  <project name="LineageOS/android_hardware_samsung" path="hardware/samsung" remote="github" />
  <project name="LineageOS/android_external_stlport" path="external/stlport" remote="github" />
  <project name="LineageOS/android_packages_resources_devicesettings" path="packages/resources/devicesettings" remote="github" />
</manifest>
  1. 下載代碼

下面的任意一條命令都可以,推薦使用第一條,-c代表只下載當(dāng)前的分支,這一步是下載源代碼,需要好的網(wǎng)絡(luò),也需要時間,過程中如果出錯,重新執(zhí)行這個命令,如果卡住不動,ctrl + c終止,重新執(zhí)行這個命令。

#1:
$ repo sync
#2:
$ repo sync -c -j4
  1. 編譯代碼

完成上一步以后,說明代碼已經(jīng)下載完成了。可以開始編譯了

7.1 給所有項目起一個分支,可選

$ repo start cm14 --all

7.2 然后在平臺目錄執(zhí)行下面的命令

$ source build/envsetup.sh

7.3 然后執(zhí)行

$ brunch viennalte

$ lunch lineage_viennalte-userdebug export USE_CCACHE=1 make -j10 bacon

開始編譯,大概需要3個小時。

雖然網(wǎng)上的資料這樣寫,但我在實(shí)驗(yàn)的時候,發(fā)現(xiàn)這兩個命令的結(jié)果不相同的,其中有一個能生成刷機(jī)的zip包,好像是第二個命令,但這兩個命令能反復(fù)執(zhí)行,不沖突。

  1. 編譯成果

平臺目錄下,進(jìn)入out/target/product/viennalte,就能看見編譯生成的lineage-14.1-20181106-UNOFFICIAL-viennalte.zip,它就是所謂的ROM了。還有recovery.img,在刷機(jī)的時候會用到。

  1. 編譯過程中遇到的問題

編譯過程中遇到問題,要分析出現(xiàn)問題的地方,如果是某個項目例如proprietary報錯,進(jìn)入這個項目,執(zhí)行 git checkout 將其還原,然后執(zhí)行前面的7.3。

在只有8G內(nèi)存的情況下編譯,有out of memory的錯誤,執(zhí)行 gedit ~/.jack-settings,加入下面一行

JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"

然后在平臺目錄重啟jack,再執(zhí)行前面的7.3

$ prebuilts/sdk/tools/jack-admin kill-server
$ prebuilts/sdk/tools/jack-admin start-server

You’ve done it! Welcome to the elite club of self-builders. You’ve built your operating system from scratch, from the ground up. You are the master/mistress of your domain… and hopefully you’ve learned a bit on the way and had some fun too.

Samsung Galaxy Note Pro 12.2 Wi-Fi 刷機(jī)

刷機(jī)可以根據(jù)LineageOS Wiki刷機(jī)教程, 不同的手機(jī)刷法不一樣,刷機(jī)有風(fēng)險,謀定而后動。

大致說明如下:

按鍵

Volume Down (靠近電源鍵)+ Home + Power進(jìn)入下載模式。
Home + Volume Up + Power 進(jìn)入 recovery 模式

先用odin刷入twrp recovery固件

How to Install TWRP Samsung Galaxy Note Pro 12.2 Qualcomm LTE SM-P905

follow this steps :

1. Download TWRP file Samsung Galaxy Note Pro 12.2 Qualcomm LTE SM-P905 from https://dl.twrp.me/viennaltexx/ (twrp-2.8.7.0-viennaltexx.img.tar 11.6M)

2. Download Odin v3.09 or the latest Odin version
(如果失敗,嘗試一下其它版本的Odin)

3. Extract Odin ZIP file

4. Open Odin

5. Reboot Phone in Download Mode ( Turn Power off the phone, Press and hold Volume Down key (靠近電源鍵)and Home key, While pressing these keys press and hold Power key, Release all key when Download Mode / Odin Mode appears )

6. Connect the phone to PC with USB Cable Data, then Press and release Volume Up key

7. Wait until you get a blue sign in Odin

8. Add the TWRP .tar file to AP / PDA

9. Make sure re-partition is NOT ticked

10. Click the start button, sit back and wait few minutes

安裝lineageOS固件Installing LineageOS from recovery

1. Download the LineageOS install package that you’d like to install or build the package yourself.

  • Optionally, download 3rd party application packages such as Google Apps (use the arm architecture)

2. Place the LineageOS .zip package, as well as any other .zip packages on the root of /sdcard:

  • Using adb: adb push filename.zip /sdcard/

  • You can use any method you are comfortable with. adb is universal across all devices, and works both in Android and recovery mode, providing USB debugging is enabled.

3. If you aren’t already in recovery, reboot into recovery:

  • With the device powered off, hold Home + Volume Up + Power.

4. (Optional, but recommended): Select the Backup button to create a backup.
(Backup all partitions (it least efs) and store somewhere - it need to do - because you can loose imei)

5. Select Wipe and then Advanced Wipe.

6. Select Cache, System and Data partitions to be wiped and then Swipe to Wipe.

7. Go back to return to main menu, then select Install.

8. Navigate to /sdcard, and select the LineageOS .zip package.

9. Follow the on-screen prompts to install the package.

10. (Optional): Install any additional packages using the same method. NOTE: If you want any Google Apps on your device, you must follow this step before the first reboot!

11. (Optional): Root the device by installing the LineageOS su add-on (use the arm package) or using any other method you prefer.

12. Once installation has finished, return to the main menu, select Reboot, and then System.

Root

Settings -> About tablet -> press 7 times "Build number"

Then Settings -> Developer options -> Root access

Work

Almost all work: calls, mobile data, wifi, bluetooth, sound, vibra, camera (photo), sdcard, mtp, otg, pen, gps, ir

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容