Gradle - 无法使用Gradle依赖在Android Studio中导入viewpagerindicator

3

我正在尝试使用ActionBarSherlock和ViewPagerIndicator,但是不知道为什么,ViewPagerIndicator库无法被导入。有没有人有什么想法?

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.viewpagerindicator:library:2.4.0'
}

dependencies {
    compile project(':libraries:facebook')
}

这个导入错误提示“无法解析符号viewpageindicator”

import com.viewpagerindicator.TabPageIndicator;

这是我的“Messages gradle tasks”。
Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\android-sdk\build-tools\19.0.1\aapt.exe package -f --no-crunch -I C:\android-sdk\platforms\android-19\android.jar -M C:\myAppName\app\build\manifests\debug\AndroidManifest.xml -S C:\myAppName\app\build\res\all\debug -A C:\myAppName\app\build\assets\debug -m -J C:\myAppName\app\build\source\r\debug -F C:\myAppName\app\build\libs\app-debug.ap_ --debug-mode --custom-package com.myAppName.app --output-text-symbols C:\myAppName\app\build\symbols\debug
Error Code:
    1
Output:
    C:\myAppName\app\build\res\all\debug\values\values.xml:911: error: Error: No resource found that matches the given name: attr 'vpiTabPageIndicatorStyle'.
    C:\myAppName\app\build\res\all\debug\values\values.xml:914: error: Error retrieving parent for item: No resource found that matches the given name 'Widget.TabPageIndicator'.
    C:\myAppName\app\build\res\all\debug\values\values.xml:934: error: Error: No resource found that matches the given name: attr 'fadeDelay'.
    C:\myAppName\app\build\res\all\debug\values\values.xml:933: error: Error: No resource found that matches the given name: attr 'fadeLength'.
    C:\myAppName\app\build\res\all\debug\values\values.xml:931: error: Error: No resource found that matches the given name: attr 'selectedColor'.


C:\myAppName\app\src\main\res\values\styles.xml
    No resource found that matches the given name: attr 'vpiTabPageIndicatorStyle'.
    Error retrieving parent for item: No resource found that matches the given name 'Widget.TabPageIndicator'.
    No resource found that matches the given name: attr 'fadeDelay'.
    No resource found that matches the given name: attr 'fadeLength'.
    No resource found that matches the given name: attr 'selectedColor'.

Android Studio 的哪个版本?“不被导入”是什么意思? - Scott Barta
@ScottBarta,我刚刚更新了我的问题,并附上了确切的错误信息。 - Georgi Angelov
5个回答

6

无法通过

导入ViewPagerIndicator

compile 'com.viewpagerindicator:library:2.4.0'

因为Maven构件只是Java库的一个jar文件,而VPI依赖于不在jar文件中的Android资源。具有AAR打包的Maven构件确实有资源并且可以工作,但是VPI没有以这种方式打包。它具有apklib打包,但是Android Gradle插件不支持该打包方式。您需要下载源代码并将其设置为android-library模块。我不知道您使用的Android Studio版本,但我敢打赌您导入失败的问题是因为您使用的版本早于0.4.3;从那时起已经修复了该领域的错误。

谢谢解释。我之前不知道这个。更新到0.4.3以上的版本会帮助我导入库吗?还是我仍然需要将其作为Java库导入? - Georgi Angelov
不,您仍然需要在Gradle中将VPI设置为Android库模块。0.4.3只是修复了新添加的依赖项仍然无法正确显示导入和语法高亮的错误,尽管项目构建正常的问题。 - Scott Barta
谢谢。最终我是通过将库添加到库文件夹中而不是通过依赖项来添加它的。 - Georgi Angelov
如果那个能够运行成功,我会感到惊讶。它无法帮助你获取所需的资源。 - Scott Barta

4
您可以使用这个指南来导入AAR:
在您的build.gradle中,在mavenCentral之前将http://dl.bintray.com/populov/maven添加为存储库。
repositories {
    maven { url "http://dl.bintray.com/populov/maven" }
    mavenCentral()
}

像往常一样在你的依赖项中使用:

dependencies {
    compile 'com.viewpagerindicator:library:2.4.1@aar'
}

我尝试在Android Studio中的另一个子项目中进行此操作,但是出现了错误Widget.TabIndicator等样式未找到。你有什么想法我可能做错了什么吗? - Rat-a-tat-a-tat Ratatouille

3

补充一下@serge的答案,如果你在allprojects标签中定义了repositories,你也需要在那里添加它。

allprojects {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        mavenCentral()
    }
}

2

I use

compile 'com.mcxiaoke.viewpagerindicator:library:2.4.1@aar' 

这对我有用。

并确保重新同步gradle并重建项目。


0

我的根目录 build.gradle 文件是:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        mavenCentral()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        mavenCentral()
        jcenter()

    }
}

而且它的依赖是:compile 'com.viewpagerindicator:library:2.4.1@aar'

它的工作就像炸弹一样:-)


我已经尝试过了,它没有报错,但是我无法导入任何类。出了什么问题? - Muhammad Umar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接