内存泄漏:空Android项目+ Dagger2 + LeakCanary

时间:2018-03-07 09:30:49

标签: dagger-2

面临这样的问题。 我使用Empty Activity创建一个新项目,并添加项目的依赖项dagger2和leakcanary。 运行应用程序,然后关闭,泄漏程序显示内存泄漏。

添加依赖项

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.3'
    defaultConfig {
        applicationId "xxxxx"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    }
}
//Dagger
implementation "com.google.dagger:dagger:2.14.1"
kapt "com.google.dagger:dagger-compiler:2.14.1"
//Debug
debugImplementation "com.squareup.leakcanary:leakcanary-android:1.5.4"

创建appkication类和init leakcanary

class AppDelegate: Application() {

    override fun onCreate() {
        super.onCreate()
        onApplicationCreate()
        initDagger(this)
    }

    private fun initDagger(app: Application): AppComponent =
            DaggerAppComponent.builder()
                    .appModule(AppModule(app))
                    .dataModule(DataModule())
                    .build()

    private fun onApplicationCreate() {
        if (BuildConfig.DEBUG) {
            if (LeakCanary.isInAnalyzerProcess(this)) {
                return
            }
            LeakCanary.install(this)
        }
    }
}

MainActivity类

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

创建匕首组件

@Singleton
@Component(modules = arrayOf(AppModule::class, DataModule::class))
interface AppComponent {
}

@Module
class AppModule(private val appContext: Application) {

    @Provides
    fun provideContext() = appContext
}

@Module
class DataModule {
}

以下是泄密事件显示的screen

如果去除了匕首2的依赖性,则没有泄漏。 我做错了什么?

0 个答案:

没有答案
相关问题