通过前面的文章,Android系统源代码编译成功后,我们开始植入第一个应用程序到系统中去。
首先进入到系统源代码目录,在packages/experimental
目录下
新建项目AndyCode,目录结构如下:
MainActivity.java
源代码
package com.byhook.andy;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
内容如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.byhook.andy.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="25dp"
android:text="@string/app_hello"
android:gravity="center"
/>
</FrameLayout>
strings.xml
内容如下:
<resources>
<string name="app_name">AndyCode</string>
<string name="app_hello">Hello World!</string>
</resources>
AndroidManifest.xml
文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.byhook.andy" >
android:versionCode="1"
android:versionName="1.0.0"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最后一个Android.mk
文件内容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := AndyCode
include $(BUILD_PACKAGE)
开始单独编译应用程序:
source build/envsetup.sh
mmm packages/experimental/AndyCode
编译成功之后,重新打包android系统镜像文件system.img
make snod
启动模拟器
emulator &
可以看到我们的应用程序已经打包进去了
打开应用程序
小结:
之前看过《Android源代码情景分析》这本书,但是遗憾并没有将其记录沉淀下来,最近决定重新温习一下,希望能有新的认识和积累,不过原书的源代码还比较老,强烈建议读者使用新的源代码,实例操作。
纸上得来终觉浅,绝知此事要躬行!