在我写这篇笔记的时候,Android已经诞生了12年之多,想想自己当初在刚上大学那会(12-13)还在用Nokia的 N51,其实那个时候安卓系统已经很厉害了,之所以自己这么顽固不化主要还是因为自己的经济状况。导致自己发誓入手一台智能机的原因是: 驾考科目四被挂了,这才不得已使用智能机了。那时候已经是大三了,用的是华为麦芒。
研究生毕业后来到这家公司后,直接上手了Android源码,真的很奇怪,我好想学什么都是先底层然后再上层,这次的Android也是。现在也别管 Android以后的发展怎么样,因为它就在这里,自己必须在最短的时间内去熟悉它。
前言部分一般是无用的,可以直接跳过。
这里,我就是简单地回忆下自己思考的,根本没有从代码 其他书籍去考证这些东西的正确性,当然,后面随着熟悉的深入,也需要进行一些修改。
众所周知,我们安卓使用的应用的打包形式为APK文件,当launch 安装这个应用是,AOSP(Android Open Source Platfrom,用来简称安卓)会维持一个名为installd
的守护进程,他会自动监听应用程序包的安装情况。
在这一通道上,有一个名为PackageManagerService
的服务进行包安装。这是AOSP中一个较为核心的服务。
在aosp的源码中,external/avb
目录下包含了这个工具,特指 Android Verified Boot 2.0,该目录
包含有tools和相关的库函数。
avb
vimer@host:~/src/aosp$ avbtool info_image --image out/target/product/generic_x86_64/vbmeta.img
Minimum libavb version: 1.0
Header Block: 256 bytes
Authentication Block: 576 bytes
Auxiliary Block: 1984 bytes
Algorithm: SHA256_RSA4096
Rollback Index: 0
Flags: 1
Release String: 'avbtool 1.1.0'
Descriptors:
Chain Partition descriptor:
Partition Name: system
Rollback Index Location: 1
Public key (sha1): cdbb77177f731920bbe0a0f94f84d9038ae0617d
Prop: com.android.build.vendor.os_version -> '10'
Hashtree descriptor:
Version of dm-verity: 1
Image Size: 117182464 bytes
Tree Offset: 117182464
Tree Size: 929792 bytes
Data Block Size: 4096 bytes
Hash Block Size: 4096 bytes
FEC num roots: 2
FEC offset: 118112256
FEC size: 933888 bytes
Hash Algorithm: sha1
Partition Name: vendor
Salt: 500a5d4b28c5b94e8053a56a25f0a4a4455937e0
Root Digest: 3d0fbf077fa15a1a04600e835153516fbcb9e3fc
Flags: 0
如果想弄清楚一个文件的作用,要知道三个方面的内容:
就我目前接触到的aosp的soong系统,这三个对我而言并不是特别的清晰。当然,如果
真要说这块的大体位置,我可以绝对保证,相关的中间文件位于out/.相关的目录
。
soong有一个自带的logger,可以看一下下面这个目录:
vimer@host:~/src/aosp$ ls build/soong/ui/
build logger metrics status terminal tracer
看一下 build\soong\cmd\soong_ui\main.go 这个文件中如何使用logger.
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"android/soong/ui/build"
"android/soong/ui/logger"
"android/soong/ui/metrics"
"android/soong/ui/status"
"android/soong/ui/terminal"
"android/soong/ui/tracer"
)
这里先铺垫一下go的基本使用方法。 关键词 package main
是指定了
由这个main.go文件生成了一个main的包,这样,在其他go文件中,只要
使用import命令,就比如android/soong/ui/logger
的引用方式,我们
就可以直接使用main.xx()的方式使用main文件定义的方法。
很明显,main.go把logger模块(感觉包不如模块熟悉些)引入到main.go 中,比如:
...
writer := terminal.NewWriter(stdio)
defer writer.Finish()
log := logger.New(writer)
defer log.Cleanup()
.....
if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
if !strings.HasSuffix(start, "N") {
if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
log.Verbosef("Took %dms to start up.",
time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
}
}
...
我们可以看到log.verbosef()方法已经生效,那么这个log文件位于什么地方呢?具体应该可以在代
码中定位到的,不过我现在没有去查找,据我了解的是在out/soong.log
文件下。
但是这个方法对于某些文件并不能凑效,比如,我想调试下build/soong/cc/cmakelists.go
的某些具体量,但是不知道为什么根本加不进去logger模块,报错的:
build/soong/cc/cmakelists.go:25:2: can't find import: "android/soong/ui/logger"
非常简答,只能是作为过渡方法:
...
f_1, _ := os.Create("out/yubo_soong_include.log")
defer f_1.Close()
f_1.WriteString("yubo\n")
f_1.WriteString(fmt.Sprintf("project(%s)\n", ccModule.ModuleBase.Name()))
...
这样自定义输出log文件的好处是: 清晰 只打印自己想要的内容,但缺点是输出的内容难以控制, 需要适配,比如:某个模块的某个方法到底能返回什么样类型的值,还需要看代码去确定。
但是,先这样凑合着使用吧。
c++的虚函数与面向对象的多态
的思想紧密结合,这篇文章
简单介绍了多态的一些概念,这个值得下面进行总结。
多态主要有两种:
目前,本博客已经就编译时多态进行了一些总结,这篇文 章主要总结运行时多态的用法,也就是虚函数的用法。
举个简单的例子热身.
using namespace std;
class B {
public:
virtual void s() = 0; // pure Virtual Function
};
class D:public B{
public:
void s(){
cout <<"Virtual function in Derived class";
}
};
int main(){
B *a;
D obj;
a = &obj;
a->s();
}
为了更好的说明虚函数的用法,先看一个最基本的例子:
#include <iostream>
using namespace std;
class base {
public:
virtual void print() // keyword virtual
{
cout << "print the base class" << endl;
}
void show(){
cout << "show the base class" << endl;
}
};
class derived : public base {
public:
void print()
{
cout << "print derived class " << endl;
}
void show()
{
cout << "show derived class" << endl;
}
};
int main()
{
base* bptr;
derived d;
bptr = &d;
// vitual function, binded at runtime
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
}
输出结果为:
print derived class
show the base class
这里最显著的一个地方在于,在同一个派生类(有地址引用)调用有无
virtual修饰的方法是不一样的。
如果用我自己的话说就是,虚函数改变了基类的成员函数,这还是在不修改
基类代码的情况下。那么,在已有继承的方法前提下,这个特点还有意义吗?
或者说,虚函数到底是做什么的?这不仅仅对我这样的初学者是一种疑惑,
同样的,stackoverflow
也有类似的疑惑。Task is cheap,show me the code.
example 1
:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "I' m eating generic food" << endl;
}
};
class Cat : public Animal{
public:
void eat() { cout << "I am eating a rat " << endl; }
};
int main()
{
Animal *animal = new Animal;
Cat *cat = new Cat;
animal->eat(); // I am eating generic food
cat->eat(); // I am eating a rat
}
都没问题吧,但是,如果利用一个中间函数去调用情况又不一样了,比如:
void func(Animal *xyz) {
xyz->eat();
}
int main()
{
Animal *animal = new Animal;
Cat *cat = new Cat;
func(animal);
//I' m eating generic food
func(cat);
//I' m eating generic food ''
}
这个时候就出现问题了,全部输出了基类的方法。如果还有dog pig monkey
啥的都能乱死了,那怎么办?
关键就是在基类的方法加上virtual
就可以了。
其实,在我看来,加上这个关键词并不能有助于真正理解这个问题:问题的 关键在于怎么使用基类和派生类才会导致这个问题的产生。
如果在func(Animal *xyz)
使用 cat 去定义函数参数类型则是失败的。
这是一个编程题目大体的意思是:
一个基类: Person, 内含 name age两个成员变量,getdata()和putdata()两个方法,负责分别得到和输出两个成员变量。
派生类Professor多加一个publications的变量,两个成员方法不变,只不过在putdata方法中 还需要自动展示一个id号。
派生类Student需要一个marks[6]去存储6门课程成绩,最后输出。
代码如下:
/*
* File Name: print_test.cpp
* Author: Bo Yu
* Mail: [email protected]
* Created Time: 2020年04月26日 星期日 09时00分43秒
*/
#include <iostream>
#include <cstring>
using namespace std;
class Person {
public:
string name;
int age;
virtual void getdata() {
cin >> this->name >> this->age;
}
virtual void putdata() {
cout << this->name << " " << this->age << endl;
}
};
class Professor : public Person {
public:
Professor() {
this->cur_id = ++id;
}
int publications;
static int id;
int cur_id;
void getdata() {
cin >> this->name >> this->age >> this->publications;
}
void putdata() {
cout << this->name << " "
<< this->age << " "
<< this->publications << " "
<< this->cur_id << endl;
}
};
int Professor::id = 0;
class Student : public Person {
#define NUM_OF_MARKS 6
public:
Student() {
this->cur_id = ++id;
}
int marks[NUM_OF_MARKS];
static int id;
int cur_id;
void getdata() {
cin >> this->name >> this->age;
for (int i=0; i<NUM_OF_MARKS; i++) {
cin >> marks[i];
}
}
void putdata() {
int marksSum = 0;
for (int i=0; i<NUM_OF_MARKS; i++) {
marksSum += marks[i];
}
cout << this->name << " "
<< this->age << " "
<< marksSum << " "
<< this->cur_id << endl;
}
};
int Student::id = 0;
int main(){
int n, val;
cin>>n; //The number of objects that is going to be created.
Person *per[n];
for(int i = 0;i < n;i++){
cin>>val;
if(val == 1){
// If val is 1 current object is of type Professor
per[i] = new Professor;
}
else per[i] = new Student; // Else the current object is of type Student
per[i]->getdata(); // Get the data from the user.
}
for(int i=0;i<n;i++)
per[i]->putdata(); // Print the required output for each object.
return 0;
}
需要说明的一点是:
类中的静态成员变量必须在类外定义
,这一点是由于static变量的生存周期决定的。
我自己的代码与这份代码已经具有85%的相似性了,但是就是输出不对。
首先提前反思下,这个操作很有必要吗?正常来说,我们都应该是在开发分支上开发, 然后验证 最后合入到 master,最后再git push.
我用到这个场景的原因是:我的博客在好几个平台上都有,自己的云主机、公司的工作站或者 还有自己的笔记本上偶尔也有git push。这东西一多的问题就是:master分支经常忘记git pull. 这样导致如果直接在master分支上开发,下载下来的代码经常冲突。
git checkout -b dev_br
git push origin dev_br:master
继承的特性丰富了c++的使用,当然也有一定的使用门槛,现在简单介绍一点 这方面的知识,以备将来使用。
下面的程序代码是一个最简单的使用继承的案例,我们来看一下:
class Triangle{
public:
void triangle(){
cout<<"I am a triangle\n";
}
};
class Isosceles : public Triangle{
public:
void isosceles(){ // 等腰 三角形
cout<<"I am an isosceles triangle\n";
}
//Write your code here.
};
int main(){
Isosceles isc;
isc.isosceles();
isc.description();
isc.triangle();
return 0;
这一个是使用子类的实例化对象,继承了父类的方法。注意,这里没有解释一
些细节的问题,比如说public
的属性什么的,只是在感官层面上进行一个概
括。
当然,还可以继续继承下去.
class Equilateral : public Isosceles{
public:
void equilateral(){
cout << "I am an equilateral triangle\n";
}
};//Write your code here.
// 使用的话 ,可以直接实例化 Equilateral
int main(){
Equilateral eqr;
eqr.equilateral();
eqr.isosceles();
eqr.triangle();
return 0;
}
其实这里面对有关权限的制约还是非常复杂的,我这里暂时不打算讲述这些,毕竟我自己没有 尝试这些东西,那就先整下自己已经涉及到的内容。
这是什么意思?比如说,我的父类中有一个方法display ()
,这个方法就是用来展示父类内部
的成员变量,但是呢,父类不提供构造方法,而是通过子类去给父类的成员变量赋值,这听起
来很变态,但是,这是hackeranker的一道题目
技巧在于在子类中使用keyword using
,这个时候,你在子类中父类的变量(这个变量必须设置
为 public
)在子类中就可以自由的使用了.
#include <iostream>
using namespace std;
/*
* Create classes Rectangle and RectangleArea
*/
class Rectangle{
public:
int width;
int height;
public:
void display(){
cout << width << " " << height << endl;
}
};
class RectangleArea : public Rectangle {
public:
using Rectangle::width; // 直接调用父类的成员
using Rectangle::height;
public:
void read_input(){
cin >> width >> height;
}
void display(){
cout << width * height << endl;
}
};
int main()
{
RectangleArea r_area;
r_area.read_input();
/*
* Print the width and height
*/
r_area.Rectangle::display();
/*
* Print the area
*/
r_area.display();
return 0;
}
对于这个方案的处理,我不知道还有没有更好的处理呢?