简体   繁体   中英

How to add more build types in app than library

Just like the example here I am extending my build types to add staging:

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

But I also have a dependency:

implementation project(':mylibrary')

Compilation fails because it doesn't know what to pair staging to in :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'.

That's fair enough, but I cannot go through all of my libraries adding staging just to get a different application suffix.

I tried:

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

But it fails:

* 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 works but every build has release build of library. I don't want that, I need debug to have debug version of library.

I've seen this q , but it's pre " implementation " and publishNonDefault true had no effect, same error as above.

publishNonDefault is deprecated and has no effect anymore. All variants are now published.

Gradlew version 4.6

Been here, done that :P

You'll need to specify matchingFallback with the Android Gradle Plugin 3.0.0 for the plugin to know which fallback build type of library to use when being compiled with app code in case a certain build type defined in your app is not found in library.

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

More info here: Migrate to Android Plugin for Gradle 3.0.0 .

I was creating a staging buildType in my react native project too, the build was fine, but for some reason it was loading an old version of the app .

If your react native is loading a old version:

Try the following changes at /android/app/build.gradle file:

1. Change the buildType name from staging to releaseStaging

I don't know the reason yet. But this is the main change that made my react native app open the correct version of the app.

buildTypes {
    ...
    releaseStaging {
        initWith release
        ...
    }
}

2. Include the matchingFallbacks = ['release'] , just like @ahasbini mentioned.

This fixed some build errors.

buildTypes {
    ...
    releaseStaging {
        initWith release
        matchingFallbacks = ['release']
        ...
    }
}

3. If you have hermes enabled, you should include a releaseStagingImplementation , just like the code below.

After including this, my app stopped to crash while starting.

if (enableHermes) {
    def hermesPath = "../../node_modules/hermes-engine/android/";
    debugImplementation files(hermesPath + "hermes-debug.aar")
    releaseImplementation files(hermesPath + "hermes-release.aar")
    releaseStagingImplementation files(hermesPath + "hermes-release.aar")
} else {
    implementation jscFlavor
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM