360 FireLine Plugin是360团队针对安卓开发提供的一个功能插件,适用于对安卓APP安全检查规则。目前火线扫描规则共覆盖六大类:
-
APP安全检查
-
代码规范检查
-
内存泄露检查
-
日志输出检查
-
空指针检查
-
多线程检查
提高开发效率必备!
例如:不合理的Activity组件导出会导致拒绝服务,以下是检查之前和之后的代码编写对比,代码示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = getIntent();
String url = i.getStringExtra("url");//触发规则
}
//同时AndroidManifest.xml文件中的该activity组件设置为导出 <activity android:name="com.qihoo.test.activity.TestActivity" android:exported="true"> </activity>
运行360 fireLine后出现的推荐写法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
Intent i = getIntent();
String url = i.getStringExtra("url");//触发规则
}catch{
//捕获异常做自定义处理
}
}
//AndroidManifest.xml中不需要导出的组件设置为不可导出。当组件需要导出时,进行权限验证或者身份判断。 <activity android:name="com.qihoo.test.activity.TestActivity" android:exported="false"> </activity>
有些时候可能不经意忘记关闭流的读写操作,而且程序能正常运行,但是内存却吃不消,例如:
描述:资源对象没有关闭。请确保输入输出流创建的对象在使用完成后进行了妥善的关闭处理。例如java.io.InputStream,java.io.OutputStream,java.io.Reader,java.io.Writer,java.util.zip.ZipFile,java.net.socket等类创建出的对象。
代码示例:
InputStream in = null;
InputStream in2 = null;
try {
in = new URL("http://www.apache.org").openStream(); //触发规则
in2 = new URL("http://www.apache.org").openStream();//触发规则
} catch (SQLException e) {
e.printStackTrace();
}
推荐写法:
InputStream in = null;
InputStream in2 = null;
try {
in = new URL("http://www.apache.org").openStream();
in2 = new URL("http://www.apache.org").openStream();
} catch (SQLException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(in);//工具类关闭
if(in2!=null){
in2.close();//手动关闭
}
}
强烈推荐LeakCanary
简介:谁也不会喜欢 OutOfMemoryError,简称OOM。在处理大量的bitmap的时候,容易引发内存泄漏,其实不止在处理图片的时候发生,当有一系列对这个对象的引用,在该对象的生命周期结束的时候,还没有被系统回收掉,内存很快被耗尽,那就会造成内存泄漏,程序崩溃。
内存泄漏对策:利用MAT或者YourKit之类的内存分析工具反复查看发生泄漏的根源,并计算这个对象到GC roots的最短强引用路径,然后反复进行分析、修复。这工作量蛮大的。LeakCanary是Square公司开发的一款在程序未崩溃之前进行对程序内存泄漏检测的第三方开源类库,帮助你在开发阶段方便的检测出内存泄露的问题,使用起来更简单,如图这是一张内存泄漏后的显示报告,很优雅对吧。
LeakCanary在Eclipse中的使用:
首先导入LeakCanary依赖,这里提供官网最新依赖的资源,http://filemarkets.com/fs/5ju9st6go288a82e67/
http://download.youkuaiyun.com/download/seeing_is_believing/9873941
鼠标点击对应项目后,使用快捷键(Alt+Enter),或者选中项目,右键点击,选择Properties ,如图所示:

.

在自定义的Application中进行初始化操作:
public class InitApplication extends Application {
private RefWatcher refWatcher;
public static RefWatcher getRefWatcher(Context context) {
InitApplication initApplication = (InitApplication) context.getApplicationContext();
return initApplication.refWatcher;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
refWatcher=LeakCanary.install(this);
}
}
当然你也可以直接在onCreate方法中写入:LeakCanary.install(this);
接下来配置清单文件:加入两个服务,一个Activity
<application
android:name=".init.InitApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activityandroid:name=".MainActivity">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN" />
<categoryandroid:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.squareup.leakcanary.DisplayLeakService"
android:enabled="false" />
<activity
android:name="com.squareup.leakcanary.internal.DisplayLeakActivity"
android:enabled="false"
android:icon="@drawable/__leak_canary_icon"
android:label="@string/__leak_canary_display_activity_label"
android:taskAffinity="com.squareup.leakcanary"
android:theme="@style/__LeakCanary.Base" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.squareup.leakcanary.DisplayLeakService"
android:enabled="false" />
<activity
android:name="com.squareup.leakcanary.internal.DisplayLeakActivity"
android:enabled="false"
android:icon="@drawable/__leak_canary_icon"
android:label="@string/__leak_canary_display_activity_label"
android:taskAffinity="com.squareup.leakcanary"
android:theme="@style/__LeakCanary.Base" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
代码中配置:
在setContentView(R.layout.activity_main)之后
//内存泄漏检测
//在内存泄漏方面还是leakcanary 和allocation tracker配合着使用最好,
//leakcanary 能够确定对象是否还存活,allocation tracker确定对象分配位置。
RefWatcher refWatcher = InitApplication.getRefWatcher(this);
refWatcher.watch(this);
在onDestory()方法中
RefWatcher refWatcher = InitApplication.getRefWatcher(this);
refWatcher.watch(this);
Eclipse的LeakCanary配置到此结束,可以运行测试了。
LeakCanary在AndroidStudio中的使用:
首先在app中的Builde.gradle中加入:
dependencies {
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1' // or 1.4-beta1
eleaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1' // or 1.4-beta1
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1' // or 1.4-beta1
}
其次与Eclipse配置不同的是无需修改清单文件,自定义Application和Activity中的用法同Eclipse,这里不再重复。
小结:在当下,编写代码越来越需要效率,编写后还要及时对代码进行审核,难免会有疏忽,如果这时有一个助手帮你调试,检查疏漏的地方,那么将大大提高开发效率,节省下来的时间,你可以去外面兜兜风也是不错的。
