源码编译环境集成LeakCanary

本文详细介绍如何在源码编译环境中集成LeakCanary,包括配置android.mk文件,引入必要库和aar包,以及设置编译参数等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                        源码编译环境集成LeakCanary

        具体的android.mk编写如下,在最后会附上已经编译OK的jar和aar包。

# Copyright (C) 2009 The Android Open Source Project

#

# Licensed under the Apache License, Version 2.0 (the "License");

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#

#      http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

#


LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_JAVA_LIBRARIES := services
LOCAL_SRC_FILES := $(call all-java-files-under, src)

	
	
LOCAL_STATIC_JAVA_LIBRARIES += litepal \
							    haha   \
                                leakcanary-watcher 
#这里主要是引入V4的jar包
LOCAL_STATIC_ANDROID_LIBRARIES += \
    android-support-v4 \
    android-support-annotations \
    android-support-v7-recyclerview \
    android-support-v7-preference \
    android-support-v7-appcompat \
    android-support-v14-preference \
    android-support-v17-preference-leanback \
    android-support-v17-leanback \

#依赖aar
LOCAL_STATIC_JAVA_AAR_LIBRARIES += leakcanary-analyzer
LOCAL_STATIC_JAVA_AAR_LIBRARIES += leakcanary-android

#添加包名
LOCAL_AAPT_FLAGS := \
              --auto-add-overlay \
              --extra-packages com.squareup.leakcanary \
              --extra-packages gnu.trove

#LOCAL_PROGUARD_FLAG_FILES := proguard.cfg

LOCAL_PACKAGE_NAME := XXX_APK

LOCAL_CERTIFICATE := platform


LOCAL_PROGUARD_ENABLED := disabled
#LOCAL_DEX_PREOPT := false

include $(BUILD_PACKAGE)



include $(CLEAR_VARS)
#指明包的位置,引入第三方jar包
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := litepal:libs/litepal-1.1.1-src.jar  \										
										haha:libs/haha-2.0.4.jar \
    									leakcanary-watcher:libs/leakcanary-watcher-1.6.2.jar \
    									leakcanary-android:libs/leakcanary-android-1.6.2.aar \
    									leakcanary-analyzer:libs/leakcanary-analyzer-1.6.2.aar 
include $(BUILD_MULTI_PREBUILT)


# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))

##linm

     其中参考的博客如下,我对做了一些整理并亲自验证OK: 源码编译环境集成LeakCanary 

####一、前言 在做系统应用的时候,一般都在linus 服务器上面搭建编译环境,编译apk。跟大厂(vivo)合作的时候,验收的最后一关往往是内存泄漏测试,想要保证项目按期验收,我们最好是能在平时开发的时候发现并解决内存泄漏问题,而源码环境无法像gradle 一行代码依赖leakcanary,而是需要通过aar或者jar形式添加依赖。刚好前段时间抽空将leakcanary集成到项目中,现在将过程记录下来,为大家踩个坑。 ####二、撸起袖子 老规矩,百度一下... ![image.png](https://upload-images.jianshu.io/upload_images/11562793-8b8621df077a06ea.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) 搜到一篇文章,嗯,感觉有我要的东西 ![image.png](https://upload-images.jianshu.io/upload_images/11562793-446a980a54bf60a7.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) 点进去一看,额,只是记录了依赖,没给出相应的jar包和arr,版本1.5也不是最新的,所以我还是自己动手吧。 ##### 下载leakcanary源码 传送门 https://github.com/square/leakcanary ##### 编译出 aar 选择assembleDebug ![image.png](https://upload-images.jianshu.io/upload_images/11562793-43e389295da509e0.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) 生成的aar在目录 /build/outputs/aar 下 ![image.png](https://upload-images.jianshu.io/upload_images/11562793-2360141aa17b6f5e.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) leakcanary-android 依赖 leakcanary-analyzer,所以把leakcanary-analyzer-debug.aar也取出来 ![image.png](https://upload-images.jianshu.io/upload_images/11562793-7120eb74d6b5bc0e.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) leakcanary-analyzer 依赖 com.squareup.haha:haha:2.0.4 和 leakcanary-watcher 我们把haha:2.0.4的jar包取出来 ![image.png](https://upload-images.jianshu.io/upload_images/11562793-c1dd44406966df29.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) 同理把leakcanary-watcher 的jar包取出来。 #### 依赖 整理一下需要依赖的jar包和arr haha-2.0.4.jar leakcanary-analyzer-1.6.2.aar leakcanary-android-1.6.2.aar leakcanary-watcher-1.6.2.jar 把上面的aar和jar放到项目的libs目录下,然后打开Android.mk,开始添加依赖 ``` #依赖申明,冒号前是别名,冒号后是jar包路径 LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := haha:libs/haha-2.0.4.jar \ leakcanary-watcher:libs/leakcanary-watcher-1.6.2.jar \ leakcanary-android:libs/leakcanary-android-1.6.2.aar \ leakcanary-analyzer:libs/leakcanary-analyzer-1.6.2.aar \ include $(BUILD_MULTI_PREBUILT) ...省略其它 #依赖jar...省略其它jar包依赖 LOCAL_STATIC_JAVA_LIBRARIES += haha LOCAL_STATIC_JAVA_LIBRARIES += leakcanary-watcher #依赖aar LOCAL_STATIC_JAVA_AAR_LIBRARIES := leakcanary-analyzer LOCAL_STATIC_JAVA_AAR_LIBRARIES += leakcanary-android ...省略其它 #添加包名 LOCAL_AAPT_FLAGS += --extra-packages com.squareup.leakcanary LOCAL_AAPT_FLAGS += --extra-packages gnu.trove ``` 然后在Application中添加初始化代码 ``` import com.squareup.leakcanary.LeakCanary; #oncreate中 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(this); ``` 感觉应该没啥问题,编译一下试试 开始mm 等待中... ####编译失败 ![image.png](https://upload-images.jianshu.io/upload_images/11562793-c905b8d4a55e53c7.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) 非法字符 '\$' ?什么鬼,非法字符哪里来的,肯定是LeakCanary 的,看看去。 ![image.png](https://upload-images.jianshu.io/upload_images/11562793-2ae94c9b68676610.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) 原来是占位符导致的,这个好办,直接把applicationId 写死不就行了 这里就把 ${applicationId} 换成hello_world,然后编译一下,把aar替换成新编译的,然后mm 等待一分钟... ![image.png](https://upload-images.jianshu.io/upload_images/11562793-b36ea1fbd4f18da8.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) 编译成功,install 一下,运行没问题。 那搞个内存泄漏出来看看?![screen.png](https://upload-images.jianshu.io/upload_images/11562793-e92d1a3a1c05738e.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) 嗯,没毛病,那本次源码环境集成LeakCanary工作到此就结束了。 总结: 1.下载LeakCanary 源码,修改manifest中的非法字符’\$‘,然后编译,取出需要的aar和jar 2.然后在Android.mk中添加依赖 为何方便大家集成,依赖我已经整理出来了
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值