Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't compile Android project with gradle after adding a library

Before adding a library to my Android project the layout was like this:

  • MyappProject
    • Myapp
      • build.gradle
  • settings.gradle
  • build.gradle

The top level build.gradle has always been empty, with the settings.gradle file containing only:

include ':Myapp'

I obtained a library project which I was able to import successfully into Android Studio, so I presume that the gradle files within it were fine. I now have the following structure:

  • MyappProject
    • Myapp
      • build.gradle
    • libraries
      • Library
        • LibrarySubProject1
          • build.gradle
        • ....
        • build.gradle
  • settings.gradle
  • build.gradle

...and the top level settings gradle now looks like:

include ':Myapp'
include ':libraries:Library'

I've also updated Myapp's build.gradle so it includes the extra last line in dependencies here:

dependencies {
    compile 'com.android.support:support-v4:+'
    compile files('libs/commons-lang3-3.1.jar')
    compile files('libs/jsoup-1.7.3.jar')
    compile project(':libraries:Library')
}

Unfortunately, any attempt to do anything with gradle (sync files, build etc.) now gets me the following:

Gradle 'MyappProject' project refresh failed:
     Configuration with name 'default' not found.
     Gradle settings

Any changes to the settings appear to have no effect, and Android Studio keeps the settings set to "use default gradle wrapper". From what I understand, that means that there is a problem with the top level build.gradle along the lines of this file not containing sufficient information to build the sub projects. But, perhaps I have misunderstood, as Myapp used to build and Library also seems fine. Removing the compile project(':libraries:Library') allows gradle file syncing again, but I would like to use that library...

Any suggestions as to how to fix this would be welcome.

Edited to add build.gradle from Library. Top level:

buildscript {
    repositories {
        mavenCentral()
    }

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

allprojects {
    version = VERSION_NAME
    group = GROUP

    repositories {
        mavenCentral()
    }
}

From the next level:

apply plugin: 'android-library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

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

        instrumentTest.setRoot('tests')

        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
like image 601
knirirr Avatar asked Dec 26 '22 12:12

knirirr


1 Answers

You cannot have project inside a project in Android Studio. You can only have modules so change your project structure as shown below so that it will be compliant for the allowed project - module level structure.

MyappProject
   Myapp
    build.gradle
   libraries
    LibrarySubProject1
     build.gradle
     ....
settings.gradle
build.gradle(Root only one)

Now do the following with your build.gradle files

1. Root level build.gradle file

buildscript {
    repositories {
        mavenCentral()
    }

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

repositories {
    mavenCentral()
}

If you have something in your library project's root level build.gradle file include that also in this root level build.gradle file because only root is allowed to have which will automatically be included in each of the sub level's build.gradle files while compilation .

2. LibraryProject's build.gradle file

apply plugin: 'android-library'

    android {
        compileSdkVersion 19
        buildToolsVersion "19.0.3"

        defaultConfig {
            minSdkVersion 14
            targetSdkVersion 19
        }

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

            instrumentTest.setRoot('tests')

            debug.setRoot('build-types/debug')
            release.setRoot('build-types/release')
        }
    }

    apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'

3. Your main module's build.gradle file should look like

apply plugin: 'android'

        android {
            compileSdkVersion 19
            buildToolsVersion "19.0.3"

            defaultConfig {
                minSdkVersion 14
                targetSdkVersion 19
            }
       }

       dependencies {
           //YOUR MODULE DEPENDENCIES
           compile project(':libraries:LibrarySubProject1')
       }

your settings.gradle file will be like

  include ':Myapp'
  include ':libraries:LibrarySubProject1'
like image 51
Piyush Agarwal Avatar answered Dec 30 '22 18:12

Piyush Agarwal