10

I've never used Gradle before so I'm completely lost!

I've added SlidingMenu as a library and I have access from my project to all the SlindingMenu stuff, but trying to compile will give me this error:

Gradle: package com.jeremyfeinstein.slidingmenu.lib does not exist

I'm using Android Studio (so IntelliJ) and this is my gradle.build

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'
dependencies {
    compile files('libs/android-support-v4.jar')
}
android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 17
    }
}

Thanks in advance

Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • I have the same problem with this and also with the ActionbarSherlock libraries. I guess it's a problem with the gradle and it's handling of external library projects. But there isn't any clear solution out there. Moreover, some people could import such libraries as a standard module and use it without problem, but there are others (like me and you) who get such errors with the gradle... – Seishin May 20 '13 at 20:01
  • @JJD no, it wasn't. In the end I gave up, waiting for a more stable AndroidStudio version. I'm not sure if now it could be fixed or resolved. – Enrichman Feb 19 '14 at 09:47
  • @Enrichman Did you look through the [forks](https://github.com/jfeinstein10/SlidingMenu/network)? There are a couple of them set up for Gradle build. – JJD Feb 19 '14 at 10:50

6 Answers6

14

Assuming you have added SlidingMenu.jar into libs folder, right click on it -> Add as library. Then change in gradle.build:

Before:

dependencies {
    compile files('libs/android-support-v4.jar')
}

After:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

This will include all your jar files.

leadrien
  • 426
  • 2
  • 7
  • Nope, I don't have it as a library but as another project. Then the main project has a dependency on the SlidingMenu project. – Enrichman May 21 '13 at 12:57
  • Don't use IntelliJ's (Studio's) UI to add dependency, only edit build.gradle. I'd recommend you read on multi-project setup. – Xavier Ducrohet May 22 '13 at 01:45
  • 2
    If you know how to fix this please provide us an answer. Btw seem that with gradle you can import jar or dependency from maven repository or ivy, don't know how to link external projects. – Enrichman May 22 '13 at 10:08
  • 1
    @Enrichman To link to an external project, either push it to a repository (as an AAR package) and then depend on it as an AAR file, something ala `compile "com.actionbarsherlock:actionbarsherlock:4.4.0@aar"`, or create a libs folder and link to it using a gradle dependencies block. Here is a [resource](http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup) describing that as well. – Lo-Tan Dec 19 '13 at 00:14
  • I had an issue with external libraries after exporting from eclipse to android studio. Right clicking the `libs/*.jar` files and selecting `Add As Library...` fixed all my problems. Thank you. – Rob Mar 20 '14 at 03:10
  • Even though I added the line to the gradle build file, it required me to do the right click > Add As Library step before it worked, so thanks! – Gerard Feb 15 '15 at 14:05
2

I had the same problem. Adding sliding-menu-lib from with gradle-build as android library did help me.

My project structure is as:

-MyDemoProject
-build.gradle
-settings.gradle
--MyDemo
--build.gradle
--libs
---sliding-menu-lib
----res
----src
----AndroidManifest.xml
----build.gradle
--src

To make all the stuff working your settings.bundle should have this contents:

include ':MyDemo' ':MyDemo:libs:sliding-menu-lib'

There is a trick here, which allows you avoid errors while building project with gradle using Android Studio, as according to Android Tools Manual you should use ':libs:sliding-menu-lib' but that does not work due to issue with relative projectDir paths.

Your MyDemo/build.gradle should contain dependencies like:

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    ...
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':MyDemo:libs:sliding-menu-lib')

}

And your sliding-menu-lib/build.gradle should be like:

buildscript {
    repositories {
        mavenCentral()
    }

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

apply plugin: 'android-library'

android {
    compileSdkVersion 14
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 14
    }

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

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
}

Most important part deals with sourceSets section as you may not want change sliding-menu-lib file structure (non-default for current gradle)

Sergii N.
  • 315
  • 3
  • 10
0

I added all of my previous libraries using the default import from source tool. For SlidingMenu I used the import with Maven then deleted all of the Maven dependancies from the Project Settings for SlidingMenu and reimported the Support libraries. This seemed to clear most issues up for me.

Greg
  • 514
  • 5
  • 11
0

If the module is just a library and not a stand-alone app, it's gradle should contain

apply plugin: 'android-library'

instead of

apply plugin: 'android'
riwnodennyk
  • 8,140
  • 4
  • 35
  • 37
0

You can Sync Project with Gradle Files:

Tools -> Android -> Sync Project with Gradle Files

Hamid Behnam
  • 961
  • 1
  • 11
  • 16
0

Recently found better solution for SlidingMenu separately:
You can add SlidingMenu as generated @aar file if you do not need to make any changes to it. Just use https://github.com/jzaccone/SlidingMenu-aar and make changes as in Readme file there.
Be careful with order of repos. This one should be above mavenCentral()

Sergii N.
  • 315
  • 3
  • 10