Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Import Module Gradle Build Error

I am attempting to add a directory as a dependency in Android Studio(GameBaseUtils). I have seen other SO answers simply posting the correct gradle configuration for their particular issue, however I don't understand how I can adapt their answers to my situation.

Here is what I did:

Step one: File-> Import Module ->Navigate to directory and select it.

Step Two-: File-> Project Structure-> Modules-> Select my application->Dependencies->Add the module as a dependency to my project.

Now my code doesn't have any red lines indicating an error importing the module. However when I select build I get the following errors:

Gradle: package com.google.example.games.basegameutils does not exist
Gradle: cannot find symbol class BaseGameActivity
Gradle: cannot find symbol variable super
...

Here is the build.gradle file for my application

buildscript {
repositories {
    mavenCentral()
}
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
}
}

How can I correctly import this external library and can you please explain how and why your solution works?

like image 977
KDEx Avatar asked Jul 08 '13 19:07

KDEx


1 Answers

so here is how I solved my problem:

instead of adding

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

You have to write:

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

the 2 dots say that the Module (or directory) can be found in 1 directory above the actual one. so if you want to access a module which is 2 directories above you just have to write: '../../ModuleName'

You have to add the modules manually to the build.gradle because Android Studio is still in development and doesn't have finished the UI for editing the Project Structure.

If this does not solve your problem try to make it like this: (I would recommend this method. This is how I actually do it)

Examplestructure:

  • Project

    • libraries (normal folder)
      • Module2
    • Module1

settings.gradle

include ':Module1', ':libraries:Module2'

build.gradle (Module1)

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile project(':libraries:Module2')
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 11
    }
}

build.gradle (Module2)

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android-library'

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

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 11
    }

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

This should work well now. To make everything work 100% follow this steps:

  1. delete .idea folder
  2. delete all *.iml files
  3. Restart Android Studio and press Import Project
  4. Select the directory with your gradle project
  5. Import project from external model > Gradle > next > finish

With this steps everything should work well. If there are any problems just tell me :)

like image 199
maysi Avatar answered Nov 13 '22 03:11

maysi