盛艳明的博客


  • 首页

  • 归档

  • 标签

  • 搜索

淘宝Atlas源码解析(一)

发表于 2018-03-17 |

我的csdn博客地址

简述:这篇文章仅仅介绍atlas的基本实现原理。

这里写图片描述

这是atals中bundle加载过程包括class 和 resource

第15步:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(!installingBundles.containsKey(location) && ((bundleDir!=null&&bundleDir.exists()) || dexPatchDir!=null&&dexPatchDir.exists())){
try {
BundleContext bcontext = new BundleContext();
bcontext.bundle_tag = bundleUniqueTag;
bcontext.location = location;
bcontext.bundleDir = bundleDir;
bcontext.dexPatchDir = dexPatchDir;
bundle = new BundleImpl(bcontext);
if (bundle != null) {
bundle.optDexFile();
}
} catch (Exception e) {
if (e instanceof BundleArchive.MisMatchException) {
if (bundleDir.exists()) {
bundle = null;
}
}

可以看到bundle.optDexFile() 点进去看看

1
2
3
4
5
public /*synchronized*/ void optDexFile() {
//getArchive()获取bundle压缩文件的描述返回的是BundleArchive对象
//调用BundleArchive的optDexFile()方法
this.getArchive().optDexFile();
}

接着点

1
2
3
4
5
6
public void optDexFile() {
//BundleArchive 对象仅仅是对 BundleArchiveRevision的一层封装
//BundleArchiveRevision才是对Bundle的so文件的真正描述
//currentRevision 是BundleArchive 的实例指当前Bundle压缩文件对象
currentRevision.optDexFile();
}

接着点

1
2
3
4
5
6
7
public /*synchronized*/ void optDexFile() {
if (isDexOpted()){
return;
}
optDexFileLocked();
}

接着点入optDexFileLocked()

1
2
3
4
5
6
7
8
关键代码 Build.VERSION.SDK_INT>=21
dexFile = (DexFile) RuntimeVariables.sDexLoadBooster.getClass().getDeclaredMethod("loadDex",Context.class,String.class, String.class, int.class, boolean.class).invoke(
RuntimeVariables.sDexLoadBooster,
RuntimeVariables.androidApplication,
bundleFile.getAbsolutePath(),
odexFile.getAbsolutePath(),
0,
interpretOnly);

RuntimeVariables.sDexLoadBooster 是DexLoadBooster() 对象,是在AtlasBridgeApplication 中初始化,在BridgeApplicationDelegate 中赋值给RuntimeVariables.sDexLoadBooster 的。
所以这段反射代码最终执行的方法是DexLoadBooster()的loadDex 方法 代码如下:

1
2
3
4
public DexFile loadDex(Context context, String sourcePathName, String outputPathName, int flags,
boolean interpretOnly) throws IOException {
return AndroidRuntime.getInstance().loadDex(context, sourcePathName, outputPathName, flags, interpretOnly);
}

接着点

1
DexFile.loadDex(sourcePathName, outputPathName, flags)

这就是Android代码加载Dex文件的代码了

现在已经将dex文件加载到内存中封装成对象了,接着查BundleClassLoader的代码,这个流程是class文件的加载过程

1
clazz = ((BundleClassLoader)impl.getClassLoader()).loadOwnClass(classname);
1
2
3
4
5
6
7
8
public Class<?> loadOwnClass(String className) throws ClassNotFoundException {
Class<?> clazz = findLoadedClass(className);
if (clazz == null) {
clazz = findOwnClass(className);
}
return clazz;
}
1
2
3
4
5
6
7
8
9
10
11
private Class<?> findOwnClass(final String classname) {
try {
Class<?> clazz = archive.findClass(classname, this);
return clazz;
} catch (Exception e) {
if (e instanceof BundleArchiveRevision.DexLoadException) {
throw (BundleArchiveRevision.DexLoadException)e;
}
}
return null;
}
1
2
3
public Class<?> findClass(String className, ClassLoader cl) throws ClassNotFoundException {
return currentRevision.findClass(className, cl);
}
1
clazz = dexFile.loadClass(className, cl);

到这里我们的上面拿到的DexFile对象用上了,并且调用了loadClass方法
查看loadClass方法

1
2
3
4
public Class loadClass(String name, ClassLoader loader) {
String slashName = name.replace('.', '/');
return loadClassBinaryName(slashName, loader);
}

点入:

1
2
3
public Class loadClassBinaryName(String name, ClassLoader loader) {
return defineClass(name, loader, mCookie);
}

点入:

1
private native static Class defineClass(String name, ClassLoader loader, int cookie);

发现这是一个native方法,到此结束
总结:Atlas 通过反射为每个Bundle创建了一个ClassLoader ,将so文件中的dex文件加载到内存中以后最终调用defineClass方法加载class文件。

附属DexFile源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package dalvik.system;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
/**
* Manipulates DEX files. The class is similar in principle to
* {@link java.util.zip.ZipFile}. It is used primarily by class loaders.
* <p>
* Note we don't directly open and read the DEX file here. They're memory-mapped
* read-only by the VM.
*/
public final class DexFile {
private int mCookie;
private final String mFileName;
private final CloseGuard guard = CloseGuard.get();
/**
* Opens a DEX file from a given File object. This will usually be a ZIP/JAR
* file with a "classes.dex" inside.
*
* The VM will generate the name of the corresponding file in
* /data/dalvik-cache and open it, possibly creating or updating
* it first if system permissions allow. Don't pass in the name of
* a file in /data/dalvik-cache, as the named file is expected to be
* in its original (pre-dexopt) state.
*
* @param file
* the File object referencing the actual DEX file
*
* @throws IOException
* if an I/O error occurs, such as the file not being found or
* access rights missing for opening it
*/
public DexFile(File file) throws IOException {
this(file.getPath());
}
/**
* Opens a DEX file from a given filename. This will usually be a ZIP/JAR
* file with a "classes.dex" inside.
*
* The VM will generate the name of the corresponding file in
* /data/dalvik-cache and open it, possibly creating or updating
* it first if system permissions allow. Don't pass in the name of
* a file in /data/dalvik-cache, as the named file is expected to be
* in its original (pre-dexopt) state.
*
* @param fileName
* the filename of the DEX file
*
* @throws IOException
* if an I/O error occurs, such as the file not being found or
* access rights missing for opening it
*/
public DexFile(String fileName) throws IOException {
mCookie = openDexFile(fileName, null, 0);
mFileName = fileName;
guard.open("close");
//System.out.println("DEX FILE cookie is " + mCookie);
}
/**
* Opens a DEX file from a given filename, using a specified file
* to hold the optimized data.
*
* @param sourceName
* Jar or APK file with "classes.dex".
* @param outputName
* File that will hold the optimized form of the DEX data.
* @param flags
* Enable optional features.
*/
private DexFile(String sourceName, String outputName, int flags) throws IOException {
mCookie = openDexFile(sourceName, outputName, flags);
mFileName = sourceName;
guard.open("close");
//System.out.println("DEX FILE cookie is " + mCookie);
}
/**
* Open a DEX file, specifying the file in which the optimized DEX
* data should be written. If the optimized form exists and appears
* to be current, it will be used; if not, the VM will attempt to
* regenerate it.
*
* This is intended for use by applications that wish to download
* and execute DEX files outside the usual application installation
* mechanism. This function should not be called directly by an
* application; instead, use a class loader such as
* dalvik.system.DexClassLoader.
*
* @param sourcePathName
* Jar or APK file with "classes.dex". (May expand this to include
* "raw DEX" in the future.)
* @param outputPathName
* File that will hold the optimized form of the DEX data.
* @param flags
* Enable optional features. (Currently none defined.)
* @return
* A new or previously-opened DexFile.
* @throws IOException
* If unable to open the source or output file.
*/
static public DexFile loadDex(String sourcePathName, String outputPathName,
int flags) throws IOException {
/*
* TODO: we may want to cache previously-opened DexFile objects.
* The cache would be synchronized with close(). This would help
* us avoid mapping the same DEX more than once when an app
* decided to open it multiple times. In practice this may not
* be a real issue.
*/
return new DexFile(sourcePathName, outputPathName, flags);
}
/**
* Gets the name of the (already opened) DEX file.
*
* @return the file name
*/
public String getName() {
return mFileName;
}
/**
* Closes the DEX file.
* <p>
* This may not be able to release any resources. If classes from this
* DEX file are still resident, the DEX file can't be unmapped.
*
* @throws IOException
* if an I/O error occurs during closing the file, which
* normally should not happen
*/
public void close() throws IOException {
guard.close();
closeDexFile(mCookie);
mCookie = 0;
}
/**
* Loads a class. Returns the class on success, or a {@code null} reference
* on failure.
* <p>
* If you are not calling this from a class loader, this is most likely not
* going to do what you want. Use {@link Class#forName(String)} instead.
* <p>
* The method does not throw {@link ClassNotFoundException} if the class
* isn't found because it isn't reasonable to throw exceptions wildly every
* time a class is not found in the first DEX file we look at.
*
* @param name
* the class name, which should look like "java/lang/String"
*
* @param loader
* the class loader that tries to load the class (in most cases
* the caller of the method
*
* @return the {@link Class} object representing the class, or {@code null}
* if the class cannot be loaded
*/
public Class loadClass(String name, ClassLoader loader) {
String slashName = name.replace('.', '/');
return loadClassBinaryName(slashName, loader);
}
/**
* See {@link #loadClass(String, ClassLoader)}.
*
* This takes a "binary" class name to better match ClassLoader semantics.
*
* @hide
*/
public Class loadClassBinaryName(String name, ClassLoader loader) {
return defineClass(name, loader, mCookie);
}
private native static Class defineClass(String name, ClassLoader loader, int cookie);
/**
* Enumerate the names of the classes in this DEX file.
*
* @return an enumeration of names of classes contained in the DEX file, in
* the usual internal form (like "java/lang/String").
*/
public Enumeration<String> entries() {
return new DFEnum(this);
}
/*
* Helper class.
*/
private class DFEnum implements Enumeration<String> {
private int mIndex;
private String[] mNameList;
DFEnum(DexFile df) {
mIndex = 0;
mNameList = getClassNameList(mCookie);
}
public boolean hasMoreElements() {
return (mIndex < mNameList.length);
}
public String nextElement() {
return mNameList[mIndex++];
}
}
/* return a String array with class names */
native private static String[] getClassNameList(int cookie);
/**
* Called when the class is finalized. Makes sure the DEX file is closed.
*
* @throws IOException
* if an I/O error occurs during closing the file, which
* normally should not happen
*/
@Override protected void finalize() throws Throwable {
try {
if (guard != null) {
guard.warnIfOpen();
}
close();
} finally {
super.finalize();
}
}
/*
* Open a DEX file. The value returned is a magic VM cookie. On
* failure, an IOException is thrown.
*/
native private static int openDexFile(String sourceName, String outputName,
int flags) throws IOException;
/*
* Open a DEX file based on a {@code byte[]}. The value returned
* is a magic VM cookie. On failure, a RuntimeException is thrown.
*/
native private static int openDexFile(byte[] fileContents);
/*
* Close DEX file.
*/
native private static void closeDexFile(int cookie);
/**
* Returns true if the VM believes that the apk/jar file is out of date
* and should be passed through "dexopt" again.
*
* @param fileName the absolute path to the apk/jar file to examine.
* @return true if dexopt should be called on the file, false otherwise.
* @throws java.io.FileNotFoundException if fileName is not readable,
* not a file, or not present.
* @throws java.io.IOException if fileName is not a valid apk/jar file or
* if problems occur while parsing it.
* @throws java.lang.NullPointerException if fileName is null.
* @throws dalvik.system.StaleDexCacheError if the optimized dex file
* is stale but exists on a read-only partition.
*/
native public static boolean isDexOptNeeded(String fileName)
throws FileNotFoundException, IOException;
}

c语言生成动态链接库复习

发表于 2017-12-16 |

命令实例:生成动态链接库

gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so

使用动态链接库

gcc test.c -L. -ltest -o test    

测试是否动态链接

otool test

–

1、编译参数解析
最主要的是GCC命令行的一个选项:
-shared 该选项指定生成动态连接库(让连接器生成T类型的导出符号表,有时候也生成弱连接W类型的导
出符号),不用该标志外部程序无法连接。相当于一个可执行文件
l -fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通
过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。
l -L.:表示要连接的库在当前目录中
l -ltest:编译器查找动态连接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.so来确
定库的名称
l LD_LIBRARY_PATH:这个环境变量指示动态连接器可以装载动态库的路径。
l 当然如果有root权限的话,可以修改/etc/ld.so.conf文件,然后调用 /sbin/ldconfig来达到同样的目
的,不过如果没有root权限,那么只能采用输出LD_LIBRARY_PATH的方法了。

2、注意
调用动态库的时候有几个问题会经常碰到,有时,明明已经将库的头文件所在目录 通过 “-I” include
进来了,库所在文件通过 “-L”参数引导,并指定了“-l”的库名,但通过ldd命令察看时,就是死活找
不到你指定链接的so文件,这时你要作的就是通过修改 LD_LIBRARY_PATH或者/etc/ld.so.conf文件来指
定动态库的目录。通常这样做就可以解决库无法链接的问题了。

巩固java中Collections中sort方法的用法

发表于 2017-12-09 |

###这里不讲原理了,有兴趣可以追踪java源码

测试用例一:

ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(3);
    list.add(4);
    list.add(2);
    list.add(7);
    list.add(6);

Collections.sort(list);
System.out.println(list)

结论:默认情况下Collections.sort是升序排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Comparator<Integer> comparable = new Comparator<Integer>() {
@Override
public int compare(Integer l, Integer h) {
System.out.println(l+" "+h);
if(l > h){
return -1;
}else{
return 1;
}
}
};
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(3);
list.add(4);
list.add(2);
list.add(7);
list.add(6);
System.out.println(list);
sort(list,comparable);
System.out.println(list);

打印结果


1
2
3
4
5
6
7
8
9
10
11
12
13
[1, 3, 4, 2, 7, 6]
3 1
4 3
2 4
2 3
2 1
7 2
7 3
7 4
6 3
6 4
6 7
[7, 6, 4, 3, 2, 1]

“rsaauthentication”问题

发表于 2017-11-30 |

checkout的单文件合并用法

发表于 2017-11-30 |

问题描述

在我们开发过程中特别是分支比较多的时候会遇到一种场景就是需要
A分支上的某几个文件合并到另一个分支上比如f.java保持多个版本间
的代码同步,那么怎么办呢?git checkout的命令正好可以用上

使用用例

git checkout A 切换到A分支
git checkout B f.java (将B分支上的f文件强行覆盖|copy到分支)

git分支合并经验总结

发表于 2017-11-24 |

点滴积累c指针

发表于 2017-11-23 |

推荐的质料

提取密码:6nh9

未完成待续

gdb学习笔记

发表于 2017-11-22 |

系统环境:mac

下载安装完gdb后第一步是添加证书否者会出现以下提示

(please check gdb is codesigned - see taskgated(8))

mac下添加证书连接

  1. 编译生成可执行的文件(加上-g会生成可调试的文件)

    gcc -g test.c -o test
    
    会多生成一个文件,这个文件在我电脑上的名字是.dSYM
    //todo 查阅资料了解这个文件的作用
    

常用命令:(注:使用enter| 空格键是重复上一个命令)

1
2
3
4
5
6
7
8
9
10
11
12
l(list) :列出源码
n(next) :单点调试(就是单条语句执行)
p(print):输出你想打印的值
break :打断点 例如 break 17表示把断点打在第17行
info break :查看所有断点的信息
bt: 查看函数的堆栈信息
finish:退出函数
c: 继续运行
q: 退出gdb

Android爬坑记录

发表于 2017-11-19 |

在开发过程中遇到这么一个需求:
需要一个可折叠的列表并且在子列表中有EditText的输入和回显的交互

可折叠的列表很自然的就想到了google的原生控件ExpandableListView
但是知道为什么ExpanableListView好像并不怎么友好,在使用过程中发现好多不可思议的地方比如说getChildView会刷两次。好了闲话不多说,问题要逐一解决,先来谈谈解决ExpandListView和EditView的问题。

思路一:
为EditView设置内容监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
holder.label_et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String text = holder.label_et.getText().toString();
if(!TextUtils.isEmpty(text)){
item.setLable(text);
}
}
});

