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

时间:2018-03-14 20:02:45

标签: android gradle

就像示例here我正在扩展我的构建类型以添加​​分段:

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

但我也有依赖:

implementation project(':mylibrary')

编辑失败,因为它不知道将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'.

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

我试过了:

debugImplementation project(path: ':mylibrary', configuration: 'debug')
releaseImplementation project(path: ':mylibrary', configuration: 'release')
stagingImplementation project(path: ':mylibrary', configuration: 'release')

但它失败了:

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

这有效,但每个构建都有库的发布版本。我不想要那个,我需要调试以获得库的调试版本。

我已经看过this q了,但它已经预先发现了implementation"并且publishNonDefault true没有效果,与上述相同。

  

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

Gradlew版本4.6

1 个答案:

答案 0 :(得分:16)

来过这里,做到了:P

您需要使用Android Gradle Plugin 3.0.0为插件指定matchingFallback,以便知道在使用应用代码编译时要使用哪种回退构建类型的库,以防您在其中定义某种构建类型在图书馆中找不到应用程序。

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

此处有更多信息:Migrate to Android Plugin for Gradle 3.0.0