 So, hello, I am Arnav, I am still a third year engineering student. So, I am going to talk about hacking the Android OS code. So, by the way before I begin I would like a quick show of hands for a couple of things. How many of you have developed Android apps before? I can know about the process so everybody. How many of you have ever worked with our experience working with the OS code the AOSP as we call it. So, yeah and so for these four or five people I think at the beginning it will be a little boring. So, let us start by you know understanding the Android OS in the beginning. I will the slides will go on for like 15-20 minutes after that we will start some hands on codes. So, before we begin I would just share a few things like this is a diagram that I think a lot of people have seen a lot of times. So, the Android architecture diagram basically so you have the applications, the application framework, the libraries and the Linux kernel. So, at basically at the bottom of this whole chart is the your hardware at the top of this chart is your user, this is how Android works. So, I would like to just give a bit of load on these things like applications. When we are I am talking about Android OS here. So, I am not talking about even the apps that you get on the play store just the OS itself already has quite a few apps like the ones you see in the launcher of default device that you get that is there other than that also a lot of things are in the format of apps that. So, apps as you can see it is written out there it is simple bundled into APKs what all of you app developers have been doing. Then just level below is your system UI or the framework all over that whichever is handling the UI part. So, this defines everything else like how the buttons look, how the checkboxes look, how switches look, how the navigation bar looks, how your lock screen looks. So, all of this is there in the system UI which is basically the Android OS's abstraction layer for the graphical side. Then the other part of the frameworks is basically your providers and APIs which whenever you are developing apps everything is abstracted to you all your hardware like you want to access the sensors. So, you have got some classes which are abstracting sensors to you you want to say access the display layer or whatever. So, all the APIs are provided to you extracted from the bottom layer to the developer via these things. So, that is also the job of the framework. Then just below the frameworks is the runtime. Runtime is till now it was the Dalvik which was very much like the Java virtual machine. So, now Android is shifting over to a newer runtime which so like quote unquote Google says it is more native. So, then below that are the libraries. Libraries can be of various types some of them are for say encryption libraries you have you have got libraries which handle the hardware which are called HL hardware abstraction layer which is a set of libraries which allow you as the developer to access the GPU, the sensors, the screen. Then finally at the bottom is the kernel which when we say that Android is based on Linux. So, it is this layer that we are talking about the one the part of the OS that actually runs the hardware. So, kernel is pretty much the default Linux kernel that is available. There are some changes like the implementation of memory and power management is different. But basically this is very much like the mainline kernel tree that you have for any device if you have Linux available it will be somewhat like that. So, now we are going over to how you can build Android for your phone. So, how many of you know anything about it like you have heard that you can build Android you can download the source on your PC and you can build that have any of you looked at the instructions I think some of you have yeah. So, just like you can build apps you can build the OS itself more time more energy more space a lot of things have to be invested a little more than app development. So, these are the kind of requirements that you need to build as you can see on the general normal laptop can surface for building we are going to download I have a copy of the source but I will show you how it is downloaded that will do this is the link that actually describes how that process is done it will take a lot of space there are in lollipop I think there are 464 separate git repositories which all get downloaded and they get inflated in a directory structure in shortly I am going to show how the directory structure looks like finally when you have to build there is a there is a mammoth build directory which has some 20 25 different mk files the make files which are handling how all these parts are built. So, it is like about 10 GB of source lot of parts selectively gets built and finally you have a 200 300 MB image that you flash on your device so so we are going to talk about what can we modify and how and we are talking about that at DroidCon 2014 two of the most important tracks I felt were one about UI UX and another about the hardware side so when you want to modify the Android source I think these two are the biggest motivating factors I mean if you are making a new tablet or a new phone you might want to differentiate your phone on the basis of the software so you might like to change the user experience that is one side of it the other side is today we have a lot of different devices coming up I mean like even one year ago it was only tablets and phones and tablets are just larger phones so now we have got smart wearables Android is coming in cars Android is going to come in home automation soon so there will be a lot of devices which probably won't have a touch screen with which you probably won't interact the way you interact with a phone or a tablet so you would want to modify Android in a certain way that you can use it like for example if you have on a watch itself the Android Wear platform works much differently from how it works on a smartphone but it is after all the code it is the Android OS source itself so how we can do that and that will involve a lot of the term we hackers call this is porting porting means to getting the Android to work on a hardware a new set of hardware so on the slides I would like to show you mostly about the UI side I can show that then I will for the hardware side I would move over to show how you can actually change the code and what you can change in the code so when you talk about the UI side if you have run some custom ROMs you will see a lot of the changes are like in the contacts app in the SMS app in the settings app these things are something that as an app developer is not going to be something new to any of you it's just the exactly the same structure as you structure is your source for an APK like back in the eclipse days how you used to do if you have moved to studio it as a different structure of code but it's like the SRC files is Java files go there the rest files it's exactly like that and the only difference is the apps that come with the OS they mostly the directly the source you cannot build in a third-party ID because they have some deep linking with the OS code but the whole idea is pretty much the same then comes the modifying of the framework which on the code side I think you don't need any other knowledge other than the Java knowledge you have already for developing apps but that is something that you have to build the whole OS every time you make changes because these are deeply integrated with the other part of the OS so here is somewhere you can make changes for example for people who have anybody has developed any app for a specific device ever say if you want to build app for a Samsung phone and you want it to be included as the multi-window thing so there is a particular API for that right so if you're building your own OS you can add API's over and above what Google provides in the default Android source so so that sometimes people want to make a ecosystem of apps for themselves so that's one thing you can do here then comes the part about modifying the the libraries which are going to access the hardware and why exactly you might want to do is one of the biggest examples is what cyanogen mod does is they have a equalizer app so to do true equalization you need to have deeper access to your audio chip wherever you want amplification and all that that's an example if you want to improve the performance of the camera also wherever you need to change how we are accessing the hardware and finally the kernel modification is something that I guess I mean at a third-party level mostly people do it to add CPU governors and all that stuff but if you're going to build a new device this is something that's very important because you probably will get whoever is your SOC or your CPU vendor will probably give you a Linux tree which will boot the normal Linux so now you have to run the Android kernel on it which has certain subtle differences from the Linux kernel so it needs a bit of a bit of vast knowledge on the kernel side to be able to do this so I will show some changes on the UI UX side quickly I will go over them and you can see on the UI UX side what are the things that you can do say for example this is some changes for a ROM called AOKP so this is something I think a lot of you know this exists on a lot of custom ROMs so you can change the size of navigation bar you can make it transparent or something like that and can have changed the number of buttons that are that are available in the navigation bar and you can add some extra keys like left right keys on navigation bar from the bottom of the screen you get a Google icon so you can add some more apps out there this is something interesting you can modify the total UI so you can see this the in IC there was a tablet mode where your status bar in your navigation bar come to the same place so maybe you can enable that on a phone so that people have more screen state or something like that these are quite a lot of changes that you can do is also something related to themes which is also a lot of people who make phones they are doing like Sony has a very nice theming service and obviously the custom community people they they they are very interested in theming and all that so there are a lot of so this is another nice thing that has been done this is you can compare it to what you have on a HTC phone you can write from the lock screen you can launch some apps so this is I bet one of the best features that somebody hacking with the Android OS code has come up with is the privacy guard so how many of you people know what privacy guard is ok so as an app developer you know what the app permissions are everybody knows that so say your app takes permission for reading contacts because you have to read contacts so if you if you download an app from the Play Store and it has the permission to read your contacts and you don't want to give your contacts away to that app what do you do any answers you can uninstall that app other than that no options so this is a very deep rooted thing that has been done it you can selectively turn on and off access to various permissions for apps so that's nice right so for doing that you will have to go down to the layer in the frameworks where permissions are given so if a certain app has a permission so that means the API is to read contacts will be exposed to it when it tries to read it if via privacy guard you said that I won't give you permission to read your contacts so it'll get a blank contacts file you have to work it around that way because if you just say that no I'm not going to show your contacts the app will crash because the app is expecting something return so it'll just see your contact book is empty right so even with the internet also so if you say the internet is not connected that will say please connect to the internet or it will give a box like that but if you say like internet is connected with the speed is 0 kbps so these kind of a changes that you can do with the OS so this is something very important for privacy conscious users most of them are lot of UI based features that I mean this is something wrong called paranoid android did what they made an API to change the external UI of the phone based on what app you are running so say you are running Facebook your navigation bar becomes blue in color you say run Google plus your navigation bar becomes red in color okay so this is also some of the things that can make the user interface and the experience of how seamlessly user users are using the phone very different then what other things you can change is I can show one of the modifications I did while I was working on a project for micro max is this also I had an issue with the video basically that's where the presentation ends so I will move on to how you can actually get the source code and you can build it so firstly let's see I mean you will need to search the internet for that actually so that's why if I'll do that mirror my screen so that I can choose yes so the link that I showed in that slide was how Google gives you instructions to build Android on your on your laptop but as a person suggestion I would say if you search this thing build Android lollipop on you want to I search this and one of the first results is a XDA thread so I personally would suggest this because there are certain steps that Google misses so this has a lot of instructions like you need to get the open JDK another thing you can note if you want to build something before lollipop you will need the Sun Java JDK which you can get only from Oracle site you won't get it by any updates on you want to in the first thing I think I didn't mention you cannot build on windows you will need Mac or a Linux and for anything above ICS you will need it 64 bit to be able to build okay so you will need a few tools in the beginning you will need like I said Java anything below lollipop you will need Java 6 and the open JDK 6 won't do you won't be able to build with open JDK 6 you will need the Sun JDK 6 then for lollipop and for any future version you will need seven you will need like Python you will need Git and this this is a whole list of packages that you will need just copy that list and paste it and install all of them before you start building then as you can see like this is a very comprehensive information set whatever kind of OS you're using 12.04 or 13.04 if they have even my instructions for Mac and all that so you will have to follow these instructions get this by the tools that you need you will need to get it ready and another thing is how many of you have used ADB on Mac or Linux have had driver issues yeah so this also I mean something not just related to Android OS development if you are an app developer you might have faced issues could have solved this you dev rules you have to do this also and so now we move on to the part where we are going to get the source so like I said the source is available like this is you can note this down you can see this android.googlesource.com this is the website where the source code of a whole of android where Google publishes it as you can see there are a whole lot of repositories all of them are separate Git repositories so to download them Google has made this nice little script called repo repo so you have to do this step curled HTTPS storage google apis.com git repo download repo and it will come on to your bin folder there will be this script called repo so what this does is this is going to help you download the whole source and inflate you inflate it in the particular order in the directories where you need it to be able to build the android OS okay how many of you have that source code right now show fans okay that's quite a few people but how many of you have access to internet right now so I would like wherever you don't have source I will show the place on internet where you can see the source that's as good as that the source code when you download it will be 25 GB in size because it has all the history right from Android 1.0 to this so you have to download the whole thing because if you want to keep in sync with the updates that Google does to the source code so that's the best way to do it is some to some people I have distributed a tar file of the source but the tar file you cannot update it via git so you after you get the repo script you should make a directory and you can make it anywhere sometimes a lot of people ask if that's possible or not yes it's possible like I have it set this way I have folder called Android inside that I've got a okp is one of the customers I work on and this is the KitKat source the logic of source I'm yet to download inside that you run this command which is sure to not terminal so the command is visible is like repo I knit you then wherever you're getting the source from and it will have a branch because if you want to and download a lollipop source you have to the branch like this so for example instead of the AOSP source I want to get the cyanogen mod source so it will be it will become the line will become something like so what exactly exists here so let's have a look at that so this repo will have a file like this yeah so it's visible now so this has basically a list like this the path shows where the source goes then the name shows from where the repository will come from and then sometimes they can have a group and revision which source is going to download so it's a long list like I said all those 464 different grid repositories they are mapped like this so you do this step your repo gets initialized and then you have to run the command to sync it when you do this the whole download of that 25 GB of source it will start ok if you have a good connection you can do j20 say 20 parallel downloads will start if you are having any issues like the connection is breaking a lot repo gets hung so you can use the force flag if any of the repositories are not downloaded will skip and continue with the others so you can check out all the other arguments that repo has after you have downloaded it the source should look like this is that visible I mean these tar files are not part of the source the folders are I would like to give a brief description of what exactly the source means ok so I start with some of the most important folders first select say ART is the new runtime as you know so this has all the source code of the runtime we will delve into it later Dalvik is the older runtime then you have inside the bootable folder this is something that is going to interest the people who probably want to get Android running on a new hardware so it will have the bootloader this is the source of a reference bootloader ok so this is not the source of the bootloader that comes on the Nexus devices because as a equipment manufacturer you need your bootloader to be secure you would not want your bootloader to be open source you would want that if somebody wants to flash their custom firmware on it they should unlock it via some like fast boot OEM unlock is a method you can do it so like that this is a source code of a reference bootloader the bootloader as you all know it's going to start up all the other systems then another thing is the recovery recovery allows two things one is obviously as the name suggests if something goes wrong you can data wipe your device from the recovery you can reflash a new build via the recovery and finally a recovery helps you in photo every you know what photo means firmware over the air like you get upgrades to your Android if you have a phone you get upgrades which are like say the your ROM that comes with a phone by default for for a OEM like Samsung Sony who do their own UI will be 700 800 MB in size but your updates are generally a hundred MB or something like that so it's a delta diff of it so these updates you want to install it on the fly that also what happens is the phone reboots into recovery mode so in the recovery your system partitions your data partitions they are not mounted recovery updates your phone and then it again boots back into the normal mode so for that you have the recovery on the recovery side the community in the community a lot of people have developed a lot of recoveries the Android recovery is very simple you can just flash files and it does not have a touch interface use the volume buttons to go up and down a power button to reboot there are people who are developed that screen recovery is the phones that come with cyanogen mode on board like the OPPO N1 and this the OnePlus now the you phone is going to come out they have that screen based recovery that cyanogen has developed so this about the recovery then the build folder I will come to it at the end because this helps you build everything this is all the build configurations the CTS folder is something it contains a lot of test suits that you need to pass to be able to consider Android certified officially Android certified so let's skip on directly to the folder called device so inside device as you can see I build a lot of custom rounds for Sony devices so that device Sony and this Yuga stands as a code name for the Xperia Z phone okay so these these contain configuration files for how the Android OS source code how it will be built so for example inside this fold so here for example if you want to build Android for a new device you will have to create something like device slash the your vendor name slash your device name inside this folder you will have some certain preset make files so you have your Android Mk is very standard template this needs to be there under every separate module that gets built you will have something called Android board Mk again this file needs to exist to to tell the Android build system that this device has its own board okay this tells the this Android products that Mk redirects to the make file where you are going to define all your configurations and then the board config file so in Android when we talk about the word board board generally means anything that relates to the kernel and you talk about the device you're talking about the Android part of it okay so you the whatever changes you are doing on the Android side the board generally so here how many of you have ever experienced working on the Linux kernel or build the Linux kernel or seen the source show off hands again so okay so not a lot of you so this certain flags like this are going to set which configuration your kernel will be built in I'll go to the kernel side again so your device will have a certain parameters like the size of the boot the size of the recovery the size of the system and the user data these have to be defined here like you can see then this is going to define how the Android side is going to be built so like we have defined the height and width of the system we are going to define the basic make file that is going to be taken so here taking a full base telephony dot mk there are a lot of product files like this in the default build folder so this is a phone that has got gsm on it so I am going to inherit the full base telephony dot mk so this is going to have the basic needs for a device which has gsm on it phone or tablet kind of device for example then I'm going to inherit whatever is specific to my device okay and then I set the name the device so this for example you have written the product model this is what comes up in the about phone screen so what device you're building it for then there after that there is the the external folder is for a lot of binaries that are present on your phone which are not part of the Android project per say like you can see here there is there are binaries for say there is for apache there is for Bluetooth so these are all sources that are part of the open source community a lot of them are from the GNU project so whatever needs you need for like supporting web servers supporting encryption supporting your hardware whatever different sources that will be needed they are mostly here most of them are based on c or c++ because these are low level codes these are these are what you have in the Linux project or the GNU then there's the frameworks folder so this really when we talk about the Android platform most of the code is in this folder okay if you want to have a look at what are the things I'm showing here what you can do is you can either go to this site okay this android.googlesource.com and you can search out the source that I am showing like there will be those of you have internet you can search out like these frameworks AV frameworks base all the frameworks source that I'm showing from my laptop you can see the exact same source available on the net either this or you can also use a mirror it's github.com slash android it's more cleanly presented on github so here also say I'll search there's a frameworks base this is the main platform source you can look around here I will just show it on my own desktop so the frameworks has a lot of files say AV is where you have all the audio and the video related code there is a DRM related code so framework is the initial diagram that I showed the framework is a layer between your libraries and your apps so it needs to talk to the libraries via obviously some lower level languages like cc++ and it needs to it needs to talk to your apps via some Java abstraction so your framework you will see most of the places it is divided into the Java source and the native source with JNI mapping across it so whatever the the main function of the framework is like it's translating whatever you're doing in Java into some lower level system calls so for example there is your media server code here as you can see these are all C codes so these are again important like these are APIs to your hardware as if you want to go into like change the hardware side if you want to build Android for a new device you will have to you will have to often change the code here because these are calling the direct functions in your hardware libraries so hardware libraries say for example you're making a new device which has a camera chip that has not been used on Linux before Android before so you will write your camera binary for it so it can have functions which are not similar to what is present on the generic camera chips you have on previous Android devices so you will have to come up here at the you will have to obviously you have to write the device driver for Linux first and then you will have to change whatever calls it makes to the camera from the frameworks AV side so that it like changing say the brightness or the exposure of your camera so if you're from whatever the function calls are there on your binary you will have to adjust it accordingly here the base is something that has a lot of stuff here so this file holds a list of all APIs that are available on Android okay so during the lollipop announcement they said that they have added 5000 new APIs you noted that so when they said 5000 new API they mean that the number of lines on this text file I mean not the number of lines but number of APIs on this file that incremented by 5000 so here all your flags all your function calls they are all documented out here the CMDs are the processes like whatever Android has added which are low level stuff like you know this is the screen cap is what this is your file that is used to record your screen which was added in KitKat recently inside frameworks base again I'll be talking about say for example the core side this has all the frameworks base core Java this has most of the classes that you extend when you are working on Android okay you say for example you are extending your activity class so the source of that actually sides up here activity.java so what exactly can you do here is some of an important thing I'd like to show so here is a particular function you can see here this was not present on and AOSP by default okay so we have this community project called AOKP which is just like cyanogen mod so this is a particular line and the update immersive mode the function has been added what it does is you can see my phone everybody knows immersive mode is they've heard of it in KitKat with immersive mode is a API that if you call it your app becomes full screen and I will show what immersive mode is this happens your navigation bar goes down you can pull it up like this in any app you go so or it becomes full screen like you see and you can pull down all of this okay so this as an app developer is available to you as an API so what we did was we changed it in the activity.java file which every activity every every app is extending it so when I have immersive mode on across the whole OS so every app is extending the activity.java which is part of my system so all the apps are becoming in immersive mode okay so this is an example of say when you want to change the user experience of the interface across all apps say you want to change the themes and all so you have to go down to the basic base implementation which everybody is extending change it globally and for everybody it changes so using that when we talk about what we can do improvements on the UX side is this is not actually this is just experimenting with it okay so when you say how do you change the experience how you change the experiences you create a list of apps that users can update which will automatically become immersive okay so we have that and what you can do is you can add the apps that you want into your auto immersive list so whenever those apps run they will run on full screen whether or not the app developer wanted it so you give power to the user to decide when some of the apps should run in full screen or not so suppose you want you as a user want to use for a Facebook full screen all the time so you make Facebook as an auto immersive app so every time Facebook opens it's always full screen then let's let's go to the so you have this folder called packages here okay so this contains a lot of apps which are part of their default system so for example you have the key guard which makes your lock screen you have the system UI is your whole the shell you have documents UI which is in KitKat they have added a new file browser kind of interface when you want to attach files you have this nice looking file browser now so these apps are there so for example everybody knows that these custom norms make these toggles right much before Android had quick settings you still have toggles for example so how you do that is like in system UI you can go and so you have your status bar this is the code that is going to run your this code compile into your status bar so you can change this you have a toggles folder you make a toggles folder what you do is this toggles folder any of the files that are in the toggles folder they are going to create toggles so you add a lot of toggles so we can talk a lot of stuff in this these are things that we have added like we have added a toggle to take a screenshot a toggle to switch on off NFC a toggle to switch between the various network modes 2g 3g 4g like that so again this can enhance the user experience if you add too many toggles it can be an irritation also but so that again is something up to your purview how you're going to do it so that was about the frameworks okay let's since I'm talking about the UI I will stick to that for a couple of more minutes and see finally if you want to change the apps that are part of the OS say for you example you want to change how the dialer works for example so they are all in the packages apps folder and this part actually you can do I mean you will have to build the OS to be able to modify the ROM image but as developer you just want to change the phone app you can just get one of these folders you can download each of one of them is a separate git repository you can download it so I will tell you something interesting if you go and try to build this it will fail to build if you do it on something like Eclipse or on studio it won't build because the APIs there is a concept of hidden APIs if some of you have worked on some apps like say before KitKat if any of you wanted to work on a app that handles SMS in Android so these APIs were not exposed directly so you go into the OS code you look at how Android's own SMS app works and you call the related functions but it's not all that easy to compile it because your SDK does not have all those APIs exposed okay so for that I have written a sorry yeah so he's a script that I had written what this does is it can create SDK which has all the hidden APIs on it so you can directly take this script for example so whatever APIs are needed all of you Android developers you know there's an Android.jar file inside the SDK so every platform has a different Android.jar inside which you get all the stubs of all the methods that are present in the system so this script it is available on this github.com slash aokp slash build folder and inside tools custom SDK gen.sh so what this script does is for the first time you will need to have the whole OS code inside the build folder if you run this script it'll create an Android.jar for you with all the hidden APIs in it so you can create a new platform in your SDK folder so like if you have your like Android 18 Android 19 Android 20 21 these are all the SDKs what you can do is you can create a new platform say you're creating the hidden APIs of KitKat create a new platform called Android 119 for example okay copy the Android 19 folder make it Android 119 inside there will be a property file change 19 to 119 and replace the Android.jar file that you find it here with the Android.jar that my script generates so then you can simply build you can modify the default OS apps on your favorite ID because that's comfortable right so you can do it on your ID and it will compile also okay and it will work on your system as such so that's something that I mean these are the kind of little hacks you need to do to be able to comfortably work on because I personally feel that you know with when I am working on Java I prefer an ID so here these are this is a list of say the default apps that you have some of the apps that I feel I for the UX perspective that people like changing are like for example the dialer the MMS app and there are something that pertains to sometimes the hardware side also because it's the NFC app and all that so now let's get to the hardware side okay this is about I told how you can change the user experience but for example if you want to build a new platform you want to build a new device so first of all this is the hardware folder inside which you need there is a source code for all the different hardware that's present on your device the device drivers like we call them so mostly for say Qualcomm based device you take the source code of the media side the display the audio all of that from code Aurora forums so you can this is this website called code Aurora forums so here there is a reference source code available for everything that's based on Qualcomm similarly most other SOC or CPU manufacturers will have a particular website where they give out their reference codes so you have to take that source code and generally if you are building a new device for example so you can start up with AOSP whatever code Aurora has been adding for example for Qualcomm if you do for other device you get your hardware code this should this should be where you start from that is one thing you have to do like I said you have to obviously go into you have to create a device slash vendor slash your device name folder you will have to set up all your configurations AOSP as such is not going to build your kernel along with the main build okay so you can build the kernel separately and provide it as a pre-built or you can use suppose for example cyanogen mods build folder so what that will do is it also builds the kernel along with the whole build so kernel gets integrated right during the time so then you put your kernel inside say kernel slash your vendor name slash your whatever chip you're using this folder is something that you can name whatever you want to again some other things that I would like to mention here are there is this folder called pre-builds this contains all the cross compilers or binaries that you need to build so when you're talking about hardware side of things the cc plus preside they have to be compiled like if you compile it on Linux it is going to be compiled for this AMD 64 or x66 architecture x it's got so you have cross compilers which are going to compile c code for ARM so that you can run it on your device you can write from your kernel to your device drivers all native code so all those stuff are present and whatever other tools that you need some of the tools like all those who have worked on the Android app side would know about tools like AAPT which packages your APKs and zip align so these tools are built on the fly actually so once all of you who will actually go home and try building it you will find first time you build takes a lot of time because first you are going to build the tools that you used to build and then you will actually build and finally there is this folder called a vendor folder which generally the reason we use it is there are a lot of parts of the system despite calling Android open source there are certain things which are not open source for example your your SOC manufacturer mostly will give some of things in binary format to you they won't give the source for that okay so there is a good example is Qualcomm makes a library called libqc OPT it's Qualcomm based optimizations you don't know what exists inside that but if you run it apparently your device and smoother so for all those things there is a vendor folder so here as you can see this is the structure of the system folder so there are a lot of libraries here that directly they will just go into your phones system folder directly because these are libraries which you will just link to so the vendor folder is mainly used for these purposes for the binaries that you are not going to compile you keep it in a vendor folder they are directly copied to your kernel the ROM image okay so let's try building it is this one visible this is probably not visible at the back I will use the this term let's say okay so when we want to build this is given that the whole source code is present with you after that you source the configuration file which is present at build slash env.sh this sets up your environment for building the whole thing okay you do this then the people at Google have very funny names they call launching when you launch it so you make your environment ready for one particular type of device okay so this is like an embedded operating system so this is not like your OS distros that you can run on any kind of hardware this will be built specifically if you build it for a particular phone it will run on that phone because there are a lot of constraints you can you don't want to use a lot of RAM you don't want to add a lot of drivers you don't want to increase the size so you just build with only those things that are needed for that phone and that phone alone and not for other phones so the launching configures it according to your device you want to build so for example I have my Xperia Z phone say I want to build for that if you just click launch and press enter you get a list of all the configurations that you have so if you have got your make files all set up then to add to the lunch menu you should you can do it at two places either wherever you have the device thing device you can add a file called vendor setup.sh either there or you can have your own vendor in the vendor folder you can have this so this runs a lot of commands to add whatever configurations you have like add lunch combo name of the device like that so this list comes up so I want to do it for the Xperia Z which is a Yuga so I'm going to select number 66 so when I launch it it is going to reset all the various configurations at various places to build for this particular device why this is needed is as app developers you will know that there is this LDPI MDPI HDPI thing okay so you don't want to bundle all these resources unnecessarily for a phone which is say HDPI you would not need the LDPI MDPI resources if you have the HDPI resources exactly the same way for LDPI phone you don't need the larger resources so this is one example there are a lot of other things like this so the launching will take care of all these things that whatever particular configuration your device is off it is going to set the build instructions in the respective way after that this this little information box will come it's going to tell you which version you're about to build whatever CPU architecture you have whatever host architecture you have and it's going to show the out directory which is where the source code is going to output into then there is the C cache directory people who have worked with C++ know about C cache how many of you don't know about C cache so C cache is like for C or C++ code it is going to keep a cache of when you build it first time it is going to keep a cache of your binaries next time when you build it does a hash mapping and the source has not changed it is not going to build it again so creating a hash file is faster than compiling it right so you should use C cache always when you're building a project as large as this because it's going to reduce a time significantly from the second build then the simple command is make you can use a job variable to set number of threads that you have on your laptop or your build machine accordingly and it is going to build as per that okay so it's going to take a lot of time to build my personal laptop I can run a build in this amount of time on my SSH server instead so that has I think 16 cores or something so I will show you whatever the output is from a build folder there then this is something that you do make and it creates the whole source code okay but then going forward you're not going to change everything all the time so you're changing it component by component so every module like I said is going to have and everything that has an Android mk file is a separate module as per the build instructions okay so for example I go to my any app let's for example let's select an app packages apps and say my camera app so it will have an Android mk file and which will have a local package name okay this variable so this defines that everything and all the source inside this folder is going to build into a module called camera 2 so what I can do is I can run make so okay there are two ways of doing this is called component by component building when you are going to change code this will be needed a lot there are two ways of doing it you can either go to the folder like here you can run a command called mmm this command is available only after you have done the build slash environment setup you have sourced it you will have a command called triple m and you can do just mmm here and it will build camera but this will work only for the dependencies are already satisfied but if you do make from out from the top then it is going to build whatever dependencies the camera package needs and then it is going to build the camera app okay this is one way to build depending on what source you want to build there is another way to build depending on what target you want to build so you can select something like make and for example the kernel is built at this path the out is the variable for the out folder inside that the kernel file is where your kernel comes out so you can do make out slash kernel and then it will build just your kernel file for you so this is by ties you are targeting it by what end product you want so beyond this see what you want to build or what you want to change is something that depends on what you want to do but this pertains to a lot of the how about you want to change things so I would like to know if any other questions you have regarding how you want to build things because I'm not going to cover anything further on the how parts I'd like to know if anything specific you want to know about how you want to build it any kind of questions you have no questions about this part mostly I do hack around and figure out okay so let's say for example you want to it does come a bit over time when you keep seeing other people changing stuff you yourself changing stuff you start learning where different code is you have a basic idea like I gave see something about the device drivers it will be in the hardware folder so you go inside hardware folder do a fine grip and whatever thing you're searching for you probably find out that way so the base classes that you are extending for changing the APIs that will always be inside frameworks base core for example okay so you get a rough idea like that then you want to change a particular class so a dot java file you will have to do something like I want to find out name a common file that is extended any common Java class that is extended while developing an app yeah activity job okay so I want to search activity.java I'll go to I'll just do fine so get all the files we wanted this file right so activity.java you find out like this you got every other file that has ending with activity.java so that's one way you find out what you want to change anything else yeah yeah kernel browsers yeah see the kernel structure will be very much like the Linux kernel yeah yeah a kernel browser to like browse the source code of the Linux kernel yeah but the problem is the Linux kernel source code for every device is quite different so the Linux kernel browser works because there is a mainline tree of Linux kernel which is common to everybody right yeah so on the Android side 90% of the source is same as the Linux kernel so your Linux kernel browser is giving documentation and the particular places where you get the source code that's going to hold true for 90% of the source code of the Linux kernel Android kernel 2 the 10% that is different is implemented vastly differently for various different devices okay so you got the idea right yeah yeah so for example I want to see browse through the code of Qualcomm seat where devices so maybe except the audio audio code the video code and the CPU code everything else is mainline Linux okay thank you so any other questions okay so the next thing that comes is obviously contributing this source code back I think that's a lot of people were asking me when I was outside it we have changed it so how do we contribute it back so for that you know a lot I think they just give a sort of personal advice a lot of people think that development pertains a lot to writing code I feel that almost 50 percent of it is also getting acquainted with the tools that you need to do it and one of them the biggest ones is your version control system so if you want to contribute back to Android to AOSP or even if you want to contribute to some somebody like Sanogen you will need to have a very good grasp over git over how repo is used because say for example you change something you push it up and then it's in review somebody says something you change something in between that something that's merged which changes the code exactly where you were working so you need to have a very good command over conflict resolution and everything because this is going to come up a lot I remember like a friend of mine submitted a code to AOSP it got merged more than a year later so two Android versions came out between that but it was a system-related code so that part had not changed a lot it was a bug fix so it lied on the gerrit of the code review of Google for more than a year and then it got merged so obviously you need to be expert in rebasing and conflict merging and all of that so I will give a quick overview on how that is done there is this yeah so this is the code review of AOSP this is on the URL android-review.google-source.org so I will show something like I want to make a change I am this particular folder does not exist on AOSP but I am doing the commands they are going to pertain to AOSP as well so here repo comes to help a lot so what you can do is say I am adding a file so I created a file called test inside this folder and this is for example this is my chain I want to contribute it so I do what you do and generally get you add the file and then you do a commit of it I did a test file and I have committed it so at this point of time what you can do is I'm sorry I had to take a step before that you have to do repo start so when you use the start command it's going to start a development branch for you okay in the same folder so you do repo start say I'm going to so you have to start it with the same name of the branch into which you want to contribute okay so I do repo start KitKat and in which folder in this folder this is the but so you can see I am on the branch KitKat now okay so I have to do the same I have to do the same thing all over again test file has gone I've added this test file now is a this is the simple command that you use is you use repo upload okay so you can run this command after you have committed a lot of changes on a lot of different sections of the code so repo will search through the whole Android tree and in whichever trees there are dangling commits it is going to upload all of them to the respective review server okay for if you are doing on AOSP for all the folders the review server will be the go Android review server if you're working on some custom ROMs or something sometimes what happens is different folders are taken from different organizations so if you have worked across folders which are from different organizations they will all upload it to their respective code review how many of you know about Gerrit or get it g e r r i t okay so get it is the code review system that Google started and a lot of people use now they use it in a large-scale way for Android and for Clothe Chromium project for code reviewing source so if you don't know about get it maybe some of you know about review board so get it works in a similar manner so any questions yeah sure so then you do upload okay I think there's a issue is the internet here does not allow SSH access SSH is not a ported block anyway so if you if I if that command got executed what that happened was this is the Android review so a commit like this would have been formed I was reviewing it to our our projects Gerrit which also looks very similar to that you have a list of commit that have come should have come up somewhere like this okay so this whoever has the authority will review it verify it and finally merge it so this is the way you contribute to a project okay so I would like to open up for 10 moments for some questions once again so any questions regarding what I said till now or anything else any questions no questions okay so what else do you want me to cover that's a question from my side yeah when your phone gets 12 ohms right yeah you do a serial connection and you give the power via the USB jack yeah I'm not writing the so when when you do a mod flash you do a mod flash yeah at times it gets bricked at times it gets bricked yeah yeah so those times you you can actually power up the phone by inserting an USB jack of six ohms and serial connection so it actually gets sold on eBay I know it's called the fast-boot cable yeah something like that exactly yeah so you connect the fourth to the ground it becomes a fast-boot cable exactly that's right yeah so is it something like you know official then why is it the option is something which you can avoid breaking itself so you can avoid breaking it itself okay so that's a nice question so how does the whole breaking thing come upon is you know as a OEM people want to secure the ecosystem because tomorrow if you run a custom ROM and you get a bad experience you can you can go out and say that XYZ OEM makes bad phones so because they don't want that to happen is that is the main motivation I believe they locked down so locking down means the bootloader is going to verify the signature of each system partition before booting it okay so if without a proper bootloader unlocked if you change the contents of the system partition it won't boot that's one kind of a brick but that's a soft brick because you can rewrite the system partition probably again via some exploits okay and you can reboot your device there can be hardware level bricks also when we talk about hardware we mean if the bootloader gets corrupted because the bootloader gets corrupted there is there are not a lot of ways to fix it yourself because anybody who has worked on the electronics platform would actually know that bootloader is not you can there is no software way to write the bootloader you have to use a JTAG or ICSP programmer and you have to write the bootloader through that so I mean I think that answers the question why the brick thing happens and how you can recover from it yeah so the fast boot cable that you're saying is something that a lot of chipsets have this configuration that when you're connecting the USB the fourth pin is grounded it boots into a hidden partition okay so from there you can reflash your bootloader or your kernel and then you can get revive okay okay and one more question yeah yeah so say for example I'm developing a mobile phone or modifying the yes modifying operating system yeah yeah do you find any challenges in terms of privacy policies or regulations so how far I mean at the individual level or at our organization level at the individual level I don't think you will face a lot of problems not at an individual level more from the organizational level from organizational level so if you want to like what exactly do you want to do if you want to sell a custom say as you may want to develop something like solidly secure with no back doors and a solid system yeah secure solid secure system so whether the vendor who has actually made the phone will object to that or something like that yeah and also in terms of organizational policies privacy so how far does a developer or I mean how you want to ask that how many how much the lawyers focus take around the developers yeah so they do focus take around a lot of the developers because if it does somehow hamper legitimate interest of the vendor they will okay as an individual developer I feel it the ecosystem is slowly becoming very conducive because like Sony used to help me they still help me a lot because I used to develop on Sony phones and I don't have any association with Sony as such but they used to help me like anytime I need any documentation if I need devices to test something on so they need to provide me and really what am I doing I'm making custom firmware which delete Sony's firmware and installs mine one and they were still helpful towards it so that's fine but as a as a organization if you want to do it obviously they will have some objections to that so maybe the proper way to do it is what cyanogen ink is doing okay tie up with a hardware vendor and get it by default on some device okay thank you and the session was really interesting thanks so anything else like like I said anything else you want me to cover than what I say what I have covered if it's possible I can how do you do this kernel debugging in this suppose you want to add the IP table support yeah so how do you do the debug like in how do the kernel debugging is Linux we have GDP and all and we can connect then we can do that right and I have a device I will just show you yeah so I have a device connected so I do ADB shell and I can just do so there you have your KMSG yeah so is there something similar to that here that that I don't think would be available would be possible like doing a breakpoints on the kernel level code yeah no that won't possible I mean I mean as far as my experience is it is not but if you have a developer board and you have got UART connections to it then it will be possible because you need hardware interrupts for it right like I think you have some experience with developer board so you will need hardware interrupts to actually debug the kernel but via USB connection you can't have hardware interrupts I mean this is I'm saying yeah yeah you can do it via chemo you can do it same or something different than see for chemo you need an abstraction of the hardware in the first place okay so if you get a emulator for this thing for architecture then only you are able to do that so you will have some issues because you are going to run chemo on a 64 bit machine and you want the kernel of IRM device to be debugged so there could be some issues I'm not very familiar with this thing because my experience has been mostly working on devices that are already built so I have a developer board at my place and you can actually do this kernel breakpoint debugging kernel by breakpoints if you have a UART connection to the chip itself so that you can do interrupts via hardware and that is possible anything else okay so I guess that will wrap it up