Sunday 29 January 2017

Make App as a System App in Android

Hello Guys !! Hope you all doing well.

Today I am going to discuss how to add android app as a system app. First question, What is system App.? 

You can say , The App which came as pre-installed or as a system.img (AOSP system image as a android OS), called system App. System  app can easily access some platform(app-framework) level API call. 

As for example Camera buffer.  System App is not easily uninstalled by user, so it is a type of must have app as a android OS for a specific vendor(HTC, MOTO, Samsung).

Now how we can make an app as a system App.

  1.  Have source code
  2.  Have .apk Only

Step 1 Create a folder inside packages/apps/ 
First create a folder for your app ( Let say MyTestApp) inside packages/apps/ of your android AOSP downloaded source code.
 Then create a Android.mk file inside the folder(MyTestApp).
and in last copy your app source code inside the folder(MyTestApp) as for example following folder and file
  •  assets, 
  • build,
  •  res, 
  • src and 
  • AndroidManifest.xml etc
Step 2  open Android.mk file and add folowing code Snippet

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional 
LOCAL_UNINSTALLABLE_MODULE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_APPS)
LOCAL_CERTIFICATE := platform LOCAL_SRC_FILES
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := MyTestApp
LOCAL_PROGUARD_ENABLED := disabled 
LOCAL_PRIVILEGED_MODULE := true
LOCAL_STATIC_JAVA_LIBRARIES := libarity android-support-v4  
include $(BUILD_PACKAGE)
include $(call all-makefiles-under,$(LOCAL_PATH))
save this mk file.

Step 3 put your app name in build/target/product/ folder
open core.mk file from build/target/product/ folder and add your app name(MyTestApp) in
PRODUCT_PACKAGES  tag at the bottom MyTestApp.
Note :-
for specific vendor you can find the path like this vendor/manufacturer/device/vendor_device.mk

Now step by step procedure for .apk file
 Step 1 will be same like above
only change is that in place of src, res folder just put your .apk file.

step 2 open Android.mk file and add folowing code Snippet


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional
LOCAL_UNINSTALLABLE_MODULE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_APPS)

LOCAL_MODULE := MyTestApp

LOCAL_SRC_FILES := $(LOCAL_MODULE).apk

LOCAL_MODULE_CLASS := APPS

LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
# if Apk is signed then use it. Otherwise use platform
LOCAL_CERTIFICATE := PRESIGNED 

include $(BUILD_PREBUILT)
Step 3 will be same as above mention for Android source code (a)
This is all about how to make an android app as a system App.

 One more question here?
  if you have an .apk and you want to install it as a system app through command line(adb) in your phone then ???

in that case we have to push the .apk to the phone to the System partition. the path of the folder is /system/app or /system/priv-app (Android 4.3) using adb


adb root
adb remount
now place your apk  file in sdcard like this


adb push my-app.apk /sdcard/
adb shell
su
cd /sdcard
mv my-app.apk /system/app
# or when using Android 4.3 or higher
mv my-app.apk /system/priv-app
All System-Apps need to have the permissions rw-r--r--. So we can change them via adb with the change mode command like chmod 644 /path_to/your_file 

 Thanks Guys!!!

Happy Coding...


Saturday 21 January 2017

Add .so file in Android AOSP source code

Hello Guys !!! Hope You are doing well.
Today I am going to discuss How to add .so file in Android AOSP source code external folder.
You can add your prebuilt library in Android AOSP source code and it be a part of your AOSP System Image. I am describing step by step procedure for it.

Step 1 Create a folder ( let say myLibs) inside external folder of AOSP source code.


external folder of AOSP source code refers to external open source libraries.
That means libraries that the Android platform depend upon but that are not primarily developed and maintained by the Android open source project.

examples are webkit for the browser, FreeType for fonts, SqlLite for databases and so on. As more features are added to Android, more of these libraries are included in external.

Step 2 Create a Android.mk file

Create a Android.mk file inside your folder(let say myLibs) and copy your .so file in it.
You can use following content for your android.mk file

# Prebuilt Lib
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libMyabc # your lib name
LOCAL_SRC_FILES := libMyabc.so
# your lib .so file name
include $(BUILD_SHARED_LIBRARY)
Step 3 Add your library in Framework
In final step you have to add your library in Android AOSP framework makefile so that it will recognise and build as a part of System image.
You find Framework Android.mk file on following location
/android_aosp_sourcecode_download_folder/frameworks/base/core/jni/
Open Android.mk file and add your library in following section
LOCAL_SHARED_LIBRARIES := \
You can put your library name in that section example libMyabc \
That's it... now make it (make -j4) and you find your added so file in following folder
/android_aosp_sourcecode_download_folder/out/target/product/generic/obj/lib
with file name like :- libMyabc.so and libMyabc.so.toc
and you also found it in system/lib folder
/android_aosp_sourcecode_download_folder/out/target/product/system/lib

If you want to add .so files directly in your app, Then add below flag in your Android.mk file

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := AndroidMediaShell
LOCAL_PRIVILEGED_MODULE := true
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := APPS
LOCAL_SRC_FILES := app/$(LOCAL_MODULE).apk
LOCAL_PREBUILT_JNI_LIBS := (complete_path*)lib/your_lin_so_file.so
LOCAL_MODULE_TARGET_ARCH := arm or arm64 or x86
LOCAL_CERTIFICATE := PRESIGNED
LOCAL_OVERRIDES_PACKAGES :=
include $(BUILD_PREBUILT)

* If .so files in your current app lib folder then path is  lib/your_so_file.so

Thanks
Saurabh
Happy Coding!!!!

Friday 20 January 2017

