uniapp动态读取版本号

app下build.gradle配置

js 复制代码
apply plugin: 'com.android.application'


// 从 manifest.json 读取版本号
def getVersionInfo() {
    // 根据实际路径调整,常见路径:
    // 'src/main/assets/apps/__UNI__0BD8113/www/manifest.json'
    def manifestFile = file('src/main/assets/apps/__UNI__0BD8113/www/manifest.json')

    // 调试信息
    println "======== 版本号读取调试 ========"
    println "当前目录: ${projectDir.absolutePath}"
    println "manifest.json 路径: ${manifestFile.absolutePath}"
    println "文件是否存在: ${manifestFile.exists()}"

    if (!manifestFile.exists()) {
        println "❌ manifest.json 文件不存在,使用默认版本号"
        println "================================"
        return [versionCode: 1, versionName: "1.0.0"]
    }

    try {
        def jsonSlurper = new groovy.json.JsonSlurper()
        def manifest = jsonSlurper.parse(manifestFile)

        // 从 version 对象中读取
        def versionCode = manifest.version?.code?.toInteger() ?: 1
        def versionName = manifest.version?.name ?: "1.0.0"

        println "✅ 读取到版本信息:"
        println "   versionCode = ${versionCode}"
        println "   versionName = ${versionName}"
        println "================================"

        return [versionCode: versionCode, versionName: versionName]
    } catch (Exception e) {
        println "❌ 解析 manifest.json 失败: ${e.message}"
        println "================================"
        return [versionCode: 1, versionName: "1.0.0"]
    }
}

// 获取版本信息
def versionInfo = getVersionInfo()

android {
    compileSdkVersion 36
    buildToolsVersion '36.0.0'

    // 应和离线打包时申请离线打包key的值一致
    namespace 'com.android.chuanwu'
    defaultConfig {
        // 应和离线打包时申请离线打包key的值一致
        applicationId "com.android.chuanwu"
        // 插件aar最低支持26
        minSdkVersion 26
        targetSdkVersion 36 //建议此属性值设为21 io.dcloud.PandoraEntry 作为apk入口时   必须设置 targetSDKVersion>=21 沉浸式才生效

        // 动态设置版本号
        versionCode versionInfo.versionCode
        versionName versionInfo.versionName

        multiDexEnabled true
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }

    signingConfigs {
        config {
            // 证书别名
            keyAlias ''
            // 证书密码
            keyPassword ''
            // 证书文件路径
            storeFile file('xxx.keystore')
            // 证书密码
            storePassword ''
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.config
            zipAlignEnabled false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            signingConfig signingConfigs.config
            zipAlignEnabled false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            // 自定义 APK 名称
            outputFileName = "app-${versionInfo.versionName}.apk"
        }
    }


    //使用uniapp时,需复制下面代码
    /*代码开始*/
    aaptOptions {
        additionalParameters '--auto-add-overlay'
        //noCompress 'foo', 'bar'
        ignoreAssetsPattern "!.svn:!.git:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    /*代码结束*/
}
repositories {
    flatDir {
        dirs 'libs'
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation fileTree(dir: 'libs', include: ['*.aar'])

    /*uniapp所需库-----------------------开始*/
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.facebook.fresco:fresco:1.13.0'
    implementation "com.facebook.fresco:animated-gif:1.13.0"
    /*uniapp所需库-----------------------结束*/
    // 基座需要,必须添加
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'com.alibaba:fastjson:1.2.83'
    implementation 'androidx.webkit:webkit:1.5.0'
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
    implementation 'androidx.core:core:1.1.0'
    implementation "androidx.fragment:fragment:1.1.0"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    // 添加uni-app插件
    implementation project(':uniplugin_scancode')
}
相关推荐
千里马学框架2 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台2 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone2 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc2 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo2 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077002 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼2 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone2 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen2 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone2 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui