Is there an inherent problem with using if statements to define the minSdkVersion in the defaultConfig block?

痴心易碎 提交于 2021-02-11 14:50:14

问题


Background

Working on a project that has multiple sub projects (modules) that are dependent on some libraries within the same root project. For the purpose of some testing functionality, we need a higher minSdkVersion (21), but this minSdkVersion is not necessary for the normal use of the application as we can get all we need from version 14. Therefore we've tried to include some if logic to separate the build for testing vs production purposes. The snippet in question is here:

android {
    compileSdkVersion 30
    defaultConfig {

        // Default version necessary for some reason.
        // minSdkVersion 14

        gradle.startParameter.taskNames.each {
            if (it.contains("AndroidTest") || it.contains("cAT")) {
                minSdkVersion 21
            } else {
                minSdkVersion 14
            }
        }
        targetSdkVersion 30

        versionCode getGitCommitCount()
        versionName getTag()

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

Summary question:

Is there an inherent problem with using if statements to define the minSdkVersion in the defaultConfig block?

Further Details

I can build with the gradle wrapper via the terminal, i.e. .gradlew build without issue, but there is a red X on the configuration of the modules and when I try to run them I get:

Error running Default Activity not found.

The default activity is most definitely in the manifest, and runs just fine after clearing the gradle cache and restarting Android Studio. The red X pops up again after I do anything related to gradle (e.g. syncing with gradle files, building, etc.)

I did find a workaround (Note the commented out minSdkVersion 14 definition in the above snippet) as this resolves the run error, but I'm still not clear as to why this is necessary.

Upon a deeper look, I'm getting the following error in my Merged Manifest tab of the module's manifest:

Merging Errors: Error: uses-sdk:minSdkVersion 1 cannot be smaller than version 14 declared in library <path_to_my_library>/src/main/AndroidManifest.xml as the library might be using APIs not available in 1 Suggestion: use a compatible library with a minSdk of at most 1, or increase this project's minSdk version to at least 14, or use tools:overrideLibrary="" to force usage (may lead to runtime failures) debug manifest

You can see a lot more detail with screenshots, source code, and my theories up to this point in this PR where this is being discussed in greater detail, but without a solid explanation yet. I'll add these details here if the discussion on github results in a solid conclusion so as not to lose anything to a broken link later on.

来源:https://stackoverflow.com/questions/63936013/is-there-an-inherent-problem-with-using-if-statements-to-define-the-minsdkversio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!