Android應用程式編譯成系統程序簡介
簡介:在android操作系統中,有一些應用程序,如打電話,系統設置、鬧鐘等等在我們購買手機的時候就已經有了,如果我們想寫一個應用程序到系統中,而又不給用戶刪除。該如何做?本文將提供一個簡單的例子,看看android系統裏是如何把應用程序寫入系統中去的。
1、開發環境
操作系統:Ubuntu xx
開發工具:Eclipse
Android SDK:android 2.2
2、開發環境搭建過程
要想把android應用程序寫入系統中,首先要搭建一個android文件系統的開發環境。由於網上很多搭建環境的例子,這裏只羅列要點二具體介紹。
(1)、下載Android文件系統源碼
http://source.android.com/source/download.html
(2)、Eclipse源碼開發環境
http://source.android.com/source/using-eclipse.html
(3)、Eclipse開發App環境
這個可以參考android sdk 的文檔或者網上查找資料
3、以Helloworld爲例
(1)、建立一個App
在Eclipse-App開發環境中新建一個Android Project命名爲Helloworld,然後在模擬器上調試運行。將會見到下看到Helloworld 程序運行,並在屏幕上顯示Hello world ,Main!這一步我們不需要寫任何的代碼。
(2)、移植App到Android
在linux系統中進入Android的源碼目錄,來到./package/apps/Settings/ 目錄,把這裏的Android.mk複製到剛纔那個HelloWorld App 的 跟目錄下,並修改Android.mk文件
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := Helloworld #與工程名字相同
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
(3)、修改/bulid/target/product/generic.mk 把工程編譯到系統中
generic.mk
#
# Copyright (C) 2007 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This is a generic product that isn't specialized for a specific device.
# It includes the base Android platform.
PRODUCT_PACKAGES := \
AccountAndSyncSettings \
CarHome \
DeskClock \
AlarmProvider \
Bluetooth \
Calculator \
Calendar \
Camera \
CertInstaller \
DrmProvider \
Email \
Gallery3D \
LatinIME \
Launcher2 \
Mms \
Music \
Provision \
Protips \
QuickSearchBox \
Settings \
Sync \
Updater \
CalendarProvider \
SyncProvider \
Helloworld
$(call inherit-product, $(SRC_TARGET_DIR)/product/core.mk)
# Overrides
PRODUCT_BRAND := generic
PRODUCT_DEVICE := generic
PRODUCT_NAME := generic
(4)、將Android工程拷貝到./package/apps/目錄下
此時,系統的代碼環境已經構建好。
(5)、編譯Android SDK
在Android源碼根目錄下執行:#make sdk
等待......
編譯完成後
來到/out/host/linux-x86/sdk/android-sdk_eng.root_linux-x86/tools/下,執行 :
./emulator -avd ANDROID2
啓動模擬器,就會開到Helloworld會在Android系統中。
通過設置->應用程序->管理應用程序->全部-選擇->Helloworld (如下圖示我們可以看到該程序爲一個系統默認啓動的程序,並且用戶不可以將其卸載)