1:android平臺編譯本身編寫的framework級service server實錄html
http://blog.sina.com.cn/s/blog_68f928c10101a4nl.html
2:Android Framework學習——如何加入並編譯自定義的模塊java
http://blog.csdn.net/lylwo317/article/details/53456462 Android開發:Framework添加新模塊 http://www.360doc.com/content/14/0308/22/3700464_358895265.shtml Android5.1.1源碼 - 在Framework中添加自定義系統服務 http://blog.csdn.net/earbao/article/details/68955330 Android5.1.1源碼 - 在Framework中添加自定義系統服務 https://jaq.alibaba.com/community/art/show?spm=a313e.7916646.24000001.64.1azB9G&articleid=342 Android6.0源碼添加自定義系統服務 https://www.cnblogs.com/liam999/p/5933827.html Android FrameWork 學習之Android 系統源碼調試 (**_我能看明白如何在studio中導入源代碼進行調試_**) https://www.cnblogs.com/liumce/p/8027559.html
3:Android Framework系統服務詳解node
http://blog.csdn.net/u012169524/article/details/51264979
操做步驟:見上面3:Android Framework系統服務詳解 1):linux
joe@joe-Aspire-Z3730:/media/sdb4/aosp$ source build/envsetup.sh including device/asus/tilapia/vendorsetup.sh including device/asus/grouper/vendorsetup.sh including device/asus/deb/vendorsetup.sh including device/asus/flo/vendorsetup.sh including device/asus/fugu/vendorsetup.sh including device/lge/mako/vendorsetup.sh including device/lge/hammerhead/vendorsetup.sh including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh including device/generic/mini-emulator-x86/vendorsetup.sh including device/generic/mini-emulator-x86_64/vendorsetup.sh including device/generic/mini-emulator-arm64/vendorsetup.sh including device/generic/mini-emulator-mips/vendorsetup.sh including device/htc/flounder/vendorsetup.sh including device/moto/shamu/vendorsetup.sh including device/samsung/manta/vendorsetup.sh including sdk/bash_completion/adb.bash joe@joe-Aspire-Z3730:/media/sdb4/aosp$ popd /media/sdb4/aosp/frameworks/base
2):android
joe@joe-Aspire-Z3730:/media/sdb4/aosp$ cd /media/sdb4/aosp/frameworks/base 查看系統緩存和交換分區,以便後面高速編譯模塊 joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ echo $USE_CCACHE 1 joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ ccache -s cache directory /home/joe/.ccache cache hit (direct) 30 cache hit (preprocessed) 0 cache miss 29410 called for link 1241 called for preprocessing 9 preprocessor error 1159 unsupported source language 819 no input file 222 files in cache 79387 cache size 7.9 Gbytes max cache size 50.0 Gbytes joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ swapon -s Filename Type Size Used Priority /dev/sdb2 partition 488444 0 -1 joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ swapon ~/swap1 swapon: /home/joe/swap1: swapon failed: Operation not permitted joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ sudo swapon ~/swap1 [sudo] password for joe: joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ swapon -s Filename Type Size Used Priority /dev/sdb2 partition 488444 0 -1 /home/joe/swap1 file 2047996 0 -2
3):shell
joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ mm ============================================ PLATFORM_VERSION_CODENAME=REL PLATFORM_VERSION=5.1.1 TARGET_PRODUCT=full TARGET_BUILD_VARIANT=eng TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=arm TARGET_ARCH_VARIANT=armv7-a TARGET_CPU_VARIANT=generic TARGET_2ND_ARCH= TARGET_2ND_ARCH_VARIANT= TARGET_2ND_CPU_VARIANT= HOST_ARCH=x86_64 HOST_OS=linux HOST_OS_EXTRA=Linux-3.11.0-15-generic-x86_64-with-Ubuntu-12.04-precise HOST_BUILD_TYPE=release BUILD_ID=LMY47Z OUT_DIR=out ============================================ *** Build configuration changed: "sdk-eng-{en_US,ar_EG,ar_IL,bg_BG,ca_ES,cs_CZ,da_DK,de_AT,de_CH,de_DE,de_LI,el_GR,en_AU,en_CA,en_GB,en_IE,en_IN,en_NZ,en_SG,en_US,en_ZA,es_ES,es_US,fi_FI,fr_BE,fr_CA,fr_CH,fr_FR,he_IL,hi_IN,hr_HR,hu_HU,id_ID,it_CH,it_IT,ja_JP,ko_KR,lt_LT,lv_LV,nb_NO,nl_BE,nl_NL,pl_PL,pt_BR,pt_PT,ro_RO,ru_RU,sk_SK,sl_SI,sr_RS,sv_SE,th_TH,tl_PH,tr_TR,uk_UA,vi_VN,zh_CN,zh_TW}" -> "full-eng-{en_US,en_US,cs_CZ,da_DK,de_AT,de_CH,de_DE,de_LI,el_GR,en_AU,en_CA,en_GB,en_NZ,en_SG,eo_EU,es_ES,fr_CA,fr_CH,fr_BE,fr_FR,it_CH,it_IT,ja_JP,ko_KR,nb_NO,nl_BE,nl_NL,pl_PL,pt_PT,ru_RU,sv_SE,tr_TR,zh_CN,zh_HK,zh_TW,am_ET,hi_IN,en_US,en_AU,en_IN,fr_FR,it_IT,es_ES,et_EE,de_DE,nl_NL,cs_CZ,pl_PL,ja_JP,zh_TW,zh_CN,zh_HK,ru_RU,ko_KR,nb_NO,es_US,da_DK,el_GR,tr_TR,pt_PT,pt_BR,rm_CH,sv_SE,bg_BG,ca_ES,en_GB,fi_FI,hi_IN,hr_HR,hu_HU,in_ID,iw_IL,lt_LT,lv_LV,ro_RO,sk_SK,sl_SI,sr_RS,uk_UA,vi_VN,tl_PH,ar_EG,fa_IR,th_TH,sw_TZ,ms_MY,af_ZA,zu_ZA,am_ET,hi_IN,en_XA,ar_XB,fr_CA,km_KH,lo_LA,ne_NP,si_LK,mn_MN,hy_AM,az_AZ,ka_GE,my_MM,mr_IN,ml_IN,is_IS,mk_MK,ky_KG,eu_ES,gl_ES,bn_BD,ta_IN,kn_IN,te_IN,uz_UZ,ur_PK,kk_KZ,normal}" *** Forcing "make installclean"... *** rm -rf out/target/product/generic/data/* out/target/product/generic/data-qemu/* out/target/product/generic/userdata-qemu.img out/host/linux-x86/obj/NOTICE_FILES out/host/linux-x86/sdk out/target/product/generic/*.img out/target/product/generic/*.ini out/target/product/generic/*.txt out/target/product/generic/*.xlb out/target/product/generic/*.zip out/target/product/generic/kernel out/target/product/generic/data out/target/product/generic/skin out/target/product/generic/obj/APPS out/target/product/generic/obj/NOTICE_FILES out/target/product/generic/obj/PACKAGING out/target/product/generic/recovery out/target/product/generic/root out/target/product/generic/system out/target/product/generic/vendor out/target/product/generic/oem out/target/product/generic/dex_bootjars out/target/product/generic/obj/JAVA_LIBRARIES out/target/product/generic/obj/FAKE out/target/product/generic/obj/EXECUTABLES/adbd_intermediates out/target/product/generic/obj/STATIC_LIBRARIES/libfs_mgr_intermediates out/target/product/generic/obj/EXECUTABLES/init_intermediates out/target/product/generic/obj/ETC/mac_permissions.xml_intermediates out/target/product/generic/obj/ETC/sepolicy_intermediates out/target/product/generic/obj/ETC/init.environ.rc_intermediates *** Done with the cleaning, now starting the real build. find: `phone/java': No such file or directory find: `phone/java': No such file or directory make: Entering directory `/media/46bb100d-2505-4025-8425-34ecf3129209/aosp' Aidl: framework <= frameworks/base/core/java/android/service/test/IMyTestManager.aidl target Java: framework (out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes) 。。。。。。。。。。。。 Copying: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/noproguard.classes.jar target Dex: framework Copying: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.dex target Jar: framework (out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/javalib.jar) Notice file: frameworks/base/NOTICE -- out/target/product/generic/obj/NOTICE_FILES/src//system/framework/framework.jar.txt make: *** No rule to make target `out/target/product/generic/system/framework/framework-res.apk', needed by `out/target/product/generic/system/framework/framework.jar'. Stop. make: Leaving directory `/media/46bb100d-2505-4025-8425-34ecf3129209/aosp' #### make failed to build some targets (01:18 (mm:ss)) #### 雖然出現了錯誤,可是在/media/sdb4/aosp/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/service/test/文件夾下出現了二個文件: IMyTestManager.java IMyTestManager.P IMyTestManager.java文件內容以下: /* * This file is auto-generated. DO NOT MODIFY. * Original file: frameworks/base/core/java/android/service/test/IMyTestManager.aidl */ package android.service.test; public interface IMyTestManager extends android.os.IInterface { /** Local-side IPC implementation stub class. */ public static abstract class Stub extends android.os.Binder implements android.service.test.IMyTestManager { private static final java.lang.String DESCRIPTOR = "android.service.test.IMyTestManager"; /** Construct the stub at attach it to the interface. */ public Stub() { this.attachInterface(this, DESCRIPTOR); } /** * Cast an IBinder object into an android.service.test.IMyTestManager interface, * generating a proxy if needed. */ public static android.service.test.IMyTestManager asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof android.service.test.IMyTestManager))) { return ((android.service.test.IMyTestManager)iin); } return new android.service.test.IMyTestManager.Stub.Proxy(obj); } @Override public android.os.IBinder asBinder() { return this; } @Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_sayHello: { data.enforceInterface(DESCRIPTOR); java.lang.String _result = this.sayHello(); reply.writeNoException(); reply.writeString(_result); return true; } case TRANSACTION_priFunction: { data.enforceInterface(DESCRIPTOR); int _result = this.priFunction(); reply.writeNoException(); reply.writeInt(_result); return true; } } return super.onTransact(code, data, reply, flags); } private static class Proxy implements android.service.test.IMyTestManager { private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) { mRemote = remote; } @Override public android.os.IBinder asBinder() { return mRemote; } public java.lang.String getInterfaceDescriptor() { return DESCRIPTOR; } @Override public java.lang.String sayHello() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); java.lang.String _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_sayHello, _data, _reply, 0); _reply.readException(); _result = _reply.readString(); } finally { _reply.recycle(); _data.recycle(); } return _result; } @Override public int priFunction() throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); int _result; try { _data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_priFunction, _data, _reply, 0); _reply.readException(); _result = _reply.readInt(); } finally { _reply.recycle(); _data.recycle(); } return _result; } } static final int TRANSACTION_sayHello = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0); static final int TRANSACTION_priFunction = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1); } public java.lang.String sayHello() throws android.os.RemoteException; public int priFunction() throws android.os.RemoteException; } IMyTestManager.P文件內容以下: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/service/test/IMyTestManager.java: \ frameworks/base/core/java/android/service/test/IMyTestManager.aidl
4):建立新的系統服務類 frameworks/base/core/java/android/service/test/MyTestManagerService.javaapi
package android.service.test; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class MyTestManagerService extends IMyTestManager.Stub{ private static final String TAG = "MyTestManagerService"; private Context mContext; public MyTestManagerService(Context context){ mContext = context; } @Override public String sayHello() throws RemoteException{ Log.d(TAG,"sayHello()"); return "Hello AIDL"; } @Override public int priFunction() throws RemoteException{ Log.d(TAG,"priFunction()"); return 0; } }
5:建立系統服務公開接串口管理類 frameworks/base/core/java/android/service/test/MyTestManager.java緩存
package android.service.test; import android.os.RemoteException; import android.util.Log; import android.service.test.IMyTestManager; public class MyTestManager{ private static final TAG="MyTestManager"; private IMyTestManager mService; public MyTestManager(IMyTestManager service){ mService = service; } public String sayHello() throws RemoteException{ Log.d(TAG,"sayHello()"); return mService.sayHello(); } }
6:將建立的服務添加進ServiceManager 文件:frameworks/base/services/java/com/android/server/SystemServer.javabash
import android.service.test.MyTestManagerService; private void startOtherServices()函數中 AudioService audioService = null; MyTestManagerService mTestService = null; 。。。。。。 try{ Slog.i(TAG,"MyTestManager Service"); mTestService = new MyTestManagerService(context); ServiceManager.addService(Context.MYTEST_SERVICE,mTestService); }catch(Throwable e){ reportWtf("starting MyTestManager",e); }
7:代碼中的Context.MYTEST_SERVICE服務常量添加 路徑:frameworks/base/core/java/android/content/Context.javaapp
/** @hide */ @StringDef({ AUDIO_SERVICE, MYTEST_SERVICE, 增長這行代碼在@StringDef註解中 另外還在代碼中 public static final String MYTEST_SERVICE = "mytest";
8:註冊服務 路徑:frameworks/base/core/java/android/app/ContextImpl.java
import android.service.test.IMyTestManager; import android.service.test.MyTestManager; 在靜態代碼塊中: registerService(AUDIO_SERVICE, new ServiceFetcher() { public Object createService(ContextImpl ctx) { return new AudioManager(ctx); }}); registerService(MYTEST_SERVICE,new ServiceFetcher(){ public Object createService(ContextImpl ctx){ IBinder iBinder = ServiceManager.getService(Context.MYTEST_SERVICE); if(iBinder == null){ return null; } IMyTestManager service = IMyTestManager.Stub.asInterface(iBinder); return new MyTestManager(service); } });
另外,還要在aosp/external/sepolicy/service_contexts文件中在中間增長一行
(audio u:object_r:system_server_service:s0的下面)
mytest u:object_r:system_server_service:s0
9:我認爲的編譯方式(可能個人理解不正確)
joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ mm ============================================ PLATFORM_VERSION_CODENAME=REL PLATFORM_VERSION=5.1.1 TARGET_PRODUCT=full TARGET_BUILD_VARIANT=eng TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=arm TARGET_ARCH_VARIANT=armv7-a TARGET_CPU_VARIANT=generic TARGET_2ND_ARCH= TARGET_2ND_ARCH_VARIANT= TARGET_2ND_CPU_VARIANT= HOST_ARCH=x86_64 HOST_OS=linux HOST_OS_EXTRA=Linux-3.11.0-15-generic-x86_64-with-Ubuntu-12.04-precise HOST_BUILD_TYPE=release BUILD_ID=LMY47Z OUT_DIR=out ============================================ find: `phone/java': No such file or directory find: `phone/java': No such file or directory make: Entering directory `/media/46bb100d-2505-4025-8425-34ecf3129209/aosp' make: *** No rule to make target `out/target/product/generic/system/framework/framework-res.apk', needed by `out/target/product/generic/system/framework/framework.jar'. Stop. make: Leaving directory `/media/46bb100d-2505-4025-8425-34ecf3129209/aosp' #### make failed to build some targets (1 seconds) ####
10:
joe@joe-Aspire-Z3730:/media/sdb4/aosp$ make update-api ============================================ PLATFORM_VERSION_CODENAME=REL PLATFORM_VERSION=5.1.1 TARGET_PRODUCT=full TARGET_BUILD_VARIANT=eng TARGET_BUILD_TYPE=release TARGET_BUILD_APPS= TARGET_ARCH=arm TARGET_ARCH_VARIANT=armv7-a TARGET_CPU_VARIANT=generic TARGET_2ND_ARCH= TARGET_2ND_ARCH_VARIANT= TARGET_2ND_CPU_VARIANT= HOST_ARCH=x86_64 HOST_OS=linux HOST_OS_EXTRA=Linux-3.11.0-15-generic-x86_64-with-Ubuntu-12.04-precise HOST_BUILD_TYPE=release BUILD_ID=LMY47Z OUT_DIR=out ============================================ including ./abi/cpp/Android.mk ... including ./art/Android.mk ... including ./bionic/Android.mk ... 。。。。。 Copying: out/target/common/obj/JAVA_LIBRARIES/android-support-v7-mediarouter_intermediates/classes.jar target Java: android-support-v7-palette (out/target/common/obj/JAVA_LIBRARIES/android-support-v7-palette_intermediates/classes) Copying: out/target/common/obj/JAVA_LIBRARIES/android-support-v7-palette_intermediates/classes-jarjar.jar Copying: out/target/common/obj/JAVA_LIBRARIES/android-support-v7-palette_intermediates/emma_out/lib/classes-jarjar.jar Copying: out/target/common/obj/JAVA_LIBRARIES/android-support-v7-palette_intermediates/classes.jar Docs droiddoc: out/target/common/docs/doc-comment-check DroidDoc took 264 sec. to write docs to out/target/common/docs/doc-comment-check Copying current.txt Copying removed.txt Docs droiddoc: out/target/common/docs/system-api-stubs DroidDoc took 12 sec. to write docs to out/target/common/docs/system-api-stubs Copying system-current.txt Copying system-removed.txt #### make completed successfully (12:02 (mm:ss)) ####
11:
joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ mmm core/res/ 。。。。 warning: string 'wifi_display_notification_title' is missing 87 required localizations: af_ZA am_ET ar_EG az_AZ bg_BG bn_BD ca_ES cs_CZ da_DK de_AT de_CH de_DE de_LI el_GR en_AU en_CA en_GB en_IN en_NZ en_SG en_US eo_EU es_ES es_US et_EE eu_ES fa_IR fi_FI fr_BE fr_CA fr_CH fr_FR gl_ES hi_IN hr_HR hu_HU hy_AM in_ID is_IS it_CH it_IT iw_IL ja_JP ka_GE kk_KZ km_KH kn_IN ko_KR ky_KG lo_LA lt_LT lv_LV mk_MK ml_IN mn_MN mr_IN ms_MY my_MM nb_NO ne_NP nl_BE nl_NL pl_PL pt_BR pt_PT rm_CH ro_RO ru_RU si_LK sk_SK sl_SI sr_RS sv_SE sw_TZ ta_IN te_IN th_TH tl_PH tr_TR uk_UA ur_PK uz_UZ vi_VN zh_CN zh_HK zh_TW zu_ZA warning: no entries written for dimen/password_keyboard_height (0x010500a8) Notice file: frameworks/base/core/res/NOTICE -- out/target/product/generic/obj/NOTICE_FILES/src//system/framework/framework-res.apk.txt Install: out/target/product/generic/system/framework/framework-res.apk make: Leaving directory `/media/46bb100d-2505-4025-8425-34ecf3129209/aosp' #### make completed successfully (30 seconds) ####
12:
joe@joe-Aspire-Z3730:/media/sdb4/aosp/frameworks/base$ mmm -B 由於代碼寫錯了,從新修改源代碼,回家以後再重啓電腦
13:開啓swap ,從新配置source build/envsetup 及lunch 以後
joe@joe-Aspire-Z3730:/media/sdb4/aosp$ make -j8 ....... Target system fs image: out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img Running: mkuserimg.sh out/target/product/generic/system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img ext4 system 576716800 out/target/product/generic/root/file_contexts make_ext4fs -T -1 -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/obj/PACKAGING/systemimage_intermediates/system.img out/target/product/generic/system Creating filesystem with parameters: Size: 576716800 Block size: 4096 Blocks per group: 32768 Inodes per group: 7040 Inode size: 256 Journal blocks: 2200 Label: Blocks: 140800 Block groups: 5 Reserved block group size: 39 Created filesystem with 1467/35200 inodes and 109536/140800 blocks Install system fs image: out/target/product/generic/system.img out/target/product/generic/system.img+ maxsize=588791808 blocksize=2112 total=576716800 reserve=5947392 #### make completed successfully (13:34 (mm:ss)) ####
說明編譯成功了,此時須要用模擬器來看看實驗結果。先要編譯SDK。
14:lunch sdk-eng而後再:
joe@joe-Aspire-Z3730:/media/sdb4/aosp$ make sdk 。。。。。 Package SDK: out/host/linux-x86/sdk/sdk/android-sdk_eng.joe_linux-x86.zip SDK: warning: including GNU target out/target/product/generic/system/lib/libexif.so SDK: warning: including GNU target out/target/product/generic/system/lib/libgccdemangle.so SDK: warning: including GNU target out/target/product/generic/system/lib/libnl.so #### make completed successfully (01:02:57 (hh:mm:ss)) #### 編譯完以後會在/media/sdb4/aosp/out/host/linux-x86/sdk/sdk/ 目錄下產生一個android-sdk_eng.joe_linux-x86子目錄及zip壓縮文件,這個壓縮文件其實就是android-sdk_eng.joe_linux-x86目錄壓縮而成。之後開發時就能夠用這個子目錄或zip壓縮文件(解壓)以後做爲SDK使用。
15:joe@joe-Aspire-Z3730:/media/sdb4/aosp$ make從新編譯系統以後啓動模擬器,用adb shell啓動shell, 而後root@generic:/system/lib # service list
26 mytest: [android.service.test.IMyTestManager]
針對步驟9中出現的錯誤,網上有人說:https://bbs.csdn.net/topics/370150557
1.先在framework/base目錄下執行 mmm core/res/ 生成framework-res.apk 2.接着在framework/base下繼續執行 mm -B 便可生成Install: out/target/product/generic/system/framework/framework.jar
簡化方法:
解決了個人問題,不過我簡化了一下: mmm frameworks/base/core/res/ mmm frameworks/base/
備註: 網上有人說增長了系統服務以後,須要從新編譯再啓動服務,我尚未到這一步,先記錄下來,以備忘記
. 測試 make make update-api 更新current.xml文件 生成system.imz文件,放到<ANDROID_SDK>/platform/android-20/images/目錄下, adb shell service list
還有人說:https://www.2cto.com/kf/201709/687510.html
回到根目錄下執行make update-api,不然編譯不能經過。先編譯framework.jar,而後編譯service.jar,最後編譯本身加mymodule。能夠用mm命令編譯,沒問題後再整編。由於編譯出來的out目錄有boot.art和boot.oat,framework.jar和service.jar不能push調試,因此只能刷整包驗證。