一、参考
需要参考在源码中如何编译apk。
http://my.oschina.net/u/572562/blog/62011
http://www.xuebuyuan.com/1557315.html
在packages/apps/目录下有不少app,可以参考。
二、测试
1、写app
1)在android studio中写好空的工程
2)编辑strings.xml
<resources>
<string name="app_name">example1</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="value">value</string>
<string name="hint">hint</string>
<string name="read">read</string>
<string name="write">write</string>
<string name="clear">clear</string>
</resources>
3)编辑activity_main.xml,结果为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/value">
</TextView>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_value"
android:hint="@string/hint">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/button_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read">
</Button>
<Button
android:id="@+id/button_write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/write">
</Button>
<Button
android:id="@+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear">
</Button>
</LinearLayout>
</LinearLayout>
4)编辑activity类,结果为:
package com.example.gumh.example1;
import android.app.Activity;
import android.os.Bundle;
import android.os.IExampleService;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.example.gumh.example1.R;
public class MainActivity extends Activity implements OnClickListener {
private final static String LOG_TAG = "com.example.gumh.example1.MainActivity";
private IExampleService exampleService = null;
private EditText valueText = null;
private Button readButton = null;
private Button writeButton = null;
private Button clearButton = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exampleService = IExampleService.Stub.asInterface(
ServiceManager.getService("example"));
valueText = (EditText)findViewById(R.id.edit_value);
readButton = (Button)findViewById(R.id.button_read);
writeButton = (Button)findViewById(R.id.button_write);
clearButton = (Button)findViewById(R.id.button_clear);
readButton.setOnClickListener(this);
writeButton.setOnClickListener(this);
clearButton.setOnClickListener(this);
Log.i(LOG_TAG, "Example Activity Created");
}
@Override
public void onClick(View v) {
if(v.equals(readButton)) {
try {
int val = exampleService.getVal();
String text = String.valueOf(val);
valueText.setText(text);
} catch (RemoteException e) {
Log.e(LOG_TAG, "Remote Exception while reading value from example service.");
}
}
else if(v.equals(writeButton)) {
try {
String text = valueText.getText().toString();
int val = Integer.parseInt(text);
exampleService.setVal(val);
} catch (RemoteException e) {
Log.e(LOG_TAG, "Remote Exception while writing value to example service.");
}
}
else if(v.equals(clearButton)) {
String text = "";
valueText.setText(text);
}
}
}
5)在android源码下的app下新增example1文件夹:
packages/apps/example1
6)新增Android.mk文件:
[zzz@localhost example1]$ cat 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:=example1
LOCAL_CERTIFICATE:=platform
include $(BUILD_PACKAGE)
7)复制工程部分文件到这里(因为有些gradle的东西和结构不需要)
结果为:
[zzz@localhost example1]$ pwd
/home/zzz/opensource/android-src/packages/apps/example1
[zzz@localhost example1]$ ll
-rw-rw-r--. 1 zzz zzz 691 Nov 17 11:30 AndroidManifest.xml
-rw-rw-r--. 1 zzz zzz 217 Nov 17 11:41 Android.mk
drwxrwxr-x. 11 zzz zzz 4096 Nov 17 11:30 res
drwxrwxr-x. 3 zzz zzz 4096 Nov 17 11:38 src
[zzz@localhost example1]$ ll src/com/example/zzz/example1/MainActivity.java
-rw-rw-r--. 1 zzz zzz 2434 Nov 17 12:03 src/com/example/zzz/example1/MainActivity.java
2、编译app
[zzz@localhost android-src]$ mmm packages/apps/example1/
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=6.0
TARGET_PRODUCT=aosp_arm
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=
TARGET_2ND_ARCH_VARIANT=
TARGET_2ND_CPU_VARIANT=
HOST_ARCH=x86_64
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.10.0-229.14.1.el7.x86_64-x86_64-with-centos-7.1.1503-Core
HOST_CROSS_OS=windows
HOST_BUILD_TYPE=release
BUILD_ID=MASTER
OUT_DIR=out
============================================
Unknown option: -C
usage: git [--version] [--help] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
make: Entering directory `/home/gumh/opensource/android-src'
Running kati to generate build-aosp_arm-mmm-packages_apps_example1_Android.mk.ninja...
No need to regenerate ninja file
Starting build with ninja
ninja: Entering directory `.'
[ 83% 5/6] build out/target/product/ge...le1_intermediates/oat/arm/package.odex
dex2oatd W 19919 19919 art/runtime/arch/arm/instruction_set_features_arm.cc:96] Unknown instruction set features for ARM CPU variant (generic) using conservative defaults
dex2oatd I 19919 19919 art/dex2oat/dex2oat.cc:1944] out/host/linux-x86/bin/dex2oatd --runtime-arg -Xms64m --runtime-arg -Xmx512m --boot-image=out/target/product/generic/dex_bootjars/system/framework/boot.art --dex-file=out/target/product/generic/obj/APPS/example1_intermediates/oat/arm/package.odex.input --dex-location=/system/app/example1/example1.apk --oat-file=out/target/product/generic/obj/APPS/example1_intermediates/oat/arm/package.odex --android-root=out/target/product/generic/system --instruction-set=arm --instruction-set-variant=generic --instruction-set-features=default --include-patch-information --runtime-arg -Xnorelocate --no-generate-debug-info --abort-on-hard-verifier-error
dex2oatd I 19919 19919 art/runtime/gc/space/image_space.cc:692] Dumping image sections
dex2oatd I 19919 19919 art/runtime/gc/space/image_space.cc:696] SectionObjects start=0x70000000 size=3015800 range=0-3015800
dex2oatd I 19919 19919 art/runtime/gc/space/image_space.cc:696] SectionArtFields start=0x702e0478 size=544436 range=3015800-3560236
dex2oatd I 19919 19919 art/runtime/gc/space/image_space.cc:696] SectionArtMethods start=0x7036532c size=1993956 range=3560236-5554192
dex2oatd I 19919 19919 art/runtime/gc/space/image_space.cc:696] SectionDexCacheArrays start=0x7054c010 size=2139552 range=5554192-7693744
dex2oatd I 19919 19919 art/runtime/gc/space/image_space.cc:696] SectionInternedStrings start=0x707565b0 size=114840 range=7693744-7808584
dex2oatd I 19919 19919 art/runtime/gc/space/image_space.cc:696] SectionImageBitmap start=0x70773000 size=49152 range=7811072-7860224
dex2oatd I 19919 19919 art/dex2oat/dex2oat.cc:1742] dex2oat took 1.030s (threads: 4) arena alloc=137KB java alloc=56KB native alloc=267KB free=436KB
dex2oatd I 19919 19919 art/dex2oat/dex2oat.cc:1742] Code dedupe: 0 collisions, 0 max hash collisions, 0/3 probe distance, 3783 ns hash time
dex2oatd I 19919 19919 art/dex2oat/dex2oat.cc:1742] Mapping table dedupe: 0 collisions, 0 max hash collisions, 0/0 probe distance, 0 ns hash time
dex2oatd I 19919 19919 art/dex2oat/dex2oat.cc:1742] Vmap table dedupe: 0 collisions, 0 max hash collisions, 0/3 probe distance, 847 ns hash time
dex2oatd I 19919 19919 art/dex2oat/dex2oat.cc:1742] GC map dedupe: 0 collisions, 0 max hash collisions, 0/0 probe distance, 0 ns hash time
dex2oatd I 19919 19919 art/dex2oat/dex2oat.cc:1742] CFI info dedupe: 0 collisions, 0 max hash collisions, 0/0 probe distance, 0 ns hash time
dex2oatd W 19919 19919 art/runtime/runtime.cc:233] Current thread not detached in Runtime shutdown
[100% 6/6] Install: out/target/product...tem/app/example1/oat/arm/example1.odex
make: Leaving directory `/home/zzz/opensource/android-src'
#### make completed successfully (8 seconds) ####
[zzz@localhost android-src]$ ll out/target/product/generic/system/app/example1/
total 32
-rw-rw-r--. 1 zzz zzz 24758 Nov 17 12:03 example1.apk
drwxrwxr-x. 3 zzz zzz 4096 Nov 17 12:03 oat
[zzz@localhost android-src]$
可以看到,在输出目录已经有example1.apk了。
3、运行虚拟机
实际上,我在out/target/product/generic/目录下用指令:
emulator -sysdir ./ -system system.img -data userdata.img -kernel ~/opensource/android/goldfish/arch/arm/boot/zImage
虚拟机启动后,一直都是黑屏。
而用:
emulator -data userdata.img
确很快可以进入系统,怀疑是我加的某些代码导致内核有问题或者system.img有问题。
于是重新去对整个系统进行编译:
[zzz@localhost android-src]$ make -j4
...
Note: Recompile with -Xlint:unchecked for details.
[ 2% 49/1736] target Java: icu4j (out...LIBRARIES/icu4j_intermediates/classes)
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: external/icu/icu4j/main/classes/core/src/com/ibm/icu/impl/Relation.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
[ 5% 88/1736] Docs droiddoc: out/target/common/docs/api-stubs
DroidDoc took 85 sec. to write docs to out/target/common/docs/api-stubs
[ 5% 90/1736] Docs droiddoc: out/target/common/docs/system-api-stubs
DroidDoc took 83 sec. to write docs to out/target/common/docs/system-api-stubs
[ 5% 92/1736] Checking API: checkpublicapi-current
FAILED: /bin/bash -c "(true) && ( out/host/linux-x86/bin/apicheck -JXmx1024m -J\"classpath /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91-2.6.2.1.el7_1.x86_64/bin/../lib/tools.jar:out/host/linux-x86/framework/doclava.jar:out/host/linux-x86/framework/jsilver.jar\" -error 2 -error 3 -error 4 -error 5 -error 6 -error 7 -error 8 -error 9 -error 10 -error 11 -error 12 -error 13 -error 14 -error 15 -error 16 -error 17 -error 18 -error 19 -error 20 -error 21 -error 23 -error 24 -error 25 -error 26 -error 27 frameworks/base/api/current.txt out/target/common/obj/PACKAGING/public_api.txt frameworks/base/api/removed.txt out/target/common/obj/PACKAGING/removed.txt || ( cat build/core/apicheck_msg_current.txt ; exit 38 ) ) && (mkdir -p out/target/common/obj/PACKAGING/) && (touch out/target/common/obj/PACKAGING/checkpublicapi-current-timestamp)"
out/target/common/obj/PACKAGING/public_api.txt:23087: error 3: Added class IExampleService to package android.os
out/target/common/obj/PACKAGING/public_api.txt:23092: error 3: Added class IExampleService.Stub to package android.os
******************************
You have tried to change the API from what has been previously approved.
To make these errors go away, you have two choices:
1) You can add "@hide" javadoc comments to the methods, etc. listed in the
errors above.
2) You can update current.txt by executing the following command:
make update-api
To submit the revised current.txt to the main Android repository,
you will need approval.
******************************
[ 5% 92/1736] Checking API: checksystemapi-current
FAILED: /bin/bash -c "(true) && ( out/host/linux-x86/bin/apicheck -JXmx1024m -J\"classpath /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91-2.6.2.1.el7_1.x86_64/bin/../lib/tools.jar:out/host/linux-x86/framework/doclava.jar:out/host/linux-x86/framework/jsilver.jar\" -error 2 -error 3 -error 4 -error 5 -error 6 -error 7 -error 8 -error 9 -error 10 -error 11 -error 12 -error 13 -error 14 -error 15 -error 16 -error 17 -error 18 -error 19 -error 20 -error 21 -error 23 -error 24 -error 25 -error 26 -error 27 frameworks/base/api/system-current.txt out/target/common/obj/PACKAGING/system-api.txt frameworks/base/api/system-removed.txt out/target/common/obj/PACKAGING/system-removed.txt || ( cat build/core/apicheck_msg_current.txt ; exit 38 ) ) && (mkdir -p out/target/common/obj/PACKAGING/) && (touch out/target/common/obj/PACKAGING/checksystemapi-current-timestamp)"
out/target/common/obj/PACKAGING/system-api.txt:25032: error 3: Added class IExampleService to package android.os
out/target/common/obj/PACKAGING/system-api.txt:25037: error 3: Added class IExampleService.Stub to package android.os
******************************
You have tried to change the API from what has been previously approved.
To make these errors go away, you have two choices:
1) You can add "@hide" javadoc comments to the methods, etc. listed in the
errors above.
2) You can update current.txt by executing the following command:
make update-api
To submit the revised current.txt to the main Android repository,
you will need approval.
******************************
[ 5% 92/1736] Building with Jack: out...k_intermediates/with-local/classes.dex
ninja: build stopped: subcommand failed.
make: *** [ninja_wrapper] Error 1
[zzz@localhost android-src]$ pwd
看到有几个错误:
out/target/common/obj/PACKAGING/public_api.txt:23087: error 3: Added class IExampleService to package android.os
out/target/common/obj/PACKAGING/public_api.txt:23092: error 3: Added class IExampleService.Stub to package android.os
out/target/common/obj/PACKAGING/system-api.txt:25032: error 3: Added class IExampleService to package android.os
out/target/common/obj/PACKAGING/system-api.txt:25037: error 3: Added class IExampleService.Stub to package android.os
用make update-api来解决,或在方法前增加 /*{@hide}/
1、增加 hide
在那个
frameworks/base/services/core/java/com/android/server/ExampleService.java
的接口的方法setVal和getVal上增加了:
/*{@hide} /
public void setVal(int val) {
再编译还是有问题,可能没有加对地方,先放过。
2、执行make update-api
[ 99% 281/282] Docs droiddoc: out/target/common/docs/doc-comment-check
DroidDoc took 416 sec. to write docs to out/target/common/docs/doc-comment-check
[100% 282/282] Copying current.txt
Copying removed.txt
3、再次编译:
make j4
...
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
[100% 1449/1449] host Java: ahat-test-.../ahat-test-dump_intermediates/classes)
Note: art/tools/ahat/test-dump/Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details
但运行虚拟机还是有问题。