如何正确添加appcompat库到android项目?

时间:2015-09-04 14:14:21

标签: android android-gradle facebook-android-sdk android-appcompat

这是我的build.gradle文件:

buildscript {
    repositories {
        mavenCentral()

        // Configuration for Fabric
        jcenter()
    maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

dependencies {
    // 'jar' files in '/libs' folder
    compile fileTree(dir: 'libs', include: '*.jar')

    compile 'com.facebook.android:facebook-android-sdk:4.1.0'
    compile 'com.google.android.gms:play-services-gcm:7.8.0'

    // Fabric
    compile('com.crashlytics.sdk.android:crashlytics:2.+@aar') {
        transitive = true;
    }
}

repositories {
    mavenCentral()

    // Configuration for Fabric
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion 15
    buildToolsVersion 22

    // Define current date and time now to have the same all along the build
    def currentDate = getCurrentDate()
def currentDateAndTime = getCurrentDateAndTime()

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }
    }

    defaultConfig {
        versionCode 2.7
        versionName name
        minSdkVersion 15
        targetSdkVersion 22
    }

    productFlavors {
        x86 {
            flavorDimension "abi"
            ndk {
                abiFilter "x86"
            }
        }
        arm {
            flavorDimension "abi"
            ndk {
                abiFilter "armeabi-v7a"
            }
        }
    }


    buildTypes {
        // Configuration for release candidates
        release {

            // Proguard configuration
            minifyEnabled true
            proguardFile 'procfg.pro'

        }


    lintOptions {
        abortOnError false
    }
}

将依赖项添加到appcompat后:

....
dependencies {
    // new dependency added
    compile 'com.android.support:appcompat-v7:22.0.0'
    // 'jar' files in '/libs' folder
    compile fileTree(dir: 'libs', include: '*.jar')

    compile 'com.facebook.android:facebook-android-sdk:4.1.0'
    compile 'com.google.android.gms:play-services-gcm:7.8.0'

    // Fabric
    compile('com.crashlytics.sdk.android:crashlytics:2.+@aar') {
        transitive = true;
    }
}
....

我收到以下错误:

AGPBI: {"kind":"ERROR","text":"Attribute \"textAllCaps\" has already been defined","sourcePath":"<dir>/colors.xml","position":{"startLine":1},"original":""}
AGPBI: {"kind":"ERROR","text":"Attribute \"track\" has already been defined","sourcePath":"<dir>/colors.xml","position":{"startLine":1},"original":""}
AGPBI: {"kind":"ERROR","text":"Attribute \"thumbTextPadding\" has already been defined","sourcePath":"<dir>/colors.xml","position":{"startLine":1},"original":""}
AGPBI: {"kind":"ERROR","text":"Attribute \"switchMinWidth\" has already been defined","sourcePath":"<dir>/colors.xml","position":{"startLine":1},"original":""}
AGPBI: {"kind":"ERROR","text":"Attribute \"switchPadding\" has already been defined","sourcePath":"<dir>/colors.xml","position":{"startLine":1},"original":""}

我在color.xml文件中没有定义任何值。我想问题可能与facebook sdk库有关。

如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

首先,您可以尝试使用appcompat-v7 22.2.1的最新版本。 如果仍有问题,请使用带有模块名称的gradle来检查依赖项列表:dependencies。如果我的模块是应用程序尝试终端:

.logo::after {
    content: "";
    background: transparent url("/wp-content/themes/porto/images/shadow.png") repeat scroll 0% 0%;
    width: 247px;
    height: 14px;
    position: absolute;
    top: 64px;
    right: 30px;
    min-height: 0px;
}
#main-menu {
    position: relative;
    margin-right: 15%;
    margin-bottom: 2%;
}

(如果您使用的是graddle包装,则为gradlew)。 你应该有这样的东西:

gradle app:dependencies

你可以看到appcompat-v7和facebook-android-sdk都支持-v4。 要从facebook sdk中排除support-v4,您必须写:

 +--- com.android.support:appcompat-v7:22.2.1
    |    \--- com.android.support:support-v4:22.2.1
    |         \--- com.android.support:support-annotations:22.2.1
    \--- com.facebook.android:facebook-android-sdk:4.1.0
         +--- com.android.support:support-v4:[21,22) -> 22.2.1 (*)
         \--- com.parse.bolts:bolts-android:1.2.0

如果你运行上一个gradle app:dependencies,你会看到现在facebook sdk的子目录中没有support-v7。

希望这会对你有所帮助。