V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
banxi1988
V2EX  ›  Android

Android Studio Gradle 生成 Build Number

  •  
  •   banxi1988 · 2015-10-17 13:26:43 +08:00 · 5523 次点击
    这是一个创建于 3107 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我昨天晚上有一个小需要就是:
    每次调试运行一个 Xposed 模块时 想在 log 中看到当前的构建版本编号。
    添加一个编译版本的时间 戳应该可以满足我的需求。
    虽然按 Xposed 的工作原理每次更新模块之后,都必须重启手机,模块才会被重新加载应用。
    但是我还是想看到一个编译的构建版本号。

    根据一般编译过程(见最下) 来看。
    每次编译generateDebugBuildConfig 任务都会执行。
    而且添加在 BuildConfig 类中作为静态常量,使用上也很方便。

    于是:
    1 ). 编写一个时间戳生成函数 :

    def generateBuildNumber(){
        def date = new Date()
        def formattedDate = date.format("yyyyMMdd")
        def formattedSeconds = date.format("HHmmss")
        def formatInt = formattedDate as int;
        def secondsInt = formattedSeconds as int;
        return (formatInt + secondsInt) as int
    }
    

    2 ). 在 defaultConfig DSL 容器中添加一个增加自定义 buildConfigField 的配置

    buildConfigField 'int','buildNumber', "${generateBuildNumber()}"
    

    然后 BuildConfig 类中就有了一个自定义的 buildNumber

    // Fields from default config.
      public static final int buildNumber = 20282037;
    
    Executing tasks: [:app:assembleDebug]
    
    Configuration on demand is an incubating feature.
    :app:preBuild UP-TO-DATE
    :app:preDebugBuild UP-TO-DATE
    :app:checkDebugManifest
    :app:preReleaseBuild UP-TO-DATE
    :app:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
    :app:prepareComAndroidSupportDesign2301Library UP-TO-DATE
    :app:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
    :app:prepareDebugDependencies
    :app:compileDebugAidl UP-TO-DATE
    :app:compileDebugRenderscript UP-TO-DATE
    :app:generateDebugBuildConfig
    :app:generateDebugAssets UP-TO-DATE
    :app:mergeDebugAssets UP-TO-DATE
    :app:generateDebugResValues UP-TO-DATE
    :app:generateDebugResources UP-TO-DATE
    :app:mergeDebugResources UP-TO-DATE
    :app:processDebugManifest UP-TO-DATE
    :app:processDebugResources UP-TO-DATE
    :app:generateDebugSources
    :app:processDebugJavaRes UP-TO-DATE
    :app:compileDebugJavaWithJavac
    :app:compileDebugNdk UP-TO-DATE
    :app:compileDebugSources
    Compile Debug Sources banxi
    :app:preDexDebug UP-TO-DATE
    :app:dexDebug
    :app:validateDebugSigning
    :app:packageDebug
    :app:zipalignDebug
    :app:assembleDebug
    
    BUILD SUCCESSFUL
    
    Total time: 1.752 secs
    
    2 条回复    2015-10-17 19:23:29 +08:00
    hantsy
        1
    hantsy  
       2015-10-17 15:57:39 +08:00
    return (formatInt + secondsInt) as int 这个重复的概率应该很大的吧。

    直接用 timestamp 就行了。也可以读取 Git 的版本号。
    banxi1988
        2
    banxi1988  
    OP
       2015-10-17 19:23:29 +08:00
    @hantsy 这个我只是为了让自己确认代码修改了。
    对于一天的开发过程来说是没有重复的。
    另外读取 Git 版本号不适用? 为什么呢? 我这是用于开发编译版本号。
    因为修改之后还没有提交。 Git 的版本历史需要有新的提交才好。适合于非开发时发布使用。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   901 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:54 · PVG 04:54 · LAX 13:54 · JFK 16:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.