无法将子模块添加到android项目

时间:2018-10-31 12:45:14

标签: android

我正在尝试将子模块导入到我的项目中,但始终会出现该错误。

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

那些没有帮助

Cant compile project with modules

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

应用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')

}


}

库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'
}

我有

include ':app', ':libname'

在设置gradle中

3 个答案:

答案 0 :(得分:2)

您有一些选择。

选项1)

通过文件菜单导入模块,然后让Android将其复制到您的根目录中,并更新Gradle和IDEA文件等。

选项2)

您可以通过相对路径导入它。并不总是推荐,但是可以解决问题。当然,您必须意识到您的相对路径可能与您的其他开发人员不匹配,因此,如果您决定采用这种方式,那么与repos进行文件夹结构匹配非常重要。

settings.gradle应该包含

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

然后在build.gradle中为您应包括的应用程序

implementation project(':Module 2')

公平的警告,有些开发人员讨厌用这种方式这样做,但是我第一次看到在开发周期中保持最新的回购可能带来的价值,当您发现问题时,最新的回购可能仍在变化。因此在年轻的模块上就有这种情况。

选项3)

使用Gradle从Maven,jitpack或Ivy仓库导入已编译的二进制文件。 假设您的模块已经成熟,可以打包和使用了。

答案 1 :(得分:0)

使用api代替库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'
}

如果在子模块中通过实现添加依赖项,则无法在主模块中使用它,因此应将其更改为api。

答案 2 :(得分:0)

当我创建新模块作为“电话和平板电脑模块”类型时,我遇到了同样的问题,而当我尝试“ Android库”类型时,此问题得以解决。

因此,在创建用作库的新子模块时,请确保进入Android Studio->文件->新建->新模块-> “ Android库”