问题
LeakCanary is a memory leak detection library for Android and Java. LeakCanary
My project is based on android make file system, which relies on some android internal interfaces and custom methods.
How can I add the LeakCanary as a lib into my app to detect memory leak.
My solution: First, I have to build the LeakCanary as a jar file, but how to. as it's a gradle directory structure, and I have never used Gradle before.
Any tip is precious.
回答1:
LeakCanary is not just a JAR - it contains not only the java code, but resources too (png's for example). If you are using ANT, then the only thing you can do is to include LeakCanary as a library project. But I strongly recommend switching to gradle. Because android development team is not going to support importing any library which is not just jar in any user-friendly way in the nearest future (because gradle is priority). And importing as library projects is a painful procedure, which get's more painful when it sometimes comes to including library's dependencies also as library projects. I've done it by hand long ago only because the client used eclipse with ant for big project for ages. Generally, you must do the following:
- Obtain sources: source code, assets, resources...
- Investigate LeakCanary's dependencies. You may look "POM Object Model" here.
- For dependencies, which have more than just java code, you'll need to include them as library again. For others, just download them as jar.
- For assets (fonts, for example) there are an extra step: you must copy them to the project you are working on or you'll get a resource not found exception.
The final step is to enable manifest merger. Add the following line to project.properties of your project:
manifestmerger.enabled=true
It is done to merge all the AndroidManifest.xml from library project into final apk.
回答2:
For Leak Canary, you should work with Android Studio. In Android Studio, In your build.gradle, add this
dependencies {
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
}
And in your Application class, add this line LeakCanary.install(this); in onCreate() method like this
public class ExampleApplication extends Application {
@Override public void onCreate() {
super.onCreate();
LeakCanary.install(this);
}
}
follow this link https://github.com/square/leakcanary for more details.
Hope this might be helpful.
回答3:
This is needed to be part of your application in order for leakcanary to work.
public class ExampleApplication extends Application {
@Override public void onCreate() {
super.onCreate();
LeakCanary.install(this);
}
}
but this is confusing because most of us do not write such kind of class that extends the Application class directly, a work around rather a simple statement will fix this problem,
public class YourClass extends Activity{//AppCompatActivity, A ctionBarActivity or anything
@Override public void onCreate() {
super.onCreate();
LeakCanary.install(getApplication());
}
}
hope this helps!!!
Its better to call this method in
onPostCreate()
回答4:
What you need is to install gradle, then call "gradle assembleRelease" in library dir of leak canary. If everything goes find, you get several aars files in build/outputs directory. These are android libraries. You should reference them as any other android libraries.
回答5:
This answer is probably too late for you, but it might help others. It is possible to add more projects exposed through gradle in a regular project as jar files.
Gradle is just a build tool. These java libraries that you need a still bunch of jar files at the end of the day. So what I do is the following ...
- Install Android Studio if that's not already the case.
- Create a gradle based project in which you can simple add the dependencies you need.
- Sync the project so that it process the gradle file and updates the external libraries.
- In the project explorer, find and expand "External Libraries" and you will find your jar files.
- Copy the jar files wherever you need to have them in other project and voila.
I have done this to import the whole or sometimes parts of the Google Play library, and many other libraries. In fact, it's a common practice for me.
Now, as Deepscorn mentionned it, I notice that LeakCanary has some res/ folders obviously containing Android resources ... and I badly need to include LeakCanary in Titanium SDK right now. So I am going to proceed the same way, but this time, I will add the resources in the titanium project's resources folder and see what happens.
Resources all end up being included and being available in the entire project. I am not away of scoping in android resources. You just to R... or ...getIdentifier(...) to get them.
You will also need to make sure that AndroidManifest entries are copied to an AndroidManifest file that will be compiled.
I will post my results here if I have time. In the mean time, anyone can give this a try and post some feedback.
来源:https://stackoverflow.com/questions/30211913/how-to-use-leakcanary-how-to-add-leakcanary-as-a-jar-to-build-a-apk-with-mk-fi