Android使用leakcanary教程

新建一个Android studio 工程

从第一个activity跳转到第二个acitivity,第二个activity退出存在内存泄漏,等一会leakcanary显示内存泄漏通知如下:

直接贴在新建项目中使用leakcanary git修改记录

diff --git a/app/build.gradle b/app/build.gradle
index 1421326..bfbd842 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -13,6 +13,8 @@ android {
         versionName "1.0"

         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
+        minSdkVersion 22
+        targetSdkVersion 25
     }

     buildTypes {
@@ -25,6 +27,8 @@ android {
         sourceCompatibility JavaVersion.VERSION_1_8
         targetCompatibility JavaVersion.VERSION_1_8
     }
+    compileSdkVersion 30
+    buildToolsVersion '30.0.2'
 }

 dependencies {
@@ -35,4 +39,7 @@ dependencies {
     testImplementation 'junit:junit:4.+'
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
+
+    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.1'
+    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.1'
 }
\ No newline at end of file
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index debd91e..4d4f565 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -2,6 +2,9 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.leakcanary">

+    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
+
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
@@ -18,6 +21,10 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+        <activity android:name=".Main2Activity" android:exported="true">
+        </activity>
+
     </application>

 </manifest>
--- /dev/null
+++ b/app/src/main/java/com/example/leakcanary/Main2Activity.java
@@ -0,0 +1,30 @@
+package com.example.leakcanary;
+
+import android.os.Bundle;
+import android.view.View;
+
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
+
+public class Main2Activity extends AppCompatActivity {
+    private static Thread sRunnable;
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main2);
+
+        sRunnable = new Thread() {
+            @Override
+            public void run() {
+                super.run();
+            }
+        };
+
+        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View view) {
+                finish();
+            }
+        });
+    }
+}

+++ b/app/src/main/java/com/example/leakcanary/MainActivity.java
@@ -1,14 +1,52 @@
 package com.example.leakcanary;

 import androidx.appcompat.app.AppCompatActivity;
+import androidx.core.app.ActivityCompat;

+import android.app.Activity;
+import android.content.Intent;
+import android.content.pm.PackageManager;
 import android.os.Bundle;

+import com.squareup.leakcanary.LeakCanary;
+
 public class MainActivity extends AppCompatActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        if (LeakCanary.isInAnalyzerProcess(this)) {
+            // This process is dedicated to LeakCanary for heap analysis.
+            // You should not init your app in this process.
+            return;
+        }
+        LeakCanary.install(getApplication());
         setContentView(R.layout.activity_main);
+
+        verifyStoragePermissions(this);
+
+        Intent intent = new Intent(this,Main2Activity.class);
+        startActivity(intent);
+
+    }
+
+    private static final int REQUEST_EXTERNAL_STORAGE = 1;
+    private static String[] PERMISSIONS_STORAGE = {
+            "android.permission.READ_EXTERNAL_STORAGE",
+            "android.permission.WRITE_EXTERNAL_STORAGE" };
+
+    public static void verifyStoragePermissions(Activity activity) {
+        try {
+            //检测是否有写的权限
+            int permission = ActivityCompat.checkSelfPermission(activity,
+                    "android.permission.WRITE_EXTERNAL_STORAGE");
+            if (permission != PackageManager.PERMISSION_GRANTED) {
+                // 没有写的权限,去申请写的权限,会弹出对话框
+                ActivityCompat.requestPermissions(activity,
+                        PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
     }
 }

+++ b/app/src/main/res/layout/activity_main2.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <Button
+        android:id="@+id/btn_back"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:text="Button" />
+</LinearLayout>
\ No newline at end of fil

Android studio中配置如下

file-->projeck structure

参考博客:

Android Studio 4.2.2 安装遇到问题和解决方案_m0_54130475的博客-CSDN博客

demo github 地址

git clone [email protected]:hejiangzhou1/leakcanary.git

猜你喜欢

转载自blog.csdn.net/u014630142/article/details/120401353