Friday, 17 March 2017

Some famous Android Make and ADB command



Hello Guys !!! Hope you all are doing well !!!!
In my previous post, I have given detail explanation of AOSP build process and its make command.
Today I am going to list down some famous and useful make and ADB command for android source code build(AOSP).

Android source code build process
Very first time after opening the terminal(Ubuntu/Linux) we have to give following set of command

1. Go to the <aosp_working_dir> ( type cd /u-dir/aosp_android_dir>)

2. then use source source build/envsetup.sh

3. Then use lunch command and choose your lunch option

4. And in the last have to give make -j16 or make -j8 command for build

Below is the some useful make command

1. Seeing the Build Commands
When we build the AOSP, it doesn’t actually show us the commands it’s running. Instead, it prints out only a summary of each step it’s at. If we want to see everything it does
make -j4 showcommands

2. To compile an app
if you want to build .apk file from your <aosp_working_dir> without going to particulat app dir and issueing mm command then use this command.
make app_name.apk -j4

3. Power Menu and Lock screen
make android.policy

4. Framework Changes
make framework (the initial framework files)
make framework-res (the initial framework resources)

5. Update the services changes

make services (the services.jar file)

6. Android SDK build
make sdk (builds the android sdk)

7. Build all Modules
make modules (builds all modules)

8. Build clean or fresh
make installclean (removes all staging directories out/target/product/boardname/system)
make clean (removes the whole /out directory)
make recoveryimage (builds the recovery from /bootable/recovery, customizable if wanted!)

9. Build with latest changes only
make snod - build the system image from the current software binaries

10. Build Everything
make all - make everything, whether it is included in the product definition or not


11. Updating the API
The build systems has safeguards in case you modify the AOSP’s core API. If you do,
the build will fail by default with a warning such as this
You have tried to change the API from what has been previously approved
make update-api


Some macros and functions
There are some helper macros and functions that are installed when we set envsetup.sh. Following are most used one :-

- execute 'make' from the top of the tree (even if your current directory is somewhere else)

mm - Builds all of the modules in the current directory, but not their dependencies

mmm <dir1> - Builds all of the modules in the supplied directories, but not their dependencies

mma - Builds all of the modules in the current directory, and their dependencies.

mmma <dir> - Builds all of the modules in the supplied directories, and their dependencies

mm -B : Rebuild the module even it hasn’t any changes.


Some Most used ADB/Fastboot Commands

1. Basic information about devices connected
adb devices


2. Reboot your Device
adb reboot
This command reboots device in normal mode.

3. Reboot into Recovery
adb reboot recovery
Reboot your device into recovery mode, say for flashing a custom ROM.If you have flashed a custom recovery on your device, you will be taken to the same instead of the stock recovery

4. Reboot into Bootloader Mode
adb reboot bootloader
reboot your device into bootloader mode. That is where you can unlock your bootloader, reboot into fastboot and recovery mode, and do some other tasks .

5. Rebbot in Fastboot mode

adb reboot fastboot
it will take your device directly to fastboot mode. The fastboot mode helps you flash custom recoveries as well as custom ROMs on your device

6. Send File to your Device
adb push Source file Destination folder

7. Get File from your Device
adb pull FileLocation Destination folder

8. Install an App on your Device
adb install APKLocation

9. Remount the System
adb remount

10. Get logs of connected device

adb logcat

11. Capture Bugreport

adb bugreport
Shows three element on the screen: dump sys, dump state and logcat data

12. Start ADB server
adb start-server
Starts the ADB server process

12. Stops the ADB server
adb kill-server
Kill the current running ADB server

13. Lunch Remote Shell
adb shell
Launches remote shell console for connected device

14. Help
adb help
Displays the help contents for ADB


15. Sqlite command-line
adb -s emulator-5554 shell (hit enter key)
sqlite3 path of DB
# /data/data/com.example.google.rss.rssexample/databases/rssitems.db
SQLite version 3.3.12
Enter ".help" for instructions
.... enter commands, then quit...
# sqlite> .exit
Use the sqlite3 command-line program to manage SQLite databases created by Android applications

Thanks
Saurabh
Happy Coding...

Monday, 13 March 2017

Add and use External Library or jar file in android source code(AOSP)

Hello Guys !!! Hope you all doing well !!!

Adding an external library or Jar file in android is very easy. Many of you are aware of this thing. But What about if you have to add in android source code (AOSP) .

In one of my previous post I have mention how to create a .so file from existing native (c/c++ ) code in AOSP and its uses.

In Toady post I am going to discuss addition or utilization of existing java library or Jar file in android sour code framework (AOSP).
So let say you’ve downloaded or built the library into a jar file, which we’ll call mylib.jar .


Now you want to add this lib file in android source code(AOSP) so that any one access it easily in App layer or in framework layer.

Step by step procedure is as follow :-

Step 1 Adding library to prebuilt or external folder
Copy mylib.jar file into the Android prebuilts directory, somewhere such as
<aosp-working-dir>/prebuilts/misc/common/mylib/
Or you can add it in external folder like this
<aosp-working-dir>/external/mylib/
In that same directory( here directory is mylib ), create a makefile called Android.mk with the following contents

$LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := mylib.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)
You can specify any name you wish for LOCAL_MODULE, but ensure that LOCAL_SRC_FILES specifies your jar file exactly as it is named.

Step 2 Building the added jar file
Now it is time to check that the added library file is building or not . For Building, go to the top-level Android source directory and run make prebuilts, if you have added your library in prebuilt folder.
Or, navigate to the directory where you added the library (example external/mylib ) and run mm -B command.
The output of the build should include one “Install:” line per library that you added.

