前言:
源代码下载以及编译:
在http://source.android.com/source/initializing.html网站上,有详细的步骤,我的系统是ubuntu12.04,所以下面给出的编译都是针对该系统,如果你的系统不一样,请参考该网站上针对其他系统的环境搭建方法。
1. 工具安装
$ sudo apt-get install git-core gnupg flex bison gperf build-essential
$ sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib/i386-linux-gnu/libGL.so
这一步基本没有遇到问题,很顺利。
2. 源码下载
$ mkdir ~/bin
$ PATH=~/bin:$PATH
$ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
$ chmod a+x ~/bin/repo
$ mkdir androidsource
$ cd androidsource
如果你想下载当前最新的版本,那么:
$ repo init -u https://android.googlesource.com/platform/manifest
如果你想下载某个特定的版本,那么:(我下载的就是4.0的)
$ repo init -u https://android.googlesource.com/platform/manifest -b android-4.0.1_r1
这一步会要求你输入用户名以及一个邮箱,输入就行了,我感觉这个邮箱好像也没啥用。
$ repo sync
这一步会花很长时间,请保证你的网络一直有效,另外我遇到下面的问题1,你可以先看看,以避免这个问题。
3. 编译
$ source build/envsetup.sh
$ lunch full-eng
$ make -j4
这一步遇到问题2和3.
这一步具体要多长时间我也不确定,我是晚上睡觉之前让它编的,第二天早上编译完成,最后大处的信息如下:
注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
target Java: CtsAccountManagerTestCases (out/target/common/obj/APPS/CtsAccountManagerTestCases_intermediates/classes)
注意:某些输入文件使用或覆盖了已过时的 API。
make: *** [out/target/common/obj/APPS/android.core.tests.libcore.package.com.no- core-tests-res_intermediates/noproguard.classes-with-local.dex] 已杀死
make: *** 正在等待未完成的任务....
不确定是不是有错误,但是我发现不影响我编译动态库,所以就没管它。
4.编译动态库或静态库
进入目录:
>> cd ~/androidsource/out/target/product/generic
在这个目录下新建一个文件夹,如driver,然后可以在这个目录下创建你的c程序,编译动态库。
这里我举一个很简单的例子,新建一个c文件,实现add(int a,int b)功能。
(1).c文件如下:
#include
int add(int a,int b)
{
return (a+b);
}
(2)再新建一个Android.mk文件,注意这个文件名不能改,如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= helloworld
LOCAL_SRC_FILES := hello.c
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_PATH = $(LOCAL_PATH)
include $(BUILD_SHARED_LIBRARY)
(3)配置环境
回到源码目录:
>> cd ~/androidsource
>> source build/envsetup.sh
>> lunch full-eng
回到动态库目录:
>> ~/androidsource/out/target/product/generic/driver
编译:
>> mm
编译完成以后会在当前目录下生成helloword.c,然后你就可以在安卓的其它地方调用该库了。
当然,你如果想在Android App中调用该库,那么借口函数的名字必须修改,这个就是另外一个问题了,这里就不详述了。
遇到的错误以及解决方法:
1. 错误:fatal: HTTP request failed
74.125.31.82 www.googlesource.com
74.125.31.82 android.googlesource.com
74.125.128.93 dl.google.com
74.125.128.136 dl.google.com
2.错误:“Your version is: 1.7.0 ,The correct version is: Java SE 1.6.”
分析:出现这个错误的主要原因是本机上jdk的版本太高,而Android编译需要的jdk版本是1.6.因为之前搭建过Android App开发环境,也需要jdk,所以当时在官网上找了一个最新的版本1.7的,所以这里就会出现错误。
解决方法:解决方法说起来很简单,将jdk1.7卸载,重新安装1.6就行了。但是实际操作过程中花了我很长时间,因为jdk1.6的安装问题。首先,当 时安装jdk1.7的时候有tar.gz格式的文件,下载下来直接解压在相应目录下,然后添加路径即可。但是官网上关于1.6版本只有.bin格式的文 件,下载下来安装的时候出现
“rpm:RPM should not be used directly install RPM packages, use Alien instead!
rpm:However assuming you know what you are doing...
error: 依赖性检测失败:
/bin/basename 被 jdk-2000:1.6.0_38-fcs.i586 所需要
”
出现这种错误的原因是因为rpm是redhat系的包管理器,ubuntu是debian系的,debian系的包管理器是dpkg,需要用deb包来安装,而不是rpm包。解决方法是:
>>sudo alien -k jdk-6u38-linux-i586.rpm
注意这个rpm文件是你刚刚运行.bin文件生成的,然后会生成一个.deb包
>>sudo dpkg -i jdk_1.6.038-1_i386.deb
我在这一步的时候遇到提示错误,我忘了是什么错误了,然后我一直重新安装,还是提示错误,我又换其他1.6的版本,仍然提示错误,最后在网上有一个人说, 不用理会这个错误,其实安装已经成功了,我这才修改环境变量,一般在/usr/java/jdk1.6.0下,然后重新编译,果然没错了,折腾死了。
3. 问题:
external/yaffs2/yaffs2/yaffs_tagsvalidity.h:23:6: warning: redundant redeclaration of ‘yaffs_InitialiseTags’ [-Wredundant-decls]
external/yaffs2/yaffs2/yaffs_guts.h:888:6: note: previous declaration of ‘yaffs_InitialiseTags’ was here
host C: mkyaffs2image <= external/yaffs2/yaffs2/yaffs_tagsvalidity.c
host C++: obbtool <= frameworks/base/tools/obbtool/Main.cpp
:0:0: warning: "_FORTIFY_SOURCE" redefined [enabled by default]
原因:gcc和g++编译器版本太高
解决方法:更换较低版本的gcc和g++编译器
查看编译器版本:
>>ls -l gcc*
lrwxrwxrwx 1 root root 7 Aug 30 19:26 gcc -> gcc-4.6
-rwxr-xr-x 1 root root 353216 Apr 16 07:37 gcc-4.6
lrwxrwxrwx 1 root root 7 Mar 14 04:05 g++ -> g++-4.6
-rwxr-xr-x 1 root root 357312 Apr 16 07:36 g++-4.6
安装gcc和g++4.4 版本:
sudo apt-get install gcc-4.4 g++-4.4 g++-4.4-multilib
修改链接:
>>sudo mv gcc gcc.bak
>>sudo ln -s gcc-4.4 gcc
>>sudo mv g++ g++.bak
>>sudo ln -s g++-4.4 g++
查看当前gcc和g++版本:
>>gcc -v
>>g++ -v
4. 问题
/home/user/bin/repo: line 1: syntax error near unexpected token `newline'
/home/user/bin/repo: line 1: `<!DOCTYPE html>'
solution: change a repo
curl
5. Problem
fatal: '../platform/abi/cpp.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
error: Cannot fetch platform/abi/cpp
找到以前的一个文档,发现加上一句话就可以了:
在.repo目录下的manifest.xml里找到fetch属性 改成 fetch= "git://Android.git.linaro.org/ "
6. problem
host
_intermediates/libGLcommon.a)
host
nslator.so)
/usr/bin/ld:
collect2:
make:
解决方法:
sudo
7. 编译时出现/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../libz.so when searching for -lz错误
host Executable: aapt (out/host/linux-x86/obj/EXECUTABLES/aapt_intermediates/aapt)
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../libz.so when searching for -lz
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../libz.a when searching for -lz
/usr/bin/ld: skipping incompatible //usr/lib/libz.so when searching for -lz
/usr/bin/ld: skipping incompatible //usr/lib/libz.a when searching for -lz
/usr/bin/ld: cannot find -lz
collect2: ld returned 1 exit status
make: *** [out/host/linux-x86/obj/EXECUTABLES/aapt_intermediates/aapt] Error 1
缺少lib32z1-dev,安装即可:apt-get install lib32z1-dev
8. target R.java/Manifest.java: CtsVerifier (out/target/common/obj/APPS/CtsVerifier_intermediates/src/R.stamp)
Warning: AndroidManifest.xml already defines versionCode (in http://schemas.android.com/apk/res/android); using existing value in manifest.
Warning: AndroidManifest.xml already defines versionName (in http://schemas.android.com/apk/res/android); using existing value in manifest.
Warning: AndroidManifest.xml already defines minSdkVersion (in http://schemas.android.com/apk/res/android); using existing value in manifest.
target Java: CtsVerifier (out/target/common/obj/APPS/CtsVerifier_intermediates/classes)
cts/apps/CtsVerifier/src/com/android/cts/verifier/PassFailButtons.java:191: onCreateDialog(int,android.os.Bundle) in android.app.Activity cannot implement onCreateDialog(int,android.os.Bundle) in com.android.cts.verifier.PassFailButtons.PassFailActivity; attempting to assign weaker access privileges; was public
private static <T extends android.app.Activity & PassFailActivity>
^
1 error
make: *** [out/target/common/obj/APPS/CtsVerifier_intermediates/classes-full-debug.jar] Error 41
Solution:comment the statement of cts/Android.mk, and then all the files in cts will not be compiled.
CTS means "Compatibility Test Suite", when we finished the development of Android, we must pass the CTS, and make sure that all the Application can run normally on this platform, then hand over this test report to google and get the authentication of android market.