我有一个构建。
包含apply plugin: 'com.android.library'
的gradle文件。在我的MainActivity。
package com.foo.networkdiagnostics
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatTextView
class MainActivity : AppCompatActivity() {
lateinit var text: AppCompatEditText
lateinit var result: AppCompatTextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
text = findViewById(R.id.sample_text)
result = findViewById(R.id.result)
当我有apply plugin: 'com.android.library'
在我的库的构建。我开始在MainActivity.kt:
Unresolved reference: layout :15
Unresolved reference: id :17
Unresolved reference: id :18
Unresolved reference: id :20
如果我注释掉apply plugin
行,"未解析的引用"错误消失了。是什么导致了这种情况的发生?
这是Android Studio北极狐| 2020.3.1补丁3
编辑:这里是:app build.gradleplugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.foo.networkdiagnostics"
minSdk 26
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
// implementation project(':library')
}
和:library构建。gradle只有apply plugin: 'com.android.library'
请注意,从android 4.1开始,你不必使用"apply"对于插件你可以使用"id"相反。
例句:
before:
apply plugin: 'com.android.library'
after:
plugins {
id 'com.android.library'
}