支持库和ViewBackgroundHelper

时间:2017-07-20 06:50:38

标签: android android-studio

我对Android Studio 3.0 Canary有疑问。 在所有项目中,我无法在预览工具中使用支持库中的任何视图。它显示渲染错误。在新项目中是同样的问题。

Samople代码:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

并在预览中查看 screenshot

日志:

  

java.lang.NoSuchFieldError:ViewBackgroundHelper       在android.support.v7.widget.AppCompatBackgroundHelper.loadFromAttributes(AppCompatBackgroundHelper.java:46)       在android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:73)       在android.support.v7.widget.AppCompatImageView。(AppCompatImageView.java:66)       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:423)       在org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:481)       在org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:264)       在org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:215)       在com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadClass(LayoutlibCallbackImpl.java:214)       在android.view.BridgeInflater.loadCustomView(BridgeInflater.java:336)       在android.view.BridgeInflater.onCreateView(BridgeInflater.java:155)       在android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)       在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)       在android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:225)       在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)       在android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:863)       在android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)       在android.view.LayoutInflater.rInflate(LayoutInflater.java:837)       在android.view.LayoutInflater.inflate(LayoutInflater.java:489)       在com.android.layoutlib.bridge.bars.CustomBar。(CustomBar.java:90)       在com.android.layoutlib.bridge.bars.NavigationBar。(NavigationBar.java:52)       在com.android.layoutlib.bridge.bars.NavigationBar。(NavigationBar.java:46)       在com.android.layoutlib.bridge.impl.Layout.createNavBar(Layout.java:325)       在com.android.layoutlib.bridge.impl.Layout。(Layout.java:151)       在com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:288)       在com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:382)       在com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)       在com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:591)       在com.android.tools.idea.rendering.RenderTask.lambda $ inflate $ 3(RenderTask.java:739)       at java.util.concurrent.FutureTask.run(FutureTask.java:266)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       在java.lang.Thread.run(Thread.java:745)

的gradle:

android {
    compileSdkVersion 26
    buildToolsVersion 25.0.3
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 26
        versionCode getAutoVersionCode()
        versionName getAutoVersionName()

        applicationId "***"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        multiDexEnabled true

        printf("\n--------" + "VERSION DATA--------" + "\n" + "- PACKAGE: " + applicationId + "\n" + "- CODE: " + versionCode + "\n" +
                "- NAME: " + versionName + "\n- APK: ${archivesBaseName}" + "\n----------------------------\n")

        vectorDrawables.useSupportLibrary = true
    }

    useLibrary 'org.apache.http.legacy'

    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }

    flavorDimensions "versionOfApp"

    productFlavors {
        dev {
            applicationIdSuffix ".dev"
            versionCode getAutoVersionCode()
            versionName getAutoVersionName() + "[dev]"
            dimension "versionOfApp"
        }
        prod {
            versionCode getAutoVersionCode()
            versionName getAutoVersionName()
            dimension "versionOfApp"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

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

    // various
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    // widgets
    compile 'com.mikepenz:materialdrawer:5.9.4'
    compile "com.android.support:cardview-v7:26.0.0-beta1"
    compile "com.android.support:customtabs:26.0.0-beta1"
    //Multidex
    compile 'com.android.support:multidex:1.0.1'
    //Timber
    compile 'com.jakewharton.timber:timber:4.5.1'
    //pager bullet
    compile 'com.robohorse.pagerbullet:pagerbullet:1.0.8'
    //fb
    compile 'com.facebook.stetho:stetho:1.5.0'
    compile 'com.facebook.android:facebook-android-sdk:4.24.0'

    //glide
    compile 'com.github.bumptech.glide:glide:4.0.0-RC1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0-RC1'

    //Photo
    compile 'com.github.chrisbanes:PhotoView:2.1.2'

    //joda
    compile 'net.danlew:android.joda:2.9.5'

    implementation 'com.google.firebase:firebase-messaging:11.0.2'
}

apply plugin: 'com.google.gms.google-services'

2 个答案:

答案 0 :(得分:0)

可以通过添加禁用构建缓存来解决此问题 android.enableBuildCache=false到您的gradle.properties文件并重建项目。你也可以看到关于这个bug here的讨论。

答案 1 :(得分:0)

这是android支持库版本“26.0.0-beta2”

中的错误

使用,

git add -u
git add <new pattern>

compile 'com.android.support:appcompat-v7:26.0.0-beta1'

buildToolsVersion '26.0.0' 

everthing应该可以正常工作!