【问题标题】:Can not add submodule to android project无法将子模块添加到android项目
【发布时间】:2018-10-31 12:45:14
【问题描述】:

我正在尝试将我的子模块导入我的项目,但它总是给出该错误。

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

应用分级。

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

}


}

图书馆毕业

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'

在设置分级中

【问题讨论】:

    标签: android


    【解决方案1】:

    你有几个选择。

    选项 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')
    

    公平警告,一些开发人员讨厌你这样做,但我看到了在开发周期中保持最新的 repo 的价值,当你发现问题时,它可能仍然会发生变化。所以在年轻的模块上有一个案例。

    选项 3)

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

    【讨论】:

    • 如果我通过文件菜单导入模块并让Android将其复制到我的根目录中,当有人提交新功能时子模块是否会更改?
    • 不幸的是它是一个副本。因此,如果该库仍在开发中,则不会在您的副本中显示更改。
    • 我使用了选项 2,我相信它与 git 同步。谢谢!
    • 您好 Emre,我不知道您使用的是 Git,这是第四个选项。您绝对可以在 Git 中使用 subModules 来确保路径对齐。但听起来这基本上就是你正在做的事情。很高兴你有一条适合你的道路。
    • 选项 2 会在 CI 管道上产生问题,因为您的 CI 映像必须具有完全相同的文件夹结构。使用子模块通常是更好的方法。
    【解决方案2】:

    在库 build.gradle 中使用 api 而不是实现

    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。

    【讨论】:

    • 您是否启用了 VPN 或代理?
    • 不,我没有启用它们
    【解决方案3】:

    当我将新模块创建为“手机和平板电脑模块”类型时,我遇到了同样的问题,而当我尝试使用“Android 库”类型时,这个问题得到了解决。

    因此,当创建一个用作库的新子模块时,请确保 Android Studio -> File -> New ->New Module -> “Android Library”

    【讨论】:

      猜你喜欢
      • 2016-10-19
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多