Skip to content
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

Fix the gradle build configuration for the Android platform #66935

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions platform/android/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ else:
print("WARN: Architecture not suitable for embedding into APK; keeping .so at \\bin")

if lib_arch_dir != "":
if env.debug_features:
lib_type_dir = "debug"
elif env.dev_build:
if env.dev_build:
lib_type_dir = "dev"
elif env.debug_features:
lib_type_dir = "debug"
else: # Release
lib_type_dir = "release"

Expand Down
43 changes: 19 additions & 24 deletions platform/android/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ allprojects {

ext {
supportedAbis = ["arm32", "arm64", "x86_32", "x86_64"]
supportedTargetsMap = [release: "release", dev: "debug", debug: "release_debug"]
supportedFlavors = ["editor", "template"]
supportedFlavorsBuildTypes = [
// The editor can't be used with target=release as debugging tools are then not
// included, and it would crash on errors instead of reporting them.
"editor": ["dev", "debug"],
"template": ["dev", "debug", "release"]
]
Comment on lines 32 to +38
Copy link
Member

Choose a reason for hiding this comment

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

I don't know what gradle needs but I think it would be clearer if we followed the same structure as in scons, so:

supportedFlavors = ["editor", "template_debug", "template_release"]
supportedBuildTypes = ["regular", "dev"]  // In SCons it's just a boolean, so could be the same here if that works.

dev_build=yes can now be used with any target so target=template_release dev_build=yes is also a valid use case (gives template_release.dev lib). It's not a particularly useful combination but it's IMO better if gradle follows the same logic of having only three targets and then a bool for dev_build.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new build model unfortunately doesn't match with Gradle's build layout.
Gradle has the concept of build types and product flavors:

  • A product flavor is a different version of the project (e.g: demo version vs full version). This aligned somewhat better with the previous build architecture where tools=no corresponded to the template flavor and tools=yes corresponded to the editor flavor
  • A build type on the other hand mapped to our previous concept of target and is used configure the build configuration (debug symbols, optimization, etc)

With the new build model, we still have two product flavors ('editor' and 'template') and still three build types ('dev', 'release_debug' and 'release'), so the gradle build model is still valid, we just need to update the mapping when we generate the scons command.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The build types are also shared across the project modules, while the flavors are linked:

  • The app module maps to the template flavor
  • The editor module maps to the editor flavor
  • As a dependency, the lib module is configured to automatically update its flavor and build type to match.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds great, thanks for the explanation :)


// Used by gradle to specify which architecture to build for by default when running
// `./gradlew build` (this command is usually used by Android Studio).
Expand Down Expand Up @@ -88,7 +93,7 @@ task copyDebugAARToAppModule(type: Copy) {
dependsOn ':lib:assembleTemplateDebug'
from('lib/build/outputs/aar')
into('app/libs/debug')
include('godot-lib.debug.aar')
include('godot-lib.template_debug.aar')
}

/**
Expand All @@ -99,7 +104,7 @@ task copyDebugAARToBin(type: Copy) {
dependsOn ':lib:assembleTemplateDebug'
from('lib/build/outputs/aar')
into(binDir)
include('godot-lib.debug.aar')
include('godot-lib.template_debug.aar')
}

/**
Expand All @@ -110,7 +115,7 @@ task copyDevAARToAppModule(type: Copy) {
dependsOn ':lib:assembleTemplateDev'
from('lib/build/outputs/aar')
into('app/libs/dev')
include('godot-lib.dev.aar')
include('godot-lib.template_debug.dev.aar')
}

/**
Expand All @@ -121,7 +126,7 @@ task copyDevAARToBin(type: Copy) {
dependsOn ':lib:assembleTemplateDev'
from('lib/build/outputs/aar')
into(binDir)
include('godot-lib.dev.aar')
include('godot-lib.template_debug.dev.aar')
}

/**
Expand All @@ -132,7 +137,7 @@ task copyReleaseAARToAppModule(type: Copy) {
dependsOn ':lib:assembleTemplateRelease'
from('lib/build/outputs/aar')
into('app/libs/release')
include('godot-lib.release.aar')
include('godot-lib.template_release.aar')
}

/**
Expand All @@ -143,7 +148,7 @@ task copyReleaseAARToBin(type: Copy) {
dependsOn ':lib:assembleTemplateRelease'
from('lib/build/outputs/aar')
into(binDir)
include('godot-lib.release.aar')
include('godot-lib.template_release.aar')
}

/**
Expand All @@ -168,13 +173,8 @@ def templateExcludedBuildTask() {
if (!isAndroidStudio()) {
logger.lifecycle("Excluding Android studio build tasks")
for (String flavor : supportedFlavors) {
for (String buildType : supportedTargetsMap.keySet()) {
if (buildType == "release" && flavor == "editor") {
// The editor can't be used with target=release as debugging tools are then not
// included, and it would crash on errors instead of reporting them.
continue
}

String[] supportedBuildTypes = supportedFlavorsBuildTypes[flavor]
for (String buildType : supportedBuildTypes) {
for (String abi : selectedAbis) {
excludedTasks += ":lib:" + getSconsTaskName(flavor, buildType, abi)
}
Expand All @@ -188,7 +188,7 @@ def templateBuildTasks() {
def tasks = []

// Only build the apks and aar files for which we have native shared libraries.
for (String target : supportedTargetsMap.keySet()) {
for (String target : supportedFlavorsBuildTypes["template"]) {
File targetLibs = new File("lib/libs/" + target)
if (targetLibs != null
&& targetLibs.isDirectory()
Expand Down Expand Up @@ -240,12 +240,7 @@ task generateGodotEditor {

def tasks = []

for (String target : supportedTargetsMap.keySet()) {
if (target == "release") {
// The editor can't be used with target=release as debugging tools are then not
// included, and it would crash on errors instead of reporting them.
continue
}
for (String target : supportedFlavorsBuildTypes["editor"]) {
File targetLibs = new File("lib/libs/tools/" + target)
if (targetLibs != null
&& targetLibs.isDirectory()
Expand Down Expand Up @@ -322,9 +317,9 @@ task cleanGodotTemplates(type: Delete) {
delete("$binDir/android_dev.apk")
delete("$binDir/android_release.apk")
delete("$binDir/android_source.zip")
delete("$binDir/godot-lib.debug.aar")
delete("$binDir/godot-lib.dev.aar")
delete("$binDir/godot-lib.release.aar")
delete("$binDir/godot-lib.template_debug.aar")
delete("$binDir/godot-lib.template_debug.dev.aar")
delete("$binDir/godot-lib.template_release.aar")

finalizedBy getTasksByName("clean", true)
}
31 changes: 20 additions & 11 deletions platform/android/java/lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,34 @@ android {
throw new GradleException("Invalid product flavor: $flavorName")
}

boolean toolsFlag = flavorName == "editor"

def buildType = variant.buildType.name
if (buildType == null || buildType == "" || !supportedTargetsMap.containsKey(buildType)) {
if (buildType == null || buildType == "" || !supportedFlavorsBuildTypes[flavorName].contains(buildType)) {
throw new GradleException("Invalid build type: $buildType")
}

def sconsTarget = supportedTargetsMap[buildType]
if (sconsTarget == null || sconsTarget == "") {
throw new GradleException("Invalid scons target: $sconsTarget")
boolean devBuild = buildType == "dev"

def sconsTarget = flavorName
if (sconsTarget == "template") {
switch (buildType) {
case "release":
sconsTarget += "_release"
break
case "debug":
case "dev":
default:
sconsTarget += "_debug"
break;
}
}

// Update the name of the generated library
def outputSuffix = "${buildType}.aar"
if (toolsFlag) {
outputSuffix = "tools.$outputSuffix"
def outputSuffix = "${sconsTarget}"
if (devBuild) {
outputSuffix = "${outputSuffix}.dev"
}
variant.outputs.all { output ->
output.outputFileName = "godot-lib.${outputSuffix}"
output.outputFileName = "godot-lib.${outputSuffix}.aar"
}

// Find scons' executable path
Expand Down Expand Up @@ -159,7 +168,7 @@ android {
def taskName = getSconsTaskName(flavorName, buildType, selectedAbi)
tasks.create(name: taskName, type: Exec) {
executable sconsExecutableFile.absolutePath
args "--directory=${pathToRootDir}", "platform=android", "tools=${toolsFlag}", "target=${sconsTarget}", "arch=${selectedAbi}", "-j" + Runtime.runtime.availableProcessors()
args "--directory=${pathToRootDir}", "platform=android", "dev_mode=${devBuild}", "dev_build=${devBuild}", "target=${sconsTarget}", "arch=${selectedAbi}", "-j" + Runtime.runtime.availableProcessors()
}

// Schedule the tasks so the generated libs are present before the aar file is packaged.
Expand Down