Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

gradle - Failed to load AppCompat ActionBar with unknown error android studio 3.0

After updating android studio to version 3.0, I can't preview layout of my app, I get the error like:

'Failed to load AppCompat ActionBar with unknown error'.

How can I fix this? but if I run the app on my device phone, its run normally.

This is my Gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId 'com.halloo'
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    testCompile 'junit:junit:4.12'
}

dependencies {
    compile 'com.squareup.okhttp3:okhttp:3.5.0'
}
dependencies {
    compile 'com.android.support:support-v4:24.+'
}
dependencies {
    compile 'com.android.support:cardview-v7:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
}

dependencies {
    compile 'com.android.support:support-v4:24.+'
    compile 'junit:junit:4.12'
}

dependencies {
    compile 'com.android.support:support-v4:24.+'
    compile 'com.mikhaellopez:hfrecyclerview:1.0.0'
}

Thank you very much for your time and assistance in this matter.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, you need to use the same version of compileSdkVersion, buildToolsVersion, targetSdkVersion, and support library version. I see that you want to use buildToolsVersion '26.0.2'. So, change all of them to version 26.

Second, you need to clean up your build.gradle. There is no need for duplicate dependencies.

Third, try clean and build your project. As the last resort, try File -> Invalidate Caches/Restart...

Your app build.gradle should be something like this:

apply plugin: 'com.android.application'

android {
  compileSdkVersion 26
  buildToolsVersion '26.0.2'
  defaultConfig {
    applicationId 'com.halloo'
    minSdkVersion 16
    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'
    }
  }
  productFlavors {
  }
}

dependencies {
  compile fileTree(include: ['*.jar'], dir: 'libs')
  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
  })
  compile 'com.android.support:appcompat-v7:26.1.0'
  compile 'com.android.support:design:26.1.0'
  compile 'com.android.support:support-v4:26.1.0'
  compile 'com.squareup.okhttp3:okhttp:3.5.0'
  compile 'com.android.support:recyclerview-v7:26.1.0'
  compile 'com.mikhaellopez:hfrecyclerview:1.0.0'
  testCompile 'junit:junit:4.12'
}

You also need to check for your project build.gradle. It should contain build:gradle:3.0.0 (as @dheeraj-joshi has pointing out), something like this:

buildscript {
  repositories {
    jcenter()
    mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0'
  }
}

allprojects {
  repositories {
    jcenter()
    mavenCentral()
//    maven { url "https://maven.google.com" }
    google()
  }
}


task clean(type: Delete) {
  delete rootProject.buildDir
}

Then, you need to check your gradle version. It should at least using gradle-4.1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...