zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【Android Gradle 插件】ProductFlavor 配置 ( 测试相关配置 | versionNameSuffix 配置 | applicationIdSuffix 配置 )

Android配置gradle测试插件 相关 ProductFlavor
2023-06-13 09:18:01 时间

文章目录

Android Plugin DSL Reference 参考文档 :

一、测试相关配置


ProductFlavor ( build.gradle#android#defaultConfig 配置 ) 文档 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html

ProductFlavor#testApplicationId ,

ProductFlavor#testFunctionalTest ,

ProductFlavor#testHandleProfiling ,

ProductFlavor#testInstrumentationRunner ,

ProductFlavor#testInstrumentationRunnerArguments ,

都是用于配置测试相关的配置 ;

详细的文档说明 :

参考地址 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html#com.android.build.gradle.internal.dsl.ProductFlavor:testApplicationId

二、ProductFlavor#useJack 配置


ProductFlavor#useJack 配置已经弃用 , 使用 ProductFlavor#jackOptions 配置替代该配置 ;

三、ProductFlavor#versionNameSuffix 配置


ProductFlavor#versionNameSuffix 配置用于配置版本名称后缀 ;

String versionNameSuffix
版本名称后缀。
当计算变量的最终版本名时,这将应用于“base”版本名。

如 :

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "kim.hsl.websocketdemo"
        minSdkVersion 18
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        applicationIdSuffix "tom"
        versionNameSuffix "9527"
    }
}

最终的 版本号 是 " 1.09527 " ;

四、ProductFlavor#applicationIdSuffix 配置


ProductFlavor#applicationIdSuffix 配置 , 用于配置应用 ID 后缀 ;

String applicationIdSuffix
应用程序id后缀。
在计算变量的最终应用程序id时,这会附加到“base”应用程序id。

如 :

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "kim.hsl.websocketdemo"
        minSdkVersion 18
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        applicationIdSuffix "tom"
        versionNameSuffix "9527"
    }
}

最终的 applicationId 是 " kim.hsl.websocketdemo.tom " ;

五、应用id后缀、版本号后缀 配置示例


完整的 build.gradle 配置文件如下 :

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "kim.hsl.websocketdemo"
        minSdkVersion 18
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        applicationIdSuffix "tom"
        versionNameSuffix "9527"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    //implementation 'org.java-websocket:Java-WebSocket:1.5.2'
    implementation project(path: ':Java-WebSocket-1.5.2')
}

选择 " 菜单栏 / Build / Make Project " 选项 , 或者使用 Ctrl + F9 编译程序 ,

之后查看 AndroidManifest.xml 清单文件的 Merge Manifest 视图 , 即可看到最终生成的 applicationId 是 “kim.hsl.websocketdemo.tom” , 最终生成的版本号是 “1.09527” ;