Skip to content

Commit

Permalink
Write an AndroidManifest.xml file to be merged with app module's mani…
Browse files Browse the repository at this point in the history
…fest.
  • Loading branch information
amanj120 committed Jul 23, 2020
1 parent f1a8a15 commit 183fd7f
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 7 deletions.
93 changes: 86 additions & 7 deletions platform/android/export/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,81 @@ class EditorExportPlatformAndroid : public EditorExportPlatform {
}
}

void _write_tmp_manifest(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, bool p_debug) {
String manifest_text =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n"
" xmlns:tools=\"http://schemas.android.com/tools\">\n";

manifest_text += _get_screen_sizes_tag(p_preset);
manifest_text += _get_gles_tag();

Vector<String> perms;
_get_permissions(p_preset, p_give_internet, perms);
for (int i = 0; i < perms.size(); i++) {
manifest_text += vformat(" <uses-permission android:name=\"%s\" />\n", perms.get(i));
}

bool uses_xr = (int)(p_preset->get("xr_features/xr_mode")) == 1;
if (uses_xr) {
int dof_index = p_preset->get("xr_features/degrees_of_freedom"); // 0: none, 1: 3dof and 6dof, 2: 6dof
if (dof_index == 1) {
manifest_text += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vr.headtracking\" android:required=\"false\" android:version=\"1\" />\n";
} else if (dof_index == 2) {
manifest_text += " <uses-feature tools:node=\"replace\" android:name=\"android.hardware.vr.headtracking\" android:required=\"true\" android:version=\"1\" />\n";
}
int hand_tracking_index = p_preset->get("xr_features/hand_tracking"); // 0: none, 1: optional, 2: required
if (hand_tracking_index == 1) {
manifest_text += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"false\" />\n";
} else if (hand_tracking_index == 2) {
manifest_text += " <uses-feature tools:node=\"replace\" android:name=\"oculus.software.handtracking\" android:required=\"true\" />\n";
}
}

String package_name = p_preset->get("package/unique_name");
manifest_text += vformat(
" <instrumentation\n"
" tools:node=\"replace\"\n"
" android:name=\".GodotInstrumentation\"\n"
" android:icon=\"@mipmap/icon\"\n"
" android:label=\"@string/godot_project_name_string\"\n"
" android:targetPackage=\"%s\" />\n",
package_name);

manifest_text +=
" <application android:label=\"@string/godot_project_name_string\"\n"
" android:allowBackup=\"false\" tools:ignore=\"GoogleAppIndexingWarning\"\n"
" android:icon=\"@mipmap/icon\">)\n";

String plugins_names = get_plugins_names(get_enabled_plugins(p_preset));
if (!plugins_names.empty()) {
manifest_text += vformat(" <meta-data tools:node=\"replace\" android:name=\"plugins\" android:value=\"%s\" />\n", plugins_names);
}

if (uses_xr) {
manifest_text += " <meta-data tools:node=\"replace\" android:name=\"com.samsung.android.vr.application.mode\" android:value=\"vr_only\" />\n";
}

String orientation = (int)(p_preset->get("screen/orientation")) == 1 ? "portrait" : "landscape";
manifest_text += vformat(
" <activity android:name=\"com.godot.game.GodotApp\" "
"tools:replace=\"android:screenOrientation\" "
"android:screenOrientation=\"%s\">\n",
orientation);

if (uses_xr) {
String focus_awareness = bool_to_string(p_preset->get("xr_features/focus_awareness"));
manifest_text += vformat(" <meta-data tools:node=\"replace\" android:name=\"com.oculus.vr.focusaware\" android:value=\"%s\" />\n", focus_awareness);
}

manifest_text += " </activity>\n";
manifest_text += " </application>\n";
manifest_text += "</manifest>\n";

String manifest_path = vformat("res://android/build/src/%s/AndroidManifest.xml", (p_debug ? "debug" : "release"));
store_string_at_path(manifest_path, manifest_text);
}