Android CTS test download and run on Device/Emulator

Hello GUYS !!! Hope Doing Well

Today I am going to discuss Android CTS test for framework level changes.


So Start with What is CTS?
 CTS is automated test suite to verify all the thing is working properly. If you made any changes in framework/middleware or driver level, Then You have to run CTS to verify that every thing is working properly.

I am here discussing step by step procedure to download and run CTS test cases in android Emulator or connected devices.

Compatibility Test Suite
How to download and run CTS test on device or emulator.

Step -1 Compatibility Test Suite Downloads
you can downloads CTS from following links based on your project requirement.
https://source.android.com/compatibility/cts/downloads.html
Then Unzip it on your /u/ Dir . It will create “android-cts” dir.

Step -2 Setting up CTS
CTS currently supports 64-bit Linux and Mac OS host machines. ADB and AAPT
Before running the CTS, make sure you have recent versions of both 
  • Android Debug Bridge (adb) and 
  • Asset Packaging Tool (AAPT) installed
You can run CTS on Emulator, CTS run from downloaded AOSP source code, and those tools' location added to the system path of your machine.
Ensure adb and aapt are in your system path.
Example:
PATH=$PATH:/home/myuser/android-sdk-linux_x86/platform-tools.
For more detail you can follow following link
https://source.android.com/compatibility/cts/setup.html

Step -3 Running CTS tests

1. Make sure you have at least one device connected
you check this by using adb command in your terminal :-
adb devices

2. Launch the cts-tradefed console by running the 'cts-tradefed'. If you've
downloaded and extracted the CTS zip, the script can be found at
android-cts/tools/cts-tradefed

Or else if you are working from the Android source tree and have run make cts,
the script can be found at
out/host/linux-x86/cts/android-cts/tools/cts-tradefed

3. Type following command in CTS-tradefed console to run CTS
'run cts' to run the default CTS plan

4. Some other useful commands are
To run a test module:
'run cts --module <module_name>'

To run a specific test:
'run cts --test <test_name>'

To shard a plan test run on multiple devices
'run cts --shards <number of shards>

note: all connected devices must be running the same build
For more options:
'run cts –help'
For more detail you can check this link:-
https://source.android.com/compatibility/cts/run.html

Step- 4 Run CTS test from development environment
Perform these steps to build and run cts-tradefed from the development environment:
cd <path to android source root>
make cts
cts-tradefed

Using CTS Verifier(supplement to the CTS)
CTS Verifier provides tests for those APIs and functions that cannot be tested on a stationary device without manual input, like audio quality, touchscreen, accelerometer, camera, etc.
The Setup and running process is simple compare to CTS.
For more detail you can follow this link
https://source.android.com/compatibility/cts/verifier.html

Thanks
Saurabh
Happy Coding !!!

Android AOSP Source Code Download and Build

Hello Guys !!! Hope doing well

Today I am going to discuss how to download and build Android Source code. I will also share info on how to run Emulator on generated AOSP  source image.  I am sharing step by step approach for download and build.
Step to download android source code(AOSP) in Ubuntu
1. First create one folder like "aosp-m" to copy AOSP code in your machine.

2. Open terminal(Ctrl+Alt+T) and change your Dir to latest created Dir let say "aosp-m"

3. After that Run the following command in terminal :-
git clone git://gitz01/cm/download/android/manifest
if this link not worked then try this one 
repo init -u https://android.googlesource.com/platform/manifest

4. Run Following command in terminal one by one
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

5. then run following command
repo init -u git://gitz01/cm/download/android/manifest -b master -m identifiedmanifest.xml
if this link not worked then try this one 
repo init -u https://android.googlesource.com/platform/manifest -b android-6.0.1_r78
here you can replace identifiedmanifest.xml to your desired AOSP source code , Let Say "android-6.0.1_r10.xml" this is code name of marshmallow.

6. and in the last run "repo sync" command. This command start downloading your desired AOSP code in your machine. This operation take more than 1 hours(depending on your INTERNET connection speed) to download source code.


That's it... Happy Coding.......

Note:- Sometime just copy paste not work properly in terminal, in that case you type these commands.

Step to compile/create System image after AOSP source code download
1. After successful download of AOSP code, First check your JDK installation
For android AOSP source code compilation, we need openjdk
For Android N we need openjdk version "1.8.0_xxxx" and for L or M we need
openjdk version "1.7.0_xxx"
2. Installing required packages (Ubuntu 14.04)
You will need a 64-bit version of Ubuntu. Ubuntu 14.04 is recommended.
sudo apt-get install git-core gnupg flex bison gperf build-essential \
zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 \
lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache \
libgl1-mesa-dev libxml2-utils xsltproc unzip
For more detail follow the link :-
https://source.android.com/source/initializing.html
3. Then run the following command in sequence
source  build/envsetup.sh
lunch (Enter this command and then choose your target from the given option. Just pass the number like 1)
make -j4 (make command based on CPU Core and RAM size you can choose like -j8, j16 etc..)

That's it. For the fresh build it will take some couple of hours (1 or 2 or 3).

Happy Coding.......

How to run Emulator step by step guide.

Running emulator in downloaded android AOSP source code is as below :-
Step 1
If you have finished your build and generated System image correctly in current running Terminal(Ubuntu), Then it is straight forward. Just type below command in your terminal:-
emulator
Step 2
If you have generated system image earlier and you have started a fresh terminal(Ubuntu) then run the following command one by one :-

  1. source build/envsetup.sh
  2. lunch 1 here 1 is my lunch type, you can replace it with yours like(7, 8 etc) and in the last
  3. emulator
    Thanks Guys !!!  Happy Coding.....

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...