作者:手机用户2602905817_973 | 来源:互联网 | 2023-01-19 17:15
我在我的Android应用程序中使用dagger2.即使没有错误,它也不会生成匕首组件类.
我在设置中启用了注释处理器并重新启动我的android工作室,但这对我不起作用.我读了这个线程Dagger2没有生成Daggercomponent类并读取一个apt
不推荐使用的线程所以我正在使用annotationProcessor
基本模块build.gradle
apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
baseFeature true
defaultConfig {
minSdkVersion 23
targetSdkVersion 26
versionCode 1
versionName "0.0.1"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
application project(':app')
feature project(":main")
feature project(":tv")
api 'com.android.support:appcompat-v7:26.0.2'
api 'com.android.support.constraint:constraint-layout:1.0.2'
api 'com.android.support:design:26.0.2'
api "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
api "org.jetbrains.anko:anko-commons:$anko_version"
api "android.arch.lifecycle:runtime:1.0.0-alpha9"
api "android.arch.lifecycle:extensions:1.0.0-alpha9"
kapt "android.arch.lifecycle:compiler:1.0.0-alpha9"
api 'com.squareup.retrofit2:retrofit:2.3.0'
api "com.squareup.retrofit2:converter-moshi:2.0.0"
api 'com.google.dagger:dagger:2.11'
kapt 'com.google.dagger:dagger-compiler:2.11'
api 'com.github.bumptech.glide:glide:4.0.0'
kapt 'com.github.bumptech.glide:compiler:4.0.0'
// new version 1.5.2 has some multi dex issue
debugApi 'com.squareup.leakcanary:leakcanary-android:1.5.1'
releaseApi 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
}
repositories {
mavenCentral()
}
apply plugin: 'com.google.gms.google-services'
电视功能build.gradle
apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 23
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation project(':base')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0'
}
项目build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.3-2'
ext.anko_version = '0.10.1'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:3.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
NetComponent.kt
@Component(modules = arrayOf(AppModule::class, NetModule::class))
interface NetComponent {
fun inject(activity: MainActivity)
}
apt
在apt目录中生成dagger类,但是现在没有这样的dagger生成类,即使我在整个项目目录中搜索.
![在此输入图像描述](https://img.php1.cn/3cd4a/189d8/978/7dbdf0f38ad53545.jpeg)
我看到它没有生成DaggerNetComponent
类,因为编译时也没有错误.有谁知道可能是什么问题?
1> Eugen Pechan..:
您的模块是即时应用功能模块.而且看起来kapt还不支持那些.
我无法用源代码支持这个,但这应该有效:
功能模块
图书馆模块+匕首+ kapt
将所有内容从功能模块移动到库模块.然后使功能模块依赖于库模块.
图书馆模块确实支持kapt.
此示例项目在Instant App Feature模块中使用Dagger和Kapt,它可以开箱即用.您的项目设置必须存在其他问题,这与Dagger或注释处理无关.
浏览Instant App Codelab,确保您没有误解任何内容.
然后原来的答案应该有效.
原始答案
使用kapt
而不是annotationProcessor
配置.如:
kapt "android.arch.lifecycle:compiler:1.0.0-alpha9"
kapt 'com.google.dagger:dagger-compiler:2.11'
kapt 'com.github.bumptech.glide:compiler:4.0.0'
如果您正在使用数据绑定,请添加以下内容:
kapt "com.android.databinding:compiler:3.0.0-beta4"
使用不同的构建插件版本时,不要忘记更新版本.
Kotlin插件没有获取annotationProcessor
依赖项,我们必须使用kapt
依赖项.
最后,要使用最新版本的Kotlin注释处理器,请将它放在模块的build.gradle顶部:
apply plugin: 'kotlin-kapt'
在Kotlin 1.1.4com.android.feature
中添加了Kotlin对模块的支持,确保你至少使用它.