void _fix_manifest(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest, bool p_give_internet) {
// Leaving the unused types commented because looking these constants up
// again later would be annoying
Expand Down Expand Up @@ -2423,6 +2498,7 @@ class EditorExportPlatformAndroid : public EditorExportPlatform {
EditorProgress ep("export", "Exporting for Android", 105, true);

bool use_custom_build = bool(p_preset->get("custom_template/use_custom_build"));
bool p_give_internet = p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG);

Ref<Image> main_image;
Ref<Image> foreground;
Expand Down Expand Up @@ -2452,9 +2528,10 @@ class EditorExportPlatformAndroid : public EditorExportPlatform {
if (err != OK) {
EditorNode::add_io_error("Unable to overwrite res://android/build/res/*.xml files with project name");
}
// Copies the project icon files into the appropriate Gradle project directory
// Copies the project icon files into the appropriate Gradle project directory.
_copy_icons_to_gradle_project(p_preset, main_image, foreground, background);

// Write an AndroidManifest.xml file into the Gradle project directory.
_write_tmp_manifest(p_preset, p_give_internet, p_debug);
//build project if custom build is enabled
String sdk_path = EDITOR_GET("export/android/custom_build_sdk_path");

Expand All @@ -2476,6 +2553,8 @@ class EditorExportPlatformAndroid : public EditorExportPlatform {
build_command = build_path.plus_file(build_command);

String package_name = get_package_name(p_preset->get("package/unique_name"));
String version_code = itos(p_preset->get("version/code"));
String version_name = p_preset->get("version/name");

Vector<PluginConfig> enabled_plugins = get_enabled_plugins(p_preset);
String local_plugins_binaries = get_plugins_binaries(BINARY_TYPE_LOCAL, enabled_plugins);
Expand All @@ -2489,6 +2568,8 @@ class EditorExportPlatformAndroid : public EditorExportPlatform {
}
cmdline.push_back("build");
cmdline.push_back("-Pexport_package_name=" + package_name); // argument to specify the package name.
cmdline.push_back("-Pversion_code=" + version_code); // argument to specify the version code.
cmdline.push_back("-Pversion_name=" + version_name); // argument to specify the version name.
cmdline.push_back("-Pplugins_local_binaries=" + local_plugins_binaries); // argument to specify the list of plugins local dependencies.
cmdline.push_back("-Pplugins_remote_binaries=" + remote_plugins_binaries); // argument to specify the list of plugins remote dependencies.
cmdline.push_back("-Pplugins_maven_repos=" + custom_maven_repos); // argument to specify the list of custom maven repos for the plugins dependencies.
Expand Down Expand Up @@ -2610,12 +2691,10 @@ class EditorExportPlatformAndroid : public EditorExportPlatform {
unzCloseCurrentFile(pkg);

//write

if (file == "AndroidManifest.xml") {
_fix_manifest(p_preset, data, p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG));
}

if (!use_custom_build) {
if (file == "AndroidManifest.xml") {
_fix_manifest(p_preset, data, p_give_internet);
}
if (file == "resources.arsc") {
_fix_resources(p_preset, data);
}
Expand Down
24 changes: 24 additions & 0 deletions platform/android/export/gradle_export_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,28 @@ Error _create_project_name_strings_files(const Ref<EditorExportPreset> &p_preset
return OK;
}

String bool_to_string(bool v) {
return v ? "true" : "false";
}

String _get_gles_tag() {
bool min_gles3 = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name") == "GLES3" &&
!ProjectSettings::get_singleton()->get("rendering/quality/driver/fallback_to_gles2");
return min_gles3 ? " <uses-feature android:glEsVersion=\"0x00030000\" android:required=\"true\" />\n" : "";
}

String _get_screen_sizes_tag(const Ref<EditorExportPreset> &p_preset) {
String manifest_screen_sizes = " <supports-screens \n tools:node=\"replace\"";
String sizes[] = { "small", "normal", "large", "xlarge" };
size_t num_sizes = sizeof(sizes) / sizeof(sizes[0]);
for (size_t i = 0; i < num_sizes; i++) {
String feature_name = vformat("screen/support_%s", sizes[i]);
String feature_support = bool_to_string(p_preset->get(feature_name));
String xml_entry = vformat("\n android:%sScreens=\"%s\"", sizes[i], feature_support);
manifest_screen_sizes += xml_entry;
}
manifest_screen_sizes += " />\n";
return manifest_screen_sizes;
}

#endif //GODOT_GRADLE_EXPORT_UTIL_H
2 changes: 2 additions & 0 deletions platform/android/java/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ android {

// Feel free to modify the application id to your own.
applicationId getExportPackageName()
versionCode getExportVersionCode()
versionName getExportVersionName()
minSdkVersion versions.minSdk
targetSdkVersion versions.targetSdk
//CHUNK_ANDROID_DEFAULTCONFIG_BEGIN
Expand Down
16 changes: 16 additions & 0 deletions platform/android/java/app/config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ ext.getExportPackageName = { ->
return appId
}

ext.getExportVersionCode = { ->
String versionCode = project.hasProperty("version_code") ? project.property("version_code") : ""
if (versionCode == null || versionCode.isEmpty()) {
versionCode = "1"
}
return Integer.parseInt(versionCode)
}

ext.getExportVersionName = { ->
String versionName = project.hasProperty("version_name") ? project.property("version_name") : ""
if (versionName == null || versionName.isEmpty()) {
versionName = "1.0"
}
return versionName
}

final String PLUGIN_VALUE_SEPARATOR_REGEX = "\\|"

/**
Expand Down

0 comments on commit 183fd7f

Please sign in to comment.