Step 3 Add and use the Library in the Android Frameworks
Now that we have the library properly setup and built. For using this library, we have to include it in framework/base folder android.mk file. The complete path is :-
<aosp- working-dir>frameworks/base/Android.mk


After that we can use it by including it as a static library in the makefile of any other Android module.

For example, if we wanted to use it in a system service like the AlarmManagerService or any other code in the frameworks/base project.

Taking Android M 6.0.1 as an example, we should insert the LOCAL_STATIC_JAVA_LIBRARIES in Line 427 of the framework base’s Android.mk file

...
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_JAVA_LIBRARIES := core-libart conscrypt okhttp core-junit bouncycastle ext
LOCAL_STATIC_JAVA_LIBRARIES += mylib anotherlib  ### add this line
LOCAL_MODULE := framework
...
Now every thing is set properly. Give a make update-api command . Build your system image and start using newly added jar file in your app or in framework services.

Adding additional libraries in the same directory

If you have more jar files and you want to use it. In that case, you can put them in the same directory, and even add them to the same Android.mk make file. Follow the same step mention above.
Just add the other library in the same Android.mk file such as :

$LOCAL_PATH := $(call my-dir)
## for mylib
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := mylib.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)

## for anotherlib
include $(CLEAR_VARS)
LOCAL_MODULE := anotherlib
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := anotherlib.jar
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_MODULE_SUFFIX := $(COMMON_JAVA_PACKAGE_SUFFIX)
include $(BUILD_PREBUILT)

Using the library in App or frameworks services
You can use your library in app just putting your library name inside your app Android.mk file like this
LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 mylib
Then Import your method in your application and use it easily.

For Using this library in any framework services, it is easy. Because we already mention this libraries inside framework/base/Android.mkfile. 
So no need to do any extra effort for accessing it. Just import your jar library method in your .java file and use it.

Thanks
Saurabh
Happy Coding !!!


Friday, 3 March 2017

Some useful git command line

Hello Guys!!!  Hope You all are doing well!!!!.
Today I am going to share some useful git command. I think here no need to start  with my favorite question What is git. it is obvious that you people know about git.  

Git is a version control system (VCS) for tracking changes in computer files and coordinating work on those files among multiple people. 

it can be used to keep track of changes in any files. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear workflows. 

1. setting proxy in git

git config --global http.proxy http://my.company.com:8070
git config --global https.proxy https://my.company.com:8070

8070 is port number

2. Removing old git
Go to the project's directory: cd PROJECT_DIRECTORY
Remove all the git specific files: rm -rf $(find . -name ".git*")

3. Initialize a new git repository
Go to the project's directory: cd PROJECT_DIRECTORY and use init command.
git init

4. Using existing remote git repository
if you want to add any remote existing github or any git base project to your current directory. Then use following command :-
git remote add origin https://github.com/user-12/Example_project.git
git fetch

It will show following result
remote: Counting objects: 5700, done.

remote: Compressing objects: 100% (36/36), done.

remote: Total 5700 (delta 15), reused 2 (delta 0), pack-reused 5654

Receiving objects: 100% (5700/5700), 8.46 MiB | 222.00 KiB/s, done.

Resolving deltas: 100% (3325/3325), done.
* [new branch] Development -> origin/Development
* [new branch] master -> origin/master

5. Check out on a remote Git branch
After successfully fetching the remote branch you can use following command to do checkout
git fetch origin
git branch -v -a
git checkout -b Development origin/Development

on successfully checkout you will get this messages

Branch Development set up to track remote branch Development from origin.
Switched to a new branch 'Development'

Now every thing set up, do 
git rebase and 
git pull and start making new changes in your existing project.

6. Steps for creating patch
First check your project's status using status command
git status
Then add all your modified class
git add packages/apps/example_app/res/layout/example.xml packages/apps/example_app/res/layout/example_1.xml …...
Commit it
git commit -m “Initial project commit”
check commit log
git log
create patch
git format-patch -1
creating patch without commit
git diff >> example_patch_file.patch

7. Apply patch
patch -p1 < ./<working-dir>/0001-Added_example_feature.patch

8. Steps for reset code to latest commit & git pull
Delete all your changes and keep the latest changes , have on server
git log
git reset --hard commit_ID //  git reset --hard for current commit
git pull

Keep your local changes and get latest changes from servers
git stash
git pull
git log

Now if you want to apply your local changes then
git stash apply

Note :- 
It is recommended to do git fetch and git rebase before pushing your changes on Git.

9. Git Configuration
git configuration will give easy access of pull and push of files. 
git version  // show the current git version installed in your machine.
git config --global user.name "user-12"
git config --global user.email "user-12@examp.com"
git config --global --list // shows all global list.
With Git 1.7.9 or later, you can just use one of the following credential helpers:
git config --global credential.helper cache
... which tells Git to keep your password cached in memory for (by default) 15 minutes. You can set a longer timeout with:
git config --global credential.helper "cache --timeout=3600"


10. Git Ignore file 

  1. Create a .gitignore file by typing touch .gitignore
  2. Open the .gitignore file with a text editor of your choice and edit its contents to make sure you list all files in the project directory that you do NOT want to upload to your remote repository. List one file on each line, then save and close the .gitignore file.  

Happy Coding !!!

Thanks

Saurabh

Note:- Please proved your comment and suggestion for my post. It will energize me.

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...