Android Studio打包apk自动获取SVN号、时间、版本号、版本名称

本文介绍如何在Android项目的Gradle构建脚本中集成SVN,以自动获取并打包最新的SVN版本号。通过在build.gradle文件中添加特定的依赖和自定义任务,可以实现在构建过程中动态获取SVN的修订版本号,并将其嵌入到APK的文件名中,确保每次打包的版本信息都是最新的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

备注:打包之前一定要记得首先从svn update一下代码 这样才能保证打包的svn号是当前最新的SVN号

一、根目录下build.gradle文件配置

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        mavenCentral()
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.3'

        classpath "org.tmatesoft.svnkit:svnkit:1.8.11"//1.添加此处

    }
}

allprojects {
    repositories {
        mavenCentral()
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
 


二、app中的build.gradle文件配置

apply plugin: 'com.android.application'

import org.tmatesoft.svn.core.wc.*//2.添加此处

//3.添加时间
def releaseTime() {

    return new Date().format("yyMMddHHmm", TimeZone.getTimeZone("GMT+08:00"))

}
//4.添加SVN号
def getSvnRevision() {
    ISVNOptions options = SVNWCUtil.createDefaultOptions(true)
    SVNClientManager clientManager = SVNClientManager.newInstance(options)
    SVNStatusClient statusClient = clientManager.getStatusClient()
    SVNStatus status = statusClient.doStatus(projectDir, false)
    status.getCommittedRevision().getNumber()
    SVNRevision revision = status.getCommittedRevision()
    return revision.getNumber()
}

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.a.b"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 2
        versionName "2.2"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
 
    }

    buildTypes {
        debug {
            minifyEnabled false
            //app打包名称
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "app_v${variant.versionName}_debug_${releaseTime()}_svn${getSvnRevision()}_V${variant.versionCode}.apk"
                }
            }
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
            debuggable true
            jniDebuggable true
        }
        
        release {
            minifyEnabled false
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "app_v${variant.versionName}_release_${releaseTime()}_svn${getSvnRevision()}_V${variant.versionCode}.apk"

                }
            }
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            debuggable false
            jniDebuggable false
        }


    }
 

    buildToolsVersion '28.0.3'


}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation fileTree(include: ['*.aar'], dir: 'libs')
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  

}

 

三、最终打包apk名称

app_v1.1_release_2005181652_svn999_V1.apk

 

备注:打包之前一定要记得首先从svn update一下代码 这样才能保证打包的svn号是当前最新的SVN号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值