android系统源代码单独编译应用程序

通过前面的文章,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源代码情景分析》这本书,但是遗憾并没有将其记录沉淀下来,最近决定重新温习一下,希望能有新的认识和积累,不过原书的源代码还比较老,强烈建议读者使用新的源代码,实例操作。
纸上得来终觉浅,绝知此事要躬行!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值