Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Add commit sha and build time, drop PackageUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
FooIbar authored and asuka-mio committed Feb 24, 2023
1 parent 66e1315 commit 3c6d3bb
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 120 deletions.
22 changes: 21 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import java.io.ByteArrayOutputStream
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

plugins {
id("com.android.application")
kotlin("android")
Expand Down Expand Up @@ -30,6 +35,20 @@ android {
enableV4Signing = true
}

val commitSha by lazy {
val stdout = ByteArrayOutputStream()
exec {
commandLine = "git rev-parse --short=7 HEAD".split(' ')
standardOutput = stdout
}
stdout.toString().trim()
}

val buildTime by lazy {
val formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm").withZone(ZoneOffset.UTC)
formatter.format(Instant.now())
}

defaultConfig {
applicationId = "moe.tarsin.ehviewer"
minSdk = 28
Expand All @@ -52,6 +71,8 @@ android {
"nb-rNO"
)
)
buildConfigField("String", "COMMIT_SHA", "\"$commitSha\"")
buildConfigField("String", "BUILD_TIME", "\"$buildTime\"")
}

externalNativeBuild {
Expand All @@ -74,7 +95,6 @@ android {
"-opt-in=coil.annotation.ExperimentalCoilApi",
"-opt-in=androidx.compose.foundation.layout.ExperimentalLayoutApi",
"-opt-in=androidx.compose.material3.ExperimentalMaterial3Api",
"-opt-in=androidx.compose.material.ExperimentalMaterialApi",
"-opt-in=androidx.compose.ui.ExperimentalComposeUiApi",
"-opt-in=androidx.compose.foundation.ExperimentalFoundationApi",
"-opt-in=androidx.compose.animation.ExperimentalAnimationApi",
Expand Down
62 changes: 23 additions & 39 deletions app/src/main/java/com/hippo/ehviewer/Crash.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
*/
package com.hippo.ehviewer

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Debug
import com.hippo.util.PackageUtils
import com.hippo.util.ReadableTime
import com.hippo.yorozuya.FileUtils
import com.hippo.yorozuya.IOUtils
Expand All @@ -31,45 +28,32 @@ import java.io.PrintWriter
import java.util.Arrays

object Crash {
private fun avoidNull(str: String?): String {
return str ?: "null"
}

@Throws(IOException::class)
private fun collectInfo(context: Context, fw: FileWriter) {
try {
val pm = context.packageManager
val pi = pm.getPackageInfo(context.packageName, PackageManager.GET_ACTIVITIES)
if (pi != null) {
val versionName = if (pi.versionName == null) "null" else pi.versionName
val versionCode = pi.versionCode.toString()
fw.write("======== PackageInfo ========\r\n")
fw.write("PackageName=")
fw.write(pi.packageName)
fw.write("\r\n")
fw.write("VersionName=")
fw.write(versionName)
fw.write("\r\n")
fw.write("VersionCode=")
fw.write(versionCode)
fw.write("\r\n")
val signature = PackageUtils.getSignature(context, pi.packageName)
fw.write("Signature=")
fw.write(signature ?: "null")
fw.write("\r\n")
fw.write("\r\n")
}
} catch (e: PackageManager.NameNotFoundException) {
fw.write("======== PackageInfo ========\r\n")
fw.write("Can't get package information\r\n")
fw.write("\r\n")
}
private fun collectInfo(fw: FileWriter) {
fw.write("======== PackageInfo ========\r\n")
fw.write("PackageName=")
fw.write(BuildConfig.APPLICATION_ID)
fw.write("\r\n")
fw.write("VersionName=")
fw.write(BuildConfig.VERSION_NAME)
fw.write("\r\n")
fw.write("VersionCode=")
fw.write(BuildConfig.VERSION_CODE)
fw.write("\r\n")
fw.write("CommitSha=")
fw.write(BuildConfig.COMMIT_SHA)
fw.write("\r\n")
fw.write("BuildTime=")
fw.write(BuildConfig.BUILD_TIME)
fw.write("\r\n")
fw.write("\r\n")

// Runtime
val topActivityClazzName = "null"
val topActivityClazzName = EhApplication.application.topActivity?.javaClass?.name
fw.write("======== Runtime ========\r\n")
fw.write("TopActivity=")
fw.write(avoidNull(topActivityClazzName))
fw.write(topActivityClazzName ?: "null")
fw.write("\r\n")
fw.write("\r\n")
fw.write("\r\n")
Expand Down Expand Up @@ -134,7 +118,7 @@ object Crash {
fw.write(Build.VERSION.RELEASE)
fw.write("\r\n")
fw.write("SDK=")
fw.write(Integer.toString(Build.VERSION.SDK_INT))
fw.write(Build.VERSION.SDK_INT.toString())
fw.write("\r\n")
fw.write("MEMORY=")
fw.write(
Expand Down Expand Up @@ -163,7 +147,7 @@ object Crash {
}
}

fun saveCrashLog(context: Context, t: Throwable) {
fun saveCrashLog(t: Throwable) {
val dir = AppConfig.getExternalCrashDir() ?: return
val nowString = ReadableTime.getFilenamableTime(System.currentTimeMillis())
val fileName = "crash-$nowString.log"
Expand All @@ -175,7 +159,7 @@ object Crash {
fw.write(nowString)
fw.write("\r\n")
fw.write("\r\n")
collectInfo(context, fw)
collectInfo(fw)
fw.write("======== CrashInfo ========\r\n")
getThrowableInfo(t, fw)
fw.write("\r\n")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/hippo/ehviewer/EhApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class EhApplication : Application(), DefaultLifecycleObserver, ImageLoaderFactor
Thread.setDefaultUncaughtExceptionHandler { t, e ->
try {
if (Settings.getSaveCrashLog()) {
Crash.saveCrashLog(application, e)
Crash.saveCrashLog(e)
}
} catch (ignored: Throwable) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class VersionPreference @JvmOverloads constructor(
) : Preference(context, attrs) {
init {
setTitle(R.string.settings_about_version)
summary = BuildConfig.VERSION_NAME
summary = "${BuildConfig.VERSION_NAME} (${BuildConfig.COMMIT_SHA})\n" +
context.getString(R.string.settings_about_build_time, BuildConfig.BUILD_TIME)
}
}
78 changes: 0 additions & 78 deletions app/src/main/java/com/hippo/util/PackageUtils.java

This file was deleted.

1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@
<string name="settings_about_latest_release">最新版本</string>
<string name="settings_about_source">源码</string>
<string name="settings_about_version">版本号</string>
<string name="settings_about_build_time">于 %s 构建</string>
<string name="settings_about_check_for_updates">检查更新</string>
<!-- ExcludedLanguagesScene -->
<string name="please_wait">请稍候</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@
<string name="settings_about_latest_release">Latest release</string>
<string name="settings_about_source">Source</string>
<string name="settings_about_version">Build version</string>
<string name="settings_about_build_time">Built at %s</string>
<string name="settings_about_check_for_updates">Check for updates</string>
<string name="proxy_direct">Direct</string>
<string name="proxy_system">System Proxy</string>
Expand Down

4 comments on commit 3c6d3bb

@asuka-mio
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这时间在ci上貌似不太对劲 是因为有gradle缓存吗(

@FooIbar
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这时间在ci上貌似不太对劲 是因为有gradle缓存吗(

UTC时间,+8不是刚好吗

@asuka-mio
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这时间在ci上貌似不太对劲 是因为有gradle缓存吗(

UTC时间,+8不是刚好吗

但是在GitHub 服务器上跑出来就不是+8了(乐

@FooIbar
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这时间在ci上貌似不太对劲 是因为有gradle缓存吗(

UTC时间,+8不是刚好吗

但是在GitHub 服务器上跑出来就不是+8了(乐

https://github.com/Ehviewer-Overhauled/Ehviewer/actions/runs/4265257747
这个 2:36 跑的,构建时间是 18:37,没问题啊

Please sign in to comment.