android - 是否可以设置 Android 应用的 versionName 以匹配依赖项的已解析版本?

标签 android gradle groovy

我正在开发一个 Android 应用程序,其中 95% 的功能都包含在我编写的库中。同一个库将用于 30 多个应用程序,我希望不必手动设置每个应用程序的版本。相反,我想设置应用程序的 versionName基于库的解析版本(因为它没有在 build.grade 文件中明确指定)。

这是我目前的build.gradle应用程序文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.commentsold.demo"
        minSdkVersion 18
        targetSdkVersion 27
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

apply plugin: 'io.fabric'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:multidex:1.0.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.github.CommentSold:Android-CommentSoldKit:1+'
}

android {
    defaultConfig {
        versionCode 1
        versionName ***SET_THIS_NAME_BASED_ON_Android-CommentSoldKit***
    }
}

apply plugin: 'com.google.gms.google-services'

请注意,占位符 **SET_THIS_NAME_BASED_ON_Android-CommentSoldKit**。这就是我想用“1.0.0”之类的东西代替的地方。

最佳答案

您可以在项目的根目录中定义共享变量 build.gradle通过这样的事情:

buildscript {
    ext {
        my_lib = "com.github.CommentSold:Android-CommentSoldKit:1.0.0"
    }
}

然后在你的每个应用程序的 build.gradle 中,你可以做
def common = rootProject.ext
dependencies {
    implementation common.my_lib
}

这是你想要的?

关于android - 是否可以设置 Android 应用的 versionName 以匹配依赖项的已解析版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50708859/

相关文章:

Android - 滚动到 ChipGroup 中的选定子位置

java - 使用VS代码的Gradle错误: “Could not run build action using Gradle distribution”

macos - 在 Mac 中从 gradle build 找到 jar

Groovy 标记生成器标签

java - 无法在 Groovy/Java 中编写 JSON 字符串

android - 简单的 HTML anchor 链接不会在 Gmail 中收到的电子邮件中显示为链接

android - 如何更改 Material 抽屉中所选项目的图标颜色?

android - 在 Android 上拦截 Activity 和按钮上的触摸事件

gradle - Intellij IDEA在项目向导中没有Kotlin Native

java - 重复事件逻辑