17

All of the answers in similar questions talk about manunally editting gradle files. But I've used Android Studio to import the AAR file and checked the build.gradle files and they all seem correct.

My problem is this:

failed imports

I've imported ShowCaseView v5.0.0 AAR but when I try to import the classes in my Tutorial.java file (you can see in red) Android Studio doesn't recognise the classes. The only import that Android Studio recognises is com.github.amlcurran.showcaseview.R.

I've also tried to clean & rebuild the project, and close & reopen Android Studio but it doesn't help.

P.S. please ignore the missing XML files as that was before I copied them into the project.

Gradle files

ShowCaseView-5.0.0 > build.gradle:

configurations.create("default")
artifacts.add("default", file('ShowCaseView-5.0.0.aar'))

My app's build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.giraffeweather"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(':ShowCaseView-5.0.0')
}

The Android Studio project's build.gradle:

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

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

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

allprojects {
    repositories {
        jcenter()
    }
}

The gradle settings file settings.gradle:

include ':app', ':ShowCaseView-5.0.0'
Community
  • 1
  • 1
Ozzy
  • 8,244
  • 7
  • 55
  • 95
  • "checked the build.gradle files and they all seem correct" -- we cannot help you confirm that, since you have not published the `build.gradle` (and, in this case, apparently `settings.gradle`) files. Why aren't you just referencing the AAR as an artifact, as is shown in [the library's documentation](https://github.com/amlcurran/ShowcaseView#project-set-up)? – CommonsWare Mar 16 '15 at 16:13
  • I've checked the answers in similar questions against my build.gradle and settings.gradle files, and it's the same as in those answers. In fact, since Android Studio created them for me I assumed there wouldn't be any problems with it. I don't know about gradle or how to use this `compile 'com.github.amlcurran.showcaseview:library:5.0.0'` command - I think that's what you're referring to. I've just switched from Eclipse ADT to Android Studio and this is the first time I've seen the word "gradle". Besides, I'd still like to know why if I import an AAR I can't reference class files like a JAR. – Ozzy Mar 16 '15 at 19:35
  • "I've checked the answers in similar questions against my build.gradle and settings.gradle files, and it's the same as in those answers" -- that does not mean that we can help you with them, since we cannot see them. "this is the first time I've see then word "gradle"" -- it's in the first bullet in [the Android Studio overview](http://developer.android.com/tools/studio/index.html). There's also documentation on [including dependencies](http://developer.android.com/tools/building/configuring-gradle.html#buildFileBasics), such as the library that you seek. – CommonsWare Mar 16 '15 at 19:46
  • Yes, the point was I didn't want answers about the gradle files and what's in them because none of them helped me :-). Thanks for the links, I'll have a look. @CommonsWare – Ozzy Mar 16 '15 at 19:50
  • So the first link just tells me to do what I did anyway, File > Import Module. I downloaded the library and imported it from the local filesystem, shouldn't be any different to importing it from an online source. The second link describes what's already in my gradle files because I used File > Import Module wizard. @CommonsWare – Ozzy Mar 16 '15 at 20:00
  • "So the first link just tells me to do what I did anyway" -- that page does not refer to importing modules. "shouldn't be any different to importing it from an online source" -- yes, it is different. And I am suggesting that you switch away from that, to what the documentation refers to as "Remote binary dependencies". – CommonsWare Mar 16 '15 at 20:06
  • " that page does not refer to importing modules" Figure 11: "File > New Module", although the example is for an online source, its the same menu to import an AAR locally. – Ozzy Mar 16 '15 at 20:19

2 Answers2

4

Recently, I encountered this very same issue. I had an AAR to import into my project. The library was distributed only as AAR. I resolved it by putting my AAR file inside libs/ folder and added the following line in my app module's gradle:

dependencies {
    ...
    compile files('libs/theFirstLib.aar')
}

You can also add multiple AARs like so:

dependencies {
    ...
    compile files('libs/theFirstLib.aar', 'libs/theSecondLib.aar')
}

If you are using Gradle 3.0.0 or higher, you may need to substitue compile with implementation:

implementation files('libs/theFirstLib.aar')

Worked like a charm!

NOTE: Importing an AAR sometimes results to another "cannot resolve symbol" error, which I resolved here when Android Studio and Gradle don't agree.

user1506104
  • 6,554
  • 4
  • 71
  • 89
2

You need to specify the location of "ShowCaseView-5.0.0.aar" in project's build.gradle file. E.G, the ShowCaseView-5.0.0.aar file is in a folder named "libs" under project's root directory, update you build.gradle file to add following

allprojects {
    repositories {
        flatDir {
            dirs 'libs'
        }
    }
}
Junyong Yao
  • 689
  • 6
  • 7