今天新建Android项目时报错,MainActivity.java报错can’t resolve symbol ‘AppCompatActivity’,但打开之前的项目就没有问题。
我也没搞清原因,但怀疑是重复导入了v4和v7包,因此发生冲突。复制之前的appl里dependencies的代码过来就解决了,可以参考下我的代码。
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.administrator.myapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
}
此外尽量使用新版本的包可以避免很多问题,安卓升级时会修复很多bug,比如compileSdkVersion 28就是最新版本。good luck~