4

I am trying to import my submodule to my project but it always gives that error.

Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve project :sanservicelibrary.
Open File
Show Details


Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve project :sanservicelibrary.
Open File
Show Details


Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve project :sanservicelibrary.
Open File
Show Details


Unable to resolve dependency for ':app@release/compileClasspath': Could not resolve project :sanservicelibrary.
Open File
Show Details


Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve project :sanservicelibrary.
Open File
Show Details

Those are didn't help

Cant compile project with modules

Unable to resolve dependency for ':app@debugUnitTest/compileClasspath', :app@debugAndroidTest/compileClasspath

App gradle.

apply plugin: 'com.android.application'

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    compileSdkVersion 27
    defaultConfig {
        applicationId ".."
        minSdkVersion 18
        targetSdkVersion 27
        versionCode 13
        versionName "Beta 1.11"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:design:$supportLibVer"
    implementation "com.android.support:appcompat-v7:$supportLibVer"
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

...

    implementation project(path:':libname', configuration: 'default')

}


}

library gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "..."
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

and I have

include ':app', ':libname'

in settings gradle

Emre Akcan
  • 982
  • 10
  • 27

3 Answers3

4

You have a few of options.

OPTION 1)

Import the module through the file menu and let Android make a copy of it into your root and updates the Gradle and IDEA files etc..

OPTION 2)

You can import it via relative path. Not always recommended, but does the trick. Of course you have to be cognitive that your relative path may not match your fellow developers, so folder structure matching of repos is important if you decide to go this route.

settings.gradle should include

include 'app', ':Module 2'
project(':Module 2').projectDir = new File('../../YourPath/Project B/Module 2')

Then in your build.gradle for the app you should include

implementation project(':Module 2')

Fair Warning, some developers hate when you do it this way, but I for one see the value during the development cycle of staying with latest of a repo that may still be changing as you find issues. So there is a case for it on young modules.

OPTION 3)

Use Gradle to import compiled binaries from a Maven, jitpack, or Ivy repo. This assumes your modules are mature and ready to be packaged and used.

Sam
  • 5,342
  • 1
  • 23
  • 39
  • If I import the module through the file menu and let Android make a copy of it into my root, is sub module changes when some one commits new feature? – Emre Akcan Oct 31 '18 at 13:34
  • Unfortunately it is a copy. So if that library is still being worked on, there changes will not show up in you copy of it. – Sam Oct 31 '18 at 13:39
  • I used option 2, i believe it is synronized with git. Thank you ! – Emre Akcan Oct 31 '18 at 14:04
  • 1
    Hi Emre, I didn't know you were working with Git, that presents a 4th option. You can definitely utilize subModules in Git to ensure the paths line up. But sounds like that is essentially what you are doing. Glad you got a path that works for you. – Sam Oct 31 '18 at 14:19
  • Option 2 will create problems on CI pipelines, because your CI image has to have the exact same folder structure. Using submodules is usually the better way to go. – muetzenflo Oct 12 '21 at 20:33
0

Use api instead of implementation in library build.gradle

dependencies {
    api fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

If you add dependency by implementation in sub module then you can't use it in your main module,so you should change it to api.

aolphn
  • 2,950
  • 2
  • 21
  • 30
0

I had same problem when I created new module as "Phone & Tablet Module" type, instead when I tried "Android Library" type this issue was resolved.

So when creating a new submodule which is being used as library then make sure to Android Studio -> File -> New ->New Module -> "Android Library"

Shyam Sunder
  • 523
  • 4
  • 13