Gradle error with Android project added as a library (SlidingMenu) [package does not exist]
Asked Answered
O

6

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

Oospore answered 20/5, 2013 at 13:45 Comment(3)
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...Nepheline
@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.Oospore
@Oospore Did you look through the forks? There are a couple of them set up for Gradle build.Erdda
S
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.

Scrawl answered 21/5, 2013 at 12:13 Comment(6)
Nope, I don't have it as a library but as another project. Then the main project has a dependency on the SlidingMenu project.Oospore
Don't use IntelliJ's (Studio's) UI to add dependency, only edit build.gradle. I'd recommend you read on multi-project setup.Hereupon
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.Oospore
@Oospore 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 describing that as well.Firstborn
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.Jellyfish
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!Greengrocery
H
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)

Helmand answered 5/9, 2013 at 12:23 Comment(0)
S
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.

Scrubland answered 20/6, 2013 at 14:10 Comment(0)
B
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'
Backboard answered 30/10, 2013 at 16:41 Comment(0)
H
0

You can Sync Project with Gradle Files:

Tools -> Android -> Sync Project with Gradle Files

Haldes answered 18/11, 2013 at 20:25 Comment(0)
H
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()

Helmand answered 4/5, 2014 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.