Skip to content

Commit 199c6fe

Browse files
committed
build.gradle: Sync app version with package.json version
1 parent 239e592 commit 199c6fe

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

android/app/build.gradle

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: "com.android.application"
22

33
import com.android.build.OutputFile
44
import org.apache.tools.ant.taskdefs.condition.Os
5-
import groovy.swing.SwingBuilder
5+
import groovy.json.JsonSlurper
66

77
/**
88
* The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
@@ -92,6 +92,15 @@ project.ext.react = [
9292

9393
apply from: new File(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim(), "../react.gradle")
9494

95+
def getVersionFromPackageJson() {
96+
// Read and parse package.json file from project root
97+
def inputFile = new File("$rootDir/../package.json")
98+
def packageJson = new JsonSlurper().parseText(inputFile.text)
99+
100+
// Return the version, you can get any value this way
101+
return packageJson["version"]
102+
}
103+
95104
/**
96105
* Set this to true to create two separate APKs instead of one:
97106
* - An APK that only works on ARM devices
@@ -147,7 +156,7 @@ android {
147156
minSdkVersion rootProject.ext.minSdkVersion
148157
targetSdkVersion rootProject.ext.targetSdkVersion
149158
versionCode 1
150-
versionName "1.0.0"
159+
versionName getVersionFromPackageJson()
151160
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
152161

153162
if (isNewArchitectureEnabled()) {

0 commit comments

Comments
 (0)