在我的办公室中,我们使用由其他团队管理的Artifactory,并且由于无法控制的原因添加外部工件需要一些时间。
我们正在开发一个新项目,并希望使用Artifactory中没有的ButterKnife 10.1.0。为了能够在过程进行过程中正常工作,我添加了ButterKnife作为模块。
对于应用程序的Java部分来说,这很好用,但是Kotlin部分却不能,我敢肯定这是因为我没有正确使用kapt。我创建了一个示例项目来演示该问题-这只是使用kotlin的Android Studios基本的新“ Empty Activity”项目,还有一些非常小的附加功能:
build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
// I added the following line
apply plugin: "kotlin-kapt"
android {
compileSdkVersion 28
defaultConfig {
applicationId "net.drimmie.kaptwithmodules"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
// And I added these two lines
implementation project(':butterknife-10.1.0')
kapt project(':butterknife-10.1.0')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
MainActivity.kt
package net.drimmie.kaptwithmodules
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity;
import android.view.Menu
import android.view.MenuItem
import android.view.View
import butterknife.ButterKnife
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}
// This function demonstrates the problem
fun onViewCreated(view: View, savedInstanceState: Bundle?) {
ButterKnife.bind(this, view)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
当我尝试在模拟器上运行应用程序时,收到以下构建错误:
/Users/drimmro/Code/KaptWithModules/app/src/main/java/net/drimmie/kaptwithmodules/MainActivity.kt: (27, 21): Cannot access class 'butterknife.Unbinder'. Check your module classpath for missing or conflicting dependencies
> Task :app:buildInfoGeneratorDebug