如何在应用程序中添加比库更多的构建类型

wes*_*ton 7 android gradle

就像这里的例子一样,我正在扩展我的构建类型来添加staging:

android {
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix '.debug'
        }
        staging {
            initWith release
            applicationIdSuffix '.staging'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我也有一个依赖:

implementation project(':mylibrary')
Run Code Online (Sandbox Code Playgroud)

编译失败,因为它不知道要配对staging的内容:mylibrary:

* What went wrong:
Could not determine the dependencies of task ':app:compileStagingJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:stagingCompileClasspath'.
   > Could not resolve project :mylibrary.
     Required by:
         project :app
      > Unable to find a matching configuration of project :mylibrary:
          - Configuration 'debugApiElements':
              - Required com.android.build.api.attributes.BuildTypeAttr 'staging' and found incompatible value 'debug'.
              - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
              - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
              - Required org.gradle.api.attributes.Usage 'java-api' and found compatible value 'java-api'.
          - Configuration 'debugRuntimeElements':
              - Required com.android.build.api.attributes.BuildTypeAttr 'staging' and found incompatible value 'debug'.
              - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
              - Found com.android.build.gradle.internal.dependency.VariantAttr 'debug' but wasn't required.
              - Required org.gradle.api.attributes.Usage 'java-api' and found incompatible value 'java-runtime'.
          - Configuration 'releaseApiElements':
              - Required com.android.build.api.attributes.BuildTypeAttr 'staging' and found incompatible value 'release'.
              - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
              - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
              - Required org.gradle.api.attributes.Usage 'java-api' and found compatible value 'java-api'.
          - Configuration 'releaseRuntimeElements':
              - Required com.android.build.api.attributes.BuildTypeAttr 'staging' and found incompatible value 'release'.
              - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
              - Found com.android.build.gradle.internal.dependency.VariantAttr 'release' but wasn't required.
              - Required org.gradle.api.attributes.Usage 'java-api' and found incompatible value 'java-runtime'.
Run Code Online (Sandbox Code Playgroud)

这很公平,但我不能通过添加所有库staging来获得不同的应用程序后缀.

我试过了:

debugImplementation project(path: ':mylibrary', configuration: 'debug')
releaseImplementation project(path: ':mylibrary', configuration: 'release')
stagingImplementation project(path: ':mylibrary', configuration: 'release')
Run Code Online (Sandbox Code Playgroud)

但它失败了:

* What went wrong:
Could not determine the dependencies of task ':app:compileReleaseJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:releaseCompileClasspath'.
   > Could not resolve project :mylibrary.
     Required by:
         project :app
      > Project :app declares a dependency from configuration 'releaseImplementation' to configuration 'release' which is not declared in the descriptor for project :mylibrary.

debugImplementation project(path: ':mylibrary', configuration: 'default')
releaseImplementation project(path: ':mylibrary', configuration: 'default')
stagingImplementation project(path: ':mylibrary', configuration: 'default')
Run Code Online (Sandbox Code Playgroud)

这有效但每个构建都有库的发布版本.我不希望这样,我需要调试以获得库的调试版本.

我已经看过这个q,但它是预先" implementation"并publishNonDefault true没有效果,与上面相同的错误.

publishNonDefault已弃用,不再有效.所有变体现已发布.

Gradlew 4.6版

aha*_*ini 26

来过这里,做到了:P

您需要matchingFallback使用Android Gradle Plugin 3.0.0 指定插件,以便知道在使用应用程序代码编译时要使用的库的后备构建类型,以防在应用程序中定义的某个构建类型未在库中找到.

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        applicationIdSuffix '.debug'
    }
    staging {
        initWith release
        applicationIdSuffix '.staging'
        matchingFallbacks = ['release']
    }
}
Run Code Online (Sandbox Code Playgroud)

更多信息:迁移到Gradle 3.0.0的Android插件.

  • +50,因为您节省了我很多时间和头发! (3认同)