可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I can't compile my Android Kotlin project.
I have no idea what is this...
Gradle log:
error: supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class android.support.v7.app.AppCompatActivity, unresolved supertypes: SupportParentable
build.gradle (app)
buildscript {
ext.android_plugin_version = "2.3.3"
ext.kotlin_version = '1.1.2-5'
repositories {
maven { url 'https://maven.google.com' }
maven { url "https://jitpack.io" }
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:$android_plugin_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
maven { url 'https://maven.google.com' }
maven { url "https://jitpack.io" }
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
def compat_version = '26.+'
def play_services_version = '11.0.1'
def firebase_version = '9.6.1'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.site.app"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = ["library" : "true"]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
javaMaxHeapSize "4g"
}
dataBinding {
enabled true
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
kapt {
generateStubs = true
correctErrorTypes = true
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
// android
compile 'com.android.support:multidex:1.0.1'
compile "com.android.support:appcompat-v7:${compat_version}"
compile "com.android.support:design:${compat_version}"
compile "com.android.support:cardview-v7:${compat_version}"
compile "com.android.support:recyclerview-v7:${compat_version}"
compile "com.android.support:gridlayout-v7:${compat_version}"
compile "com.google.android.gms:play-services:${play_services_version}"
compile "com.google.android.gms:play-services-ads:${play_services_version}"
compile "com.google.android.gms:play-services-maps:${play_services_version}"
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.maps.android:android-maps-utils:0.4+'
kapt "com.android.databinding:compiler:$android_plugin_version"
// kotlin
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
回答1:
In my case I had a library module with an abstract application class implementing Dagger's HasActivityInjector
and an application module with a dependency to the library and its own (non-abstract) application class extending the base app class.
My Gradle dependencies were implementation
s, and therefore not accessible to the application module's application class (even though there were no imports there, otherwise the issue would be immediately obvious as you'd get a 'could not resolve' error. The fix was to replace dagger implementation
dependencies with api
, that makes them available to dependant modules as well.
回答2:
I fixed it by changing the order of plugins call!
build.gradle
buildscript {
ext.android_plugin_version = '2.3.3'
ext.kotlin_version = '1.1.3-2'
repositories {
jcenter()
mavenCentral()
maven { url "https://maven.google.com" }
maven { url 'https://maven.fabric.io/public' }
maven { url "https://jitpack.io" }
}
dependencies {
classpath "com.android.tools.build:gradle:$android_plugin_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
classpath 'io.fabric.tools:gradle:1.+'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url "https://maven.google.com" }
maven { url 'https://maven.fabric.io/public' }
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.sample.app"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
debug {
ext.alwaysUpdateBuildId = false
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
javaMaxHeapSize "4g"
}
dataBinding {
enabled true
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
abortOnError false
disable 'InvalidPackage'
}
}
kapt {
generateStubs = true
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
// kotlin
kapt "com.android.databinding:compiler:$android_plugin_version"
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
// android
compile 'com.android.support:multidex:1.0.1'
compile "com.android.support:appcompat-v7:${compat_version}"
compile "com.android.support:design:${compat_version}"
compile "com.android.support:cardview-v7:${compat_version}"
compile "com.android.support:recyclerview-v7:${compat_version}"
compile "com.android.support:gridlayout-v7:${compat_version}"
compile "com.android.support:support-vector-drawable:${compat_version}"
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile "com.google.android.gms:play-services-ads:${play_services_version}"
compile "com.google.android.gms:play-services-maps:${play_services_version}"
compile "com.google.android.gms:play-services-gcm:${play_services_version}"
compile 'com.google.maps.android:android-maps-utils:0.4+'
// fabric
compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
transitive = true;
}
// logger
compile 'com.orhanobut:logger:2.1.1'
// dexter
compile 'com.karumi:dexter:4.1.0'
// firebase
compile "com.google.firebase:firebase-messaging:${firebase_version}"
// persistence
compile "android.arch.persistence.room:runtime:1.0.0-alpha3"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha3"
// facebook
compile 'com.facebook.android:facebook-android-sdk:4.+'
// retrofit
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
// gson
compile 'com.google.code.gson:gson:2.8.1'
// jobs
compile 'com.evernote:android-job:1.1.11'
// chart
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
// fresco
compile 'com.facebook.fresco:fresco:1.3.0'
// indicator
compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
}
回答3:
Before trying anything else, try:
Android Studio -> File -> Invalidate cache/restart
This worked for me.
回答4:
For me the problem was child module not using AndroidX Activity. I guess the class names clashed.
Added this to resolve:
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
回答5:
On my case I had to add the required dependency on my main module and compile the last version with:
android {
configurations.all {
resolutionStrategy.force "com.squareup.picasso:picasso:${picassoVersion}"
}
}
回答6:
Update your google play gradle version to the latest
回答7:
I was getting error for below lib in app level build.gradle
implementation 'com.google.firebase:firebase-iid:20.0.2'
So the solution is the order in project level build.gradle, So
before solving error the structure was
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
mavenCentral()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
classpath 'com.google.gms:google-services:4.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
After Solving the error the struture is :
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
maven {
url "https://maven.google.com"
}
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
classpath 'com.google.gms:google-services:4.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven {
url "https://maven.google.com" // Google's Maven repository
}
google()
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
By default it tries to download using google() as it comes first in the sequence but when we move maven on top of all then it creates the build again using maven and the build succeeds. Cheers :)
回答8:
When setting up the crashlytics to my app, I used "recommended" dependency as well, as shown below. But this was the part causes error.
// Firebase Crashlytics
// (Recommended) Add the Google Analytics dependency.
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
Removing "analytics" line solved my problem.
// Firebase Crashlytics
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
Hope this helps someone else too,
best