之前一直是做windows和Linux安全,现在自己做了几年移动游戏开发后,觉得有必要对Android深入研究一下了。并且把这些研究都记录下来做一些。
首先从搭建环境开始,目前主流是Android9.0,虽然很多都推荐从低版本开始,不过我觉得毕竟还是要跟最新的技术走,直接到9.0的最后一个发布版。研究环境VMWare 15里安装Ubuntu18.04,开启VT虚拟化。
然后就是准备环境。JDK,我选的1.8_231版本,并非网上推荐的openjdk,主要是习惯了。
sudo vim /etc/profile
配置增加
export JAVA_HOME=/usr/java/jdk1.8.0_231
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre
配置生效
source /etc/profile
安装
sudo apt-get install python
sudo apt-get install git
接下来就是安装curl
sudo apt-get install curl
ubuntu 自带的 curl 不支持 https, 需要自己编译安装curl才支持ssl,不过不支持也能用,下载源代码的时候有些警告。
curl支持ssl这里我也简单说一下
git clone https://github.com/openssl/openssl.git
cd openssl
./config --prefix=/usr/local/openssl
make
make intall
git clone https://github.com/curl/curl.git
cd curl
./configure --with-ssl=/usr/local/openssl --disable-dict --disable-ftp --disable-imap \
--disable-ldap --disable-ldaps --disable-pop3 --disable-proxy \
--disable-rtsp --disable-smtp --disable-telnet --disable-tftp \
--disable-zlib --without-ca-bundle --without-gnutls --without-libidn \
--without-librtmp --without-libssh2 --without-nss --without-zlib
make
make install
接下来安装repo
curl http://mirrors.tuna.tsinghua.edu.cn/git/git-repo > repo
chmod 777 repo
sudo mv repo /bin/repo
mkdir aosp
cd aosp
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
git config --global user.email "yourname@gmail.com"
git config --global user.name "yourname"
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-9.0.0_r49
repo sync
repo init 之后可以到.repo/manifests.git/logs/refs/remotes/origin下面看远程仓库里有些什么镜像方便下载
源代码下载完后,接下来就是编译了
source build/envsetup.sh
lunch
make -j8
lunch的时候,会让你选择,我选择x86环境,毕竟VT技术下X86架构无需指令转换是运行最流畅的
编译完成之后,就可以开始导入android studio了,我也选择的最新版本android studio安装,并且启动安装好SDK。
接下来生成工程文件
mmm development/tools/idegen/
工程文件在回在aosp目录下android.ipr android.iml
然后就开始导入了,File->Open 选择android.ipr,就开始漫长等待。
导入完成后可以大开iml看一下,有大量文件标记,我做了一份替换掉就行,这份就是排除掉一些不用的工程
<?xml version="1.0" encoding="UTF-8"?>
<module version="4" relativePaths="true" type="JAVA_MODULE">
<component name="ModuleRootManager" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<!--sourceFolder标签表示需要索引的代码目录-->
<sourceFolder url="file://$MODULE_DIR$/frameworks/base/core/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" generated="true" />
<!--excludeFolder标签表示这个目录下所有内容都不索引-->
<excludeFolder url="file://$MODULE_DIR$/.repo" />
<excludeFolder url="file://$MODULE_DIR$/abi" />
<excludeFolder url="file://$MODULE_DIR$/art" />
<excludeFolder url="file://$MODULE_DIR$/bionic" />
<excludeFolder url="file://$MODULE_DIR$/bootable" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/cts" />
<excludeFolder url="file://$MODULE_DIR$/dalvik" />
<excludeFolder url="file://$MODULE_DIR$/developers" />
<excludeFolder url="file://$MODULE_DIR$/development" />
<excludeFolder url="file://$MODULE_DIR$/device" />
<excludeFolder url="file://$MODULE_DIR$/docs" />
<excludeFolder url="file://$MODULE_DIR$/external" />
<excludeFolder url="file://$MODULE_DIR$/external/bluetooth" />
<excludeFolder url="file://$MODULE_DIR$/external/chromium" />
<excludeFolder url="file://$MODULE_DIR$/external/emma" />
<excludeFolder url="file://$MODULE_DIR$/external/icu4c" />
<excludeFolder url="file://$MODULE_DIR$/external/jdiff" />
<excludeFolder url="file://$MODULE_DIR$/external/webkit" />
<excludeFolder url="file://$MODULE_DIR$/frameworks/base/docs" />
<excludeFolder url="file://$MODULE_DIR$/frameworks/base/tests" />
<excludeFolder url="file://$MODULE_DIR$/frameworks/base/tools" />
<excludeFolder url="file://$MODULE_DIR$/hardware" />
<excludeFolder url="file://$MODULE_DIR$/libcore" />
<excludeFolder url="file://$MODULE_DIR$/libnativehelper" />
<excludeFolder url="file://$MODULE_DIR$/ndk" />
<excludeFolder url="file://$MODULE_DIR$/out" />
<excludeFolder url="file://$MODULE_DIR$/pdk" />
<excludeFolder url="file://$MODULE_DIR$/platform_testing" />
<excludeFolder url="file://$MODULE_DIR$/prebuilt" />
<excludeFolder url="file://$MODULE_DIR$/sdk" />
<excludeFolder url="file://$MODULE_DIR$/tools" />
<excludeFolder url="file://$MODULE_DIR$/vendor" />
<excludeFolder url="file://$MODULE_DIR$/toolchain" />
<excludeFolder url="file://$MODULE_DIR$/system" />
<excludeFolder url="file://$MODULE_DIR$/prebuilts" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="inheritedJdk" />
<orderEntryProperties />
</component>
</module>
接下来就是把一些依赖清理掉,节约启动时间
SDK也配置好
好,基本上源码调试环境就配置好了
接下里开始启动虚拟机
android虚拟机需要kvm支持,所以kvm需要安装一下
sudo apt install qemu qemu-kvm libvirt-bin bridge-utils virt-manager
记得权限要给,不然android虚拟机无法起来
sudo chown 你的账户名 -R /dev/kvm
回到aosp目录
source build/envsetup.sh
emulator
不出意外,模拟器就可以起来了
点击这里选择你要调试的程序
我这里选择的是Settings,然后源代码里找到Settings的Activity,下断点
然后模拟器里打开Setting界面就回断下了
好了,接下来就可以对android应用层框架进行调试了。