Android常见问题整理

问题描述

1
2
3
4
5
6
7
8
!SESSION 2017-08-29 15:27:40.107 ----------------------------------------------- 
eclipse.buildId=unknown java.version=1.8.0_112-release java.vendor=JetBrains s.r.o BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=zh_CN Command-line arguments: -os win32 -ws win32 -arch x86_64 -data @noDefault
...
!ENTRY org.eclipse.osgi 4 0 2017-08-29 15:27:41.441
!MESSAGE Application error !STACK 1 java.lang.NullPointerException
at org.eclipse.core.runtime.URIUtil.toURI(URIUtil.java:280)
at org.eclipse.e4.ui.internal.workbench.ResourceHandler.loadMostRecentModel(ResourceHandler.java:127)
...

解决办法

  1. 进程管理器中杀死monitor.exe进程;
  2. 删除$HOME/.android/monitor-workspace目录
Read More

AIDL

Binder是一个工作在Linux层面的驱动,这 一段驱动运行在内核态。Binder本身又是一种架构,这种架构提供了服务端、Binder驱动和客户端三个模块。

服务端

Binder服务端实际上就是一个Binder类的对象,当我们创建一个Binder对象的时候,Binder内部就 会启动一个隐藏线程,该线程的主要作用就是接收Binder驱动发送
来的消息,那么Binder驱动为 什么会给Binder服务端的线程发送消息呢?原因很简单,我们在客户端调用服务端的时候并不能直接调用服务端相应的类和方法,
只能通过Binder驱动来调用。当服务端的隐藏线程收到Binder 驱动发来的消息之后,就会回调服务端的onTransact方法,我们来看看这个方法的方法头:

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
/**
@param code 指定客户端要调 用服务端的哪一个方法
@param data 客户端传来的参数
@param reply 表示服务端返回的参数
@param flags 表示客户端的调用是否有返回值,0表示服务端执行完成之后有返回值,1表示服务 端执行完后没有返回值。
*/
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException


public class MyAddBinder extends Binder {
private final static int ADD = 1;

@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
switch (code) {
case ADD:
data.enforceInterface("MyAddBinder");
int a = data.readInt();
int b = data.readInt();
int add = add(a, b);
reply.writeInt(add);
return true;
}
return super.onTransact(code, data, reply, flags);
}

public int add(int a, int b) {
return a + b;
}
}

public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyAddBinder();
}
}

Read More

包体积优化

删除无用资源

底层库优化

有时候,可能只使用了开源库中对某部分功能,可以对其源码进行删减重新打包。

图片压缩

放入项目之前,对图片进行压缩,比如使用tinypng

图片资源格式的选择

优先选择使用webp格式

Gradle配置

minifyEnabled true //删除无用代码
shrinkResources true  //删除无用资源

Read More

Android中的集合

SparseArray

SparseArray由两个数组mKeys和mValues存放数据;其中key的类型为int型,这就显得SparseArray比HashMap更省内存一些。SparseArray存储的元素都是按元素的key
值从小到大排列好的。使用二分查找来判断元素的位置,数据量较小时比HashMap更快。

  • SparseIntArray:当map的结构为Map<Integer,Integer>的时候使用,效率较高。
  • SparseBooleanArray: 当map的结构为Map<Integer,Boolean>的时候使用,效率较高。
  • SparseLongArray: 当map的结构为Map<Integer,Long>的时候使用,效率较高。
Read More