Skip to content

fix(android): resolve correct app version with flavors in source map upload #1289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions android/sourcemaps.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ gradle.projectsEvaluated {
def start = name.startsWith(prefixes[0]) ? prefixes[0].length() : prefixes[1].length()
def end = name.length() - suffix.length()
def flavor = name.substring(start, end).uncapitalize()
def defaultVersion = getDefaultVersion(flavor)

task.finalizedBy createUploadSourcemapsTask(flavor)
task.finalizedBy createUploadSourcemapsTask(flavor, defaultVersion.name, defaultVersion.code)
}
}

Task createUploadSourcemapsTask(String flavor) {
Task createUploadSourcemapsTask(String flavor, String defaultVersionName, String defaultVersionCode) {
def name = 'uploadSourcemaps' + flavor.capitalize()

// Don't recreate the task if it already exists.
Expand Down Expand Up @@ -47,9 +48,8 @@ Task createUploadSourcemapsTask(String flavor) {
def inferredToken = executeShellScript(tokenShellFile, jsProjectDir)
def appToken = resolveVar('App Token', 'INSTABUG_APP_TOKEN', inferredToken)

def projectConfig = appProject.android.defaultConfig
def versionName = resolveVar('Version Name', 'INSTABUG_VERSION_NAME', "${projectConfig.versionName}")
def versionCode = resolveVar('Version Code', 'INSTABUG_VERSION_CODE', "${projectConfig.versionCode}")
def versionName = resolveVar('Version Name', 'INSTABUG_VERSION_NAME', defaultVersionName)
def versionCode = resolveVar('Version Code', 'INSTABUG_VERSION_CODE', defaultVersionCode)

exec {
def osCompatibility = Os.isFamily(Os.FAMILY_WINDOWS) ? ['cmd', '/c'] : []
Expand Down Expand Up @@ -100,6 +100,33 @@ File getSourceMapFile(File appDir, String flavor) {
return fallbackSourceMapFile
}

/**
* Infers the app version to use in source map upload based on the flavor.
* This is needed since different flavors may have different version codes and names (e.g. version suffixes).
*
* It checks the version for the flavor's variant.
* If no variant is found it falls back to the app's default config.
*
*
* @param flavor The flavor to get the app version for.
* @return A map containing the version code and version name.
*/
Map<String, String> getDefaultVersion(String flavor) {
def appProject = project(':app')
def defaultConfig = appProject.android.defaultConfig

def variants = appProject.android.applicationVariants

// uncapitalize is used to turn "Release" into "release" if the flavor is empty
def variantName = "${flavor}Release".uncapitalize()
def variant = variants.find { it.name.uncapitalize() == variantName }

def versionName = variant?.versionName ?: defaultConfig.versionName
def versionCode = variant?.versionCode ?: defaultConfig.versionCode

return [name: "${versionName}", code: "${versionCode}"]
}

boolean isUploadSourcemapsEnabled() {
def envValue = System.getenv('INSTABUG_SOURCEMAPS_UPLOAD_DISABLE')?.toBoolean()
def defaultValue = true
Expand Down