gradle – 如何为所有的flavor定义不同的库版本?

huangapple go评论58阅读模式
英文:

gradle - How to define different library versions for all flavors?

问题

在我们的项目中,我们在一个库中进行了重要的更改。但是我们的项目经理希望保留原始库版本和更改后的库版本。因此,他们希望为所有客户分发这两个版本(可能听起来有点奇怪)。

我的任务是在gradle中指定这些库的版本,以便我们能够为所有不同类型的客户生成构建,而不需要更改代码或build.gradle。

假设我的产品变种看起来像这样:

productFlavors {
    firstClient {
     ...... 
    }
    secondClient {
     ...... 
    }
    thirdClient {
     ......
    }
  }

以及构建类型:

buildTypes: {
    debug {
     ...... 
    }
    staging {
     ...... 
    }
    release {
     ......
    }
  }

问题 >> 是否可以定义/指定除了变种和构建类型之外的另一个“维度”/“参数”?

在我的情况下,它是库版本。我希望有一种方法可以实现这一点(我不希望通过复制变种来定义这个)。

英文:

In our project we made important changes in one of the libraries. But our PM wants to keep original lib version AND changed lib. So they want to distribute both of these versions for all our clients. (may sound odd, probably)

My task is to specify these lib versions in gradle. So that we would be able to make builds for all "kinds" of our clients. Without changes in the code or build.gradle.

Suppose, my flavors look like that:

productFlavors {
    firstClient {
     ...... 
    }
    secondClient {
     ...... 
    }
    thirdClient {
     ......
    }
  }

and buildTypes:

buildTypes: {
    debug {
     ...... 
    }
    staging {
     ...... 
    }
    release {
     ......
    }
  }

Question >> Is it possible to define/specify another "dimension"/"parameter" along with flavors and buildTypes?

In my case it's lib versions. I hope there's some way of doing that (I don't really want to define this by duplicating flavors).

答案1

得分: 0

(1)

首先,我们需要定义口味维度 - 在我的情况下,这些是口味和库版本:

flavorDimensions "flavor", "lib_version"

其次,为现有的口味定义“flavor”维度:

productFlavors {
    firstClient {
        dimension "flavor"
        ...... 
    }
    
    // ... 其他口味
    // ...
}

第三,为不同的库版本定义不同的维度:

productFlavors {
    // ...
    // ... 其他口味

    oldLib {
        dimension "lib_version"
    }
    
    newLib {
        dimension "lib_version"
    }
}

因此,对于我的情况,结果将如下所示:

flavorDimensions "flavor", "lib_version"

productFlavors {
    firstClient {
        dimension "flavor"
        ...... 
    }
    
    secondClient {
        dimension "flavor"
        ...... 
    }

    thirdClient {
        dimension "flavor"
        ...... 
    }

    oldLib {
        dimension "lib_version"
    }
    
    newLib {
        dimension "lib_version"
    }
}

现在,我们可以根据需要选择构建变体。

(2)

不要忘记为不同的维度定义不同的库版本!在依赖关系部分定义这些库版本:

dependencies {
    // ...
    // ... 其他依赖项

    oldLibImplementation ('org.example:libexample:1.0_old')
    newLibImplementation ('org.example:libexample:1.0_new')
}

注意

在我的情况下,有一个巨大的困难。对于其中一个库,我应该为其中一个库设置 transitive = false

    implementation(project(':lib-example')) {
        transitive = false
    }

但是哪一个呢?你必须在你的项目中自己找到答案。

英文:

For those who will come across this question.

(1)

First, we need to define flavor dimensions - in my case that are flavors and lib versions:

flavorDimensions "flavor", "lib_version"

Second, for existing flavors define dimension "flavor"

productFlavors {
    firstClient {
        dimension "flavor"
        ...... 
    }
    
    // ... other flavors
    // ...
}

Third, for different lib versions define different dimensions for libs.

productFlavors {
    // ...
    // ... other flavors

    oldLib {
        dimension "lib_version"
    }
    
    newLib {
        dimension "lib_version"
    }
}

So, for my case result would look like this:

flavorDimensions "flavor", "lib_version"

productFlavors {
    firstClient {
        dimension "flavor"
        ...... 
    }
    
    secondClient {
        dimension "flavor"
        ...... 
    }

    thirdClient {
        dimension "flavor"
        ...... 
    }

    oldLib {
        dimension "lib_version"
    }
    
    newLib {
        dimension "lib_version"
    }
}

Now we can choose Build Variant according to our needs.

(2)
Don't forget about different lib versions for different dimensions! In dependencies section define these lib versions:

dependencies {
    // ...
    // ... other dependencies

    oldLibImplementation ('org.example:libexample:1.0_old')
    newLibImplementation ('org.example:libexample:1.0_new')
}

NOTE

In my case though there was a huge difficulty. For one of the libs I should've set transitive = false for one of the libs:

    implementation(project(':lib-example')) {
        transitive = false
    }

But which one? You have to find in yourself in your project.

huangapple
  • 本文由 发表于 2023年7月7日 01:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定