发现问题很诡异,afterTextChanged 被执行了很多遍,最后失败告终。
看日志感觉解释不清楚就先不贴了,有时间研究下。

思路二:在EditText焦点发生变化的时候再获取文本内容

1
2
3
4
5
6
7
8
9
10
11
holder.label_et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
String text = holder.label_et.getText().toString();
if(!TextUtils.isEmpty(text)){
item.setLable(text);
}
}
}
});

完美解决。

listView和RadioButton的故事

发表于 2017-11-06 |

问题描述:
在xml布局中为RadioButton设置默认选中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<RadioGroup
android:id="@+id/rg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="60"
android:orientation="horizontal"
android:paddingLeft="20dp">
<RadioButton
android:id="@+id/yes_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是"
android:textColor="@android:color/black" />
<RadioButton
android:id="@+id/no_rb"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:checked="true"
android:text="否"
android:textColor="@android:color/black" />
</RadioGroup>

发现在ListView 初始化以后id 为no_rb的RadioButon无法取消掉,
原因不明。

解决办法:
在代码中设置默认选中,
RadioButton rb = (RadioButton) view.findViewById(R.id.no_rb);
rb.setChecked(true);

123
盛艳明

盛艳明

29 日志
18 标签
RSS
GitHub ZhiHu
© 2017.06 - 2018 盛艳明
由 Hexo 强力驱动
主题 - NexT.Muse
访问人数 人次 总访问量 次