简体   繁体   中英

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

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

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.

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)

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.

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'

You can Sync Project with Gradle Files :

Tools -> Android -> Sync Project with Gradle Files

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM