The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1994 lines
95KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. class AndroidProjectExporter : public ProjectExporter
  21. {
  22. public:
  23. //==============================================================================
  24. bool isXcode() const override { return false; }
  25. bool isVisualStudio() const override { return false; }
  26. bool isCodeBlocks() const override { return false; }
  27. bool isMakefile() const override { return false; }
  28. bool isAndroidStudio() const override { return true; }
  29. bool isAndroid() const override { return true; }
  30. bool isWindows() const override { return false; }
  31. bool isLinux() const override { return false; }
  32. bool isOSX() const override { return false; }
  33. bool isiOS() const override { return false; }
  34. bool usesMMFiles() const override { return false; }
  35. bool canCopeWithDuplicateFiles() override { return false; }
  36. bool supportsUserDefinedConfigurations() const override { return true; }
  37. String getNewLineString() const override { return "\n"; }
  38. bool supportsTargetType (build_tools::ProjectType::Target::Type type) const override
  39. {
  40. return type == build_tools::ProjectType::Target::GUIApp || type == build_tools::ProjectType::Target::StaticLibrary
  41. || type == build_tools::ProjectType::Target::DynamicLibrary || type == build_tools::ProjectType::Target::StandalonePlugIn;
  42. }
  43. //==============================================================================
  44. void addPlatformSpecificSettingsForProjectType (const build_tools::ProjectType&) override
  45. {
  46. // no-op.
  47. }
  48. //==============================================================================
  49. void createExporterProperties (PropertyListBuilder& props) override
  50. {
  51. createBaseExporterProperties (props);
  52. createToolchainExporterProperties (props);
  53. createManifestExporterProperties (props);
  54. createCodeSigningExporterProperties (props);
  55. createOtherExporterProperties (props);
  56. }
  57. void updateDeprecatedSettings() override
  58. {
  59. updateExternalReadPermission();
  60. updateBluetoothPermission();
  61. }
  62. static String getDisplayName() { return "Android"; }
  63. static String getValueTreeTypeName() { return "ANDROIDSTUDIO"; }
  64. static String getTargetFolderName() { return "Android"; }
  65. Identifier getExporterIdentifier() const override { return getValueTreeTypeName(); }
  66. static const char* getDefaultActivityClass() { return "com.rmsl.juce.JuceActivity"; }
  67. static const char* getDefaultApplicationClass() { return "com.rmsl.juce.JuceApp"; }
  68. static AndroidProjectExporter* createForSettings (Project& projectToUse, const ValueTree& settingsToUse)
  69. {
  70. if (settingsToUse.hasType (getValueTreeTypeName()))
  71. return new AndroidProjectExporter (projectToUse, settingsToUse);
  72. return {};
  73. }
  74. //==============================================================================
  75. ValueTreePropertyWithDefault androidJavaLibs, androidAdditionalJavaFolders, androidAdditionalResourceFolders, androidProjectRepositories,
  76. androidRepositories, androidDependencies, androidCustomAppBuildGradleContent, androidScreenOrientation,
  77. androidCustomActivityClass, androidCustomApplicationClass, androidManifestCustomXmlElements,
  78. androidGradleSettingsContent, androidVersionCode, androidMinimumSDK, androidTargetSDK, androidTheme,
  79. androidExtraAssetsFolder, androidOboeRepositoryPath, androidInternetNeeded, androidMicNeeded, androidCameraNeeded,
  80. androidBluetoothScanNeeded, androidBluetoothAdvertiseNeeded, androidBluetoothConnectNeeded,
  81. androidReadMediaAudioPermission, androidReadMediaImagesPermission,
  82. androidReadMediaVideoPermission, androidExternalWritePermission,
  83. androidInAppBillingPermission, androidVibratePermission, androidOtherPermissions, androidPushNotifications,
  84. androidEnableRemoteNotifications, androidRemoteNotificationsConfigFile, androidEnableContentSharing, androidKeyStore,
  85. androidKeyStorePass, androidKeyAlias, androidKeyAliasPass, gradleVersion, gradleToolchain, androidPluginVersion;
  86. //==============================================================================
  87. AndroidProjectExporter (Project& p, const ValueTree& t)
  88. : ProjectExporter (p, t),
  89. androidJavaLibs (settings, Ids::androidJavaLibs, getUndoManager()),
  90. androidAdditionalJavaFolders (settings, Ids::androidAdditionalJavaFolders, getUndoManager()),
  91. androidAdditionalResourceFolders (settings, Ids::androidAdditionalResourceFolders, getUndoManager()),
  92. androidProjectRepositories (settings, Ids::androidProjectRepositories, getUndoManager(), "google()\nmavenCentral()"),
  93. androidRepositories (settings, Ids::androidRepositories, getUndoManager()),
  94. androidDependencies (settings, Ids::androidDependencies, getUndoManager()),
  95. androidCustomAppBuildGradleContent (settings, Ids::androidCustomAppBuildGradleContent, getUndoManager()),
  96. androidScreenOrientation (settings, Ids::androidScreenOrientation, getUndoManager(), "unspecified"),
  97. androidCustomActivityClass (settings, Ids::androidCustomActivityClass, getUndoManager()),
  98. androidCustomApplicationClass (settings, Ids::androidCustomApplicationClass, getUndoManager(), getDefaultApplicationClass()),
  99. androidManifestCustomXmlElements (settings, Ids::androidManifestCustomXmlElements, getUndoManager()),
  100. androidGradleSettingsContent (settings, Ids::androidGradleSettingsContent, getUndoManager()),
  101. androidVersionCode (settings, Ids::androidVersionCode, getUndoManager(), "1"),
  102. androidMinimumSDK (settings, Ids::androidMinimumSDK, getUndoManager(), "16"),
  103. androidTargetSDK (settings, Ids::androidTargetSDK, getUndoManager(), "33"),
  104. androidTheme (settings, Ids::androidTheme, getUndoManager()),
  105. androidExtraAssetsFolder (settings, Ids::androidExtraAssetsFolder, getUndoManager()),
  106. androidOboeRepositoryPath (settings, Ids::androidOboeRepositoryPath, getUndoManager()),
  107. androidInternetNeeded (settings, Ids::androidInternetNeeded, getUndoManager(), true),
  108. androidMicNeeded (settings, Ids::microphonePermissionNeeded, getUndoManager(), false),
  109. androidCameraNeeded (settings, Ids::cameraPermissionNeeded, getUndoManager(), false),
  110. androidBluetoothScanNeeded (settings, Ids::androidBluetoothScanNeeded, getUndoManager(), false),
  111. androidBluetoothAdvertiseNeeded (settings, Ids::androidBluetoothAdvertiseNeeded, getUndoManager(), false),
  112. androidBluetoothConnectNeeded (settings, Ids::androidBluetoothConnectNeeded, getUndoManager(), false),
  113. androidReadMediaAudioPermission (settings, Ids::androidReadMediaAudioPermission, getUndoManager(), true),
  114. androidReadMediaImagesPermission (settings, Ids::androidReadMediaImagesPermission, getUndoManager(), true),
  115. androidReadMediaVideoPermission (settings, Ids::androidReadMediaVideoPermission, getUndoManager(), true),
  116. androidExternalWritePermission (settings, Ids::androidExternalWriteNeeded, getUndoManager(), true),
  117. androidInAppBillingPermission (settings, Ids::androidInAppBilling, getUndoManager(), false),
  118. androidVibratePermission (settings, Ids::androidVibratePermissionNeeded, getUndoManager(), false),
  119. androidOtherPermissions (settings, Ids::androidOtherPermissions, getUndoManager()),
  120. androidPushNotifications (settings, Ids::androidPushNotifications, getUndoManager(), ! isLibrary()),
  121. androidEnableRemoteNotifications (settings, Ids::androidEnableRemoteNotifications, getUndoManager(), false),
  122. androidRemoteNotificationsConfigFile (settings, Ids::androidRemoteNotificationsConfigFile, getUndoManager()),
  123. androidEnableContentSharing (settings, Ids::androidEnableContentSharing, getUndoManager(), false),
  124. androidKeyStore (settings, Ids::androidKeyStore, getUndoManager(), "${user.home}/.android/debug.keystore"),
  125. androidKeyStorePass (settings, Ids::androidKeyStorePass, getUndoManager(), "android"),
  126. androidKeyAlias (settings, Ids::androidKeyAlias, getUndoManager(), "androiddebugkey"),
  127. androidKeyAliasPass (settings, Ids::androidKeyAliasPass, getUndoManager(), "android"),
  128. gradleVersion (settings, Ids::gradleVersion, getUndoManager(), "7.5.1"),
  129. gradleToolchain (settings, Ids::gradleToolchain, getUndoManager(), "clang"),
  130. androidPluginVersion (settings, Ids::androidPluginVersion, getUndoManager(), "7.3.0"),
  131. AndroidExecutable (getAppSettings().getStoredPath (Ids::androidStudioExePath, TargetOS::getThisOS()).get().toString())
  132. {
  133. name = getDisplayName();
  134. targetLocationValue.setDefault (getDefaultBuildsRootFolder() + getTargetFolderName());
  135. }
  136. //==============================================================================
  137. void createToolchainExporterProperties (PropertyListBuilder& props)
  138. {
  139. props.add (new TextPropertyComponent (gradleVersion, "Gradle Version", 32, false),
  140. "The version of gradle that is used to build this app (4.10 is fine for JUCE)");
  141. props.add (new TextPropertyComponent (androidPluginVersion, "Android Plug-in Version", 32, false),
  142. "The version of the android build plugin for gradle that is used to build this app");
  143. props.add (new ChoicePropertyComponent (gradleToolchain, "NDK Toolchain",
  144. { "clang", "gcc" },
  145. { "clang", "gcc" }),
  146. "The toolchain that gradle should invoke for NDK compilation (variable model.android.ndk.tooclhain in app/build.gradle)");
  147. }
  148. //==============================================================================
  149. bool canLaunchProject() override
  150. {
  151. return AndroidExecutable.exists();
  152. }
  153. bool launchProject() override
  154. {
  155. if (! AndroidExecutable.exists())
  156. {
  157. jassertfalse;
  158. return false;
  159. }
  160. auto targetFolder = getTargetFolder();
  161. // we have to surround the path with extra quotes, otherwise Android Studio
  162. // will choke if there are any space characters in the path.
  163. return AndroidExecutable.startAsProcess ("\"" + targetFolder.getFullPathName() + "\"");
  164. }
  165. //==============================================================================
  166. void create (const OwnedArray<LibraryModule>& modules) const override
  167. {
  168. auto targetFolder = getTargetFolder();
  169. auto appFolder = targetFolder.getChildFile (isLibrary() ? "lib" : "app");
  170. removeOldFiles (targetFolder);
  171. copyExtraResourceFiles();
  172. writeFile (targetFolder, "settings.gradle", getGradleSettingsFileContent());
  173. writeFile (targetFolder, "build.gradle", getProjectBuildGradleFileContent());
  174. writeFile (appFolder, "build.gradle", getAppBuildGradleFileContent (modules));
  175. writeFile (targetFolder, "local.properties", getLocalPropertiesFileContent());
  176. writeFile (targetFolder, "gradle/wrapper/gradle-wrapper.properties", getGradleWrapperPropertiesFileContent());
  177. writeBinaryFile (targetFolder, "gradle/wrapper/LICENSE-for-gradlewrapper.txt", BinaryData::LICENSE, BinaryData::LICENSESize);
  178. writeBinaryFile (targetFolder, "gradle/wrapper/gradle-wrapper.jar", BinaryData::gradlewrapper_jar, BinaryData::gradlewrapper_jarSize);
  179. writeBinaryFile (targetFolder, "gradlew", BinaryData::gradlew, BinaryData::gradlewSize);
  180. writeBinaryFile (targetFolder, "gradlew.bat", BinaryData::gradlew_bat, BinaryData::gradlew_batSize);
  181. targetFolder.getChildFile ("gradlew").setExecutePermission (true);
  182. writeAndroidManifest (appFolder);
  183. if (! isLibrary())
  184. {
  185. copyAdditionalJavaLibs (appFolder);
  186. writeStringsXML (targetFolder);
  187. writeAppIcons (targetFolder);
  188. }
  189. writeCmakeFile (appFolder.getChildFile ("CMakeLists.txt"));
  190. auto androidExtraAssetsFolderValue = androidExtraAssetsFolder.get().toString();
  191. if (androidExtraAssetsFolderValue.isNotEmpty())
  192. {
  193. auto extraAssets = getProject().getFile().getParentDirectory().getChildFile (androidExtraAssetsFolderValue);
  194. if (extraAssets.exists() && extraAssets.isDirectory())
  195. {
  196. auto assetsFolder = appFolder.getChildFile ("src/main/assets");
  197. if (assetsFolder.deleteRecursively())
  198. extraAssets.copyDirectoryTo (assetsFolder);
  199. }
  200. }
  201. }
  202. void removeOldFiles (const File& targetFolder) const
  203. {
  204. targetFolder.getChildFile ("app/build").deleteRecursively();
  205. targetFolder.getChildFile ("app/build.gradle").deleteFile();
  206. targetFolder.getChildFile ("gradle").deleteRecursively();
  207. targetFolder.getChildFile ("local.properties").deleteFile();
  208. targetFolder.getChildFile ("settings.gradle").deleteFile();
  209. }
  210. void writeFile (const File& gradleProjectFolder, const String& filePath, const String& fileContent) const
  211. {
  212. build_tools::writeStreamToFile (gradleProjectFolder.getChildFile (filePath), [&] (MemoryOutputStream& mo)
  213. {
  214. mo.setNewLineString (getNewLineString());
  215. mo << fileContent;
  216. });
  217. }
  218. void writeBinaryFile (const File& gradleProjectFolder, const String& filePath, const char* binaryData, const int binarySize) const
  219. {
  220. build_tools::writeStreamToFile (gradleProjectFolder.getChildFile (filePath), [&] (MemoryOutputStream& mo)
  221. {
  222. mo.setNewLineString (getNewLineString());
  223. mo.write (binaryData, static_cast<size_t> (binarySize));
  224. });
  225. }
  226. protected:
  227. //==============================================================================
  228. class AndroidBuildConfiguration : public BuildConfiguration
  229. {
  230. public:
  231. AndroidBuildConfiguration (Project& p, const ValueTree& settings, const ProjectExporter& e)
  232. : BuildConfiguration (p, settings, e),
  233. androidArchitectures (config, Ids::androidArchitectures, getUndoManager(), isDebug() ? "armeabi-v7a x86 arm64-v8a x86_64" : ""),
  234. androidBuildConfigRemoteNotifsConfigFile (config, Ids::androidBuildConfigRemoteNotifsConfigFile, getUndoManager()),
  235. androidAdditionalXmlValueResources (config, Ids::androidAdditionalXmlValueResources, getUndoManager()),
  236. androidAdditionalDrawableResources (config, Ids::androidAdditionalDrawableResources, getUndoManager()),
  237. androidAdditionalRawValueResources (config, Ids::androidAdditionalRawValueResources, getUndoManager()),
  238. androidCustomStringXmlElements (config, Ids::androidCustomStringXmlElements, getUndoManager())
  239. {
  240. linkTimeOptimisationValue.setDefault (false);
  241. optimisationLevelValue.setDefault (isDebug() ? gccO0 : gccO3);
  242. }
  243. String getArchitectures() const { return androidArchitectures.get().toString(); }
  244. String getRemoteNotifsConfigFile() const { return androidBuildConfigRemoteNotifsConfigFile.get().toString(); }
  245. String getAdditionalXmlResources() const { return androidAdditionalXmlValueResources.get().toString(); }
  246. String getAdditionalDrawableResources() const { return androidAdditionalDrawableResources.get().toString(); }
  247. String getAdditionalRawResources() const { return androidAdditionalRawValueResources.get().toString();}
  248. String getCustomStringsXml() const { return androidCustomStringXmlElements.get().toString(); }
  249. void createConfigProperties (PropertyListBuilder& props) override
  250. {
  251. addRecommendedLLVMCompilerWarningsProperty (props);
  252. addGCCOptimisationProperty (props);
  253. props.add (new TextPropertyComponent (androidArchitectures, "Architectures", 256, false),
  254. "A list of the architectures to build (for a fat binary). Leave empty to build for all possible android architectures.");
  255. props.add (new TextPropertyComponent (androidBuildConfigRemoteNotifsConfigFile.getPropertyAsValue(), "Remote Notifications Config File", 2048, false),
  256. "Path to google-services.json file. This will be the file provided by Firebase when creating a new app in Firebase console. "
  257. "This will override the setting from the main Android exporter node.");
  258. props.add (new TextPropertyComponent (androidAdditionalXmlValueResources, "Extra Android XML Value Resources", 8192, true),
  259. "Paths to additional \"value resource\" files in XML format that should be included in the app (one per line). "
  260. "If you have additional XML resources that should be treated as value resources, add them here.");
  261. props.add (new TextPropertyComponent (androidAdditionalDrawableResources, "Extra Android Drawable Resources", 8192, true),
  262. "Paths to additional \"drawable resource\" directories that should be included in the app (one per line). "
  263. "They will be added to \"res\" directory of Android project. "
  264. "Each path should point to a directory named \"drawable\" or \"drawable-<size>\" where <size> should be "
  265. "something like \"hdpi\", \"ldpi\", \"xxxhdpi\" etc, for instance \"drawable-xhdpi\". "
  266. "Refer to Android Studio documentation for available sizes.");
  267. props.add (new TextPropertyComponent (androidAdditionalRawValueResources, "Extra Android Raw Resources", 8192, true),
  268. "Paths to additional \"raw resource\" files that should be included in the app (one per line). "
  269. "Resource file names must contain only lowercase a-z, 0-9 or underscore.");
  270. props.add (new TextPropertyComponent (androidCustomStringXmlElements, "Custom String Resources", 8192, true),
  271. "Custom XML resources that will be added to string.xml as children of <resources> element. "
  272. "Example: \n<string name=\"value\">text</string>\n"
  273. "<string name2=\"value2\">text2</string>\n");
  274. }
  275. String getProductFlavourNameIdentifier() const
  276. {
  277. return getName().toLowerCase().replaceCharacter (L' ', L'_') + String ("_");
  278. }
  279. String getProductFlavourCMakeIdentifier() const
  280. {
  281. return getName().toUpperCase().replaceCharacter (L' ', L'_');
  282. }
  283. String getModuleLibraryArchName() const override
  284. {
  285. return "${ANDROID_ABI}";
  286. }
  287. ValueTreePropertyWithDefault androidArchitectures, androidBuildConfigRemoteNotifsConfigFile,
  288. androidAdditionalXmlValueResources, androidAdditionalDrawableResources,
  289. androidAdditionalRawValueResources, androidCustomStringXmlElements;
  290. };
  291. BuildConfiguration::Ptr createBuildConfig (const ValueTree& v) const override
  292. {
  293. return *new AndroidBuildConfiguration (project, v, *this);
  294. }
  295. private:
  296. void updateExternalReadPermission()
  297. {
  298. const auto needsExternalRead = getSettingString (Ids::androidExternalReadNeeded);
  299. settings.removeProperty (Ids::androidExternalReadNeeded, nullptr);
  300. if (needsExternalRead.isEmpty())
  301. return;
  302. androidReadMediaAudioPermission .setValue (needsExternalRead, nullptr);
  303. androidReadMediaImagesPermission.setValue (needsExternalRead, nullptr);
  304. androidReadMediaVideoPermission .setValue (needsExternalRead, nullptr);
  305. }
  306. void updateBluetoothPermission()
  307. {
  308. const auto needsBluetooth = getSettingString (Ids::androidBluetoothNeeded);
  309. settings.removeProperty (Ids::androidBluetoothNeeded, nullptr);
  310. if (needsBluetooth.isEmpty())
  311. return;
  312. androidBluetoothScanNeeded .setValue (needsBluetooth, nullptr);
  313. androidBluetoothAdvertiseNeeded.setValue (needsBluetooth, nullptr);
  314. androidBluetoothConnectNeeded .setValue (needsBluetooth, nullptr);
  315. }
  316. void writeCmakeFile (const File& file) const
  317. {
  318. build_tools::writeStreamToFile (file, [&] (MemoryOutputStream& mo)
  319. {
  320. mo.setNewLineString (getNewLineString());
  321. mo << "# Automatically generated CMakeLists, created by the Projucer" << newLine
  322. << "# Don't edit this file! Your changes will be overwritten when you re-save the Projucer project!" << newLine
  323. << newLine
  324. << "cmake_minimum_required(VERSION 3.22)" << newLine
  325. << newLine
  326. << "project(juce_jni_project)" << newLine
  327. << newLine;
  328. if (! isLibrary())
  329. mo << "set(BINARY_NAME \"juce_jni\")" << newLine << newLine;
  330. auto useOboe = project.getEnabledModules().isModuleEnabled ("juce_audio_devices")
  331. && project.isConfigFlagEnabled ("JUCE_USE_ANDROID_OBOE", true);
  332. if (useOboe)
  333. {
  334. auto oboePath = [&]
  335. {
  336. auto oboeDir = androidOboeRepositoryPath.get().toString().trim();
  337. if (oboeDir.isEmpty())
  338. oboeDir = getModuleFolderRelativeToProject ("juce_audio_devices").getChildFile ("native")
  339. .getChildFile ("oboe")
  340. .rebased (getProject().getProjectFolder(), getTargetFolder(),
  341. build_tools::RelativePath::buildTargetFolder)
  342. .toUnixStyle();
  343. // CMakeLists.txt is in the "app" subfolder
  344. if (! build_tools::isAbsolutePath (oboeDir))
  345. oboeDir = "../" + oboeDir;
  346. return expandHomeFolderToken (oboeDir);
  347. }();
  348. mo << "set(OBOE_DIR " << oboePath.quoted() << ")" << newLine << newLine;
  349. mo << "add_subdirectory (${OBOE_DIR} ./oboe)" << newLine << newLine;
  350. }
  351. String cpufeaturesPath ("${ANDROID_NDK}/sources/android/cpufeatures/cpu-features.c");
  352. mo << "add_library(\"cpufeatures\" STATIC \"" << cpufeaturesPath << "\")" << newLine
  353. << "set_source_files_properties(\"" << cpufeaturesPath << "\" PROPERTIES COMPILE_FLAGS \"-Wno-sign-conversion -Wno-gnu-statement-expression\")" << newLine << newLine;
  354. {
  355. auto projectDefines = getEscapedPreprocessorDefs (getProjectPreprocessorDefs());
  356. if (projectDefines.size() > 0)
  357. mo << "add_definitions(" << projectDefines.joinIntoString (" ") << ")" << newLine << newLine;
  358. }
  359. {
  360. mo << "include_directories( AFTER" << newLine;
  361. for (auto& path : extraSearchPaths)
  362. mo << " \"" << escapeDirectoryForCmake (path) << "\"" << newLine;
  363. mo << " \"${ANDROID_NDK}/sources/android/cpufeatures\"" << newLine;
  364. mo << ")" << newLine << newLine;
  365. }
  366. mo << "enable_language(ASM)" << newLine << newLine;
  367. const auto userLibraries = getUserLibraries();
  368. if (getNumConfigurations() > 0)
  369. {
  370. bool first = true;
  371. for (ConstConfigIterator config (*this); config.next();)
  372. {
  373. auto& cfg = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  374. auto libSearchPaths = cfg.getLibrarySearchPaths();
  375. auto cfgDefines = getConfigPreprocessorDefs (cfg);
  376. auto cfgHeaderPaths = cfg.getHeaderSearchPaths();
  377. auto cfgLibraryPaths = cfg.getLibrarySearchPaths();
  378. if (! isLibrary() && libSearchPaths.size() == 0 && cfgDefines.size() == 0
  379. && cfgHeaderPaths.size() == 0 && cfgLibraryPaths.size() == 0)
  380. continue;
  381. mo << (first ? "if" : "elseif") << "(JUCE_BUILD_CONFIGURATION MATCHES \"" << cfg.getProductFlavourCMakeIdentifier() <<"\")" << newLine;
  382. if (isLibrary())
  383. {
  384. mo << " set(BINARY_NAME \"" << getNativeModuleBinaryName (cfg) << "\")" << newLine;
  385. auto binaryLocation = cfg.getTargetBinaryRelativePathString();
  386. if (binaryLocation.isNotEmpty())
  387. {
  388. auto locationRelativeToCmake = build_tools::RelativePath (binaryLocation, build_tools::RelativePath::projectFolder)
  389. .rebased (getProject().getFile().getParentDirectory(),
  390. file.getParentDirectory(), build_tools::RelativePath::buildTargetFolder);
  391. mo << " set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY \"" << "../../../../" << locationRelativeToCmake.toUnixStyle() << "\")" << newLine;
  392. }
  393. }
  394. writeCmakePathLines (mo, " ", "link_directories(", libSearchPaths);
  395. if (cfgDefines.size() > 0)
  396. mo << " add_definitions(" << getEscapedPreprocessorDefs (cfgDefines).joinIntoString (" ") << ")" << newLine;
  397. const auto cfgExtraLinkerFlags = cfg.getAllLinkerFlagsString();
  398. if (cfgExtraLinkerFlags.isNotEmpty())
  399. {
  400. mo << " set( JUCE_LDFLAGS \"" << cfgExtraLinkerFlags.replace ("\"", "\\\"") << "\" )" << newLine
  401. << " set( CMAKE_SHARED_LINKER_FLAGS \"${CMAKE_SHARED_LINKER_FLAGS} ${JUCE_LDFLAGS}\" )" << newLine << newLine;
  402. }
  403. writeCmakePathLines (mo, " ", "include_directories( AFTER", cfgHeaderPaths);
  404. if (userLibraries.size() > 0)
  405. {
  406. for (auto& lib : userLibraries)
  407. {
  408. String findLibraryCmd;
  409. findLibraryCmd << "find_library(" << lib.toLowerCase().replaceCharacter (L' ', L'_')
  410. << " \"" << lib << "\" PATHS";
  411. writeCmakePathLines (mo, " ", findLibraryCmd, cfgLibraryPaths, " NO_CMAKE_FIND_ROOT_PATH)");
  412. }
  413. mo << newLine;
  414. }
  415. if (cfg.isLinkTimeOptimisationEnabled())
  416. {
  417. // There's no MIPS support for LTO
  418. String mipsCondition ("NOT (ANDROID_ABI STREQUAL \"mips\" OR ANDROID_ABI STREQUAL \"mips64\")");
  419. mo << " if(" << mipsCondition << ")" << newLine;
  420. StringArray cmakeVariables ("CMAKE_C_FLAGS", "CMAKE_CXX_FLAGS", "CMAKE_EXE_LINKER_FLAGS");
  421. for (auto& variable : cmakeVariables)
  422. {
  423. auto configVariable = variable + "_" + cfg.getProductFlavourCMakeIdentifier();
  424. mo << " set(" << configVariable << " \"${" << configVariable << "} -flto\")" << newLine;
  425. }
  426. mo << " endif()" << newLine;
  427. }
  428. first = false;
  429. }
  430. if (! first)
  431. {
  432. ProjectExporter::BuildConfiguration::Ptr config (getConfiguration(0));
  433. if (config)
  434. {
  435. if (dynamic_cast<const AndroidBuildConfiguration*> (config.get()) != nullptr)
  436. {
  437. mo << "else()" << newLine;
  438. mo << " message( FATAL_ERROR \"No matching build-configuration found.\" )" << newLine;
  439. mo << "endif()" << newLine << newLine;
  440. }
  441. }
  442. }
  443. }
  444. Array<build_tools::RelativePath> excludeFromBuild;
  445. Array<std::pair<build_tools::RelativePath, String>> extraCompilerFlags;
  446. mo << "add_library( ${BINARY_NAME}" << newLine;
  447. mo << newLine;
  448. mo << " " << (getProject().getProjectType().isStaticLibrary() ? "STATIC" : "SHARED") << newLine;
  449. mo << newLine;
  450. addCompileUnits (mo, excludeFromBuild, extraCompilerFlags);
  451. mo << ")" << newLine << newLine;
  452. if (excludeFromBuild.size() > 0)
  453. {
  454. mo << "set_source_files_properties(" << newLine;
  455. for (auto& exclude : excludeFromBuild)
  456. mo << " \"" << exclude.toUnixStyle() << '"' << newLine;
  457. mo << " PROPERTIES HEADER_FILE_ONLY TRUE)" << newLine;
  458. mo << newLine;
  459. }
  460. if (! extraCompilerFlags.isEmpty())
  461. {
  462. for (auto& extra : extraCompilerFlags)
  463. mo << "set_source_files_properties(\"" << extra.first.toUnixStyle() << "\" PROPERTIES COMPILE_FLAGS " << extra.second << " )" << newLine;
  464. mo << newLine;
  465. }
  466. for (ConstConfigIterator config (*this); config.next();)
  467. {
  468. auto& cfg = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  469. mo << "if( JUCE_BUILD_CONFIGURATION MATCHES \"" << cfg.getProductFlavourCMakeIdentifier() << "\" )" << newLine
  470. << " target_compile_options( ${BINARY_NAME} PRIVATE";
  471. const auto recommendedFlags = cfg.getRecommendedCompilerWarningFlags();
  472. for (auto& recommendedFlagsType : { recommendedFlags.common, recommendedFlags.cpp })
  473. for (auto& flag : recommendedFlagsType)
  474. mo << " " << flag;
  475. const auto flags = getConfigCompilerFlags (cfg);
  476. if (! flags.isEmpty())
  477. mo << " " << flags.joinIntoString (" ");
  478. mo << " )" << newLine
  479. << "endif()" << newLine
  480. << newLine;
  481. }
  482. auto libraries = getAndroidLibraries();
  483. if (libraries.size() > 0)
  484. {
  485. for (auto& lib : libraries)
  486. mo << "find_library(" << lib.toLowerCase().replaceCharacter (L' ', L'_') << " \"" << lib << "\")" << newLine;
  487. mo << newLine;
  488. }
  489. mo << "target_link_libraries( ${BINARY_NAME}";
  490. if (libraries.size() > 0)
  491. {
  492. mo << newLine << newLine;
  493. for (auto& lib : libraries)
  494. mo << " ${" << lib.toLowerCase().replaceCharacter (L' ', L'_') << "}" << newLine;
  495. mo << " \"cpufeatures\"" << newLine;
  496. }
  497. if (useOboe)
  498. mo << " \"oboe\"" << newLine;
  499. for (auto& lib : userLibraries)
  500. mo << " [[" << lib << "]]" << newLine;
  501. mo << ")" << newLine;
  502. });
  503. }
  504. //==============================================================================
  505. String getGradleSettingsFileContent() const
  506. {
  507. MemoryOutputStream mo;
  508. mo.setNewLineString (getNewLineString());
  509. mo << "rootProject.name = " << "\'" << escapeQuotes (projectName) << "\'" << newLine;
  510. mo << (isLibrary() ? "include ':lib'" : "include ':app'");
  511. auto extraContent = androidGradleSettingsContent.get().toString();
  512. if (extraContent.isNotEmpty())
  513. mo << newLine << extraContent << newLine;
  514. return mo.toString();
  515. }
  516. String getProjectBuildGradleFileContent() const
  517. {
  518. MemoryOutputStream mo;
  519. mo.setNewLineString (getNewLineString());
  520. mo << "buildscript {" << newLine;
  521. mo << " repositories {" << newLine;
  522. mo << " google()" << newLine;
  523. mo << " mavenCentral()" << newLine;
  524. mo << " }" << newLine;
  525. mo << " dependencies {" << newLine;
  526. mo << " classpath 'com.android.tools.build:gradle:" << androidPluginVersion.get().toString() << "'" << newLine;
  527. if (areRemoteNotificationsEnabled())
  528. mo << " classpath 'com.google.gms:google-services:4.0.1'" << newLine;
  529. mo << " }" << newLine;
  530. mo << "}" << newLine;
  531. mo << "" << newLine;
  532. mo << "allprojects {" << newLine;
  533. mo << getAndroidProjectRepositories();
  534. mo << "}" << newLine;
  535. return mo.toString();
  536. }
  537. //==============================================================================
  538. String getAppBuildGradleFileContent (const OwnedArray<LibraryModule>& modules) const
  539. {
  540. MemoryOutputStream mo;
  541. mo.setNewLineString (getNewLineString());
  542. mo << "apply plugin: 'com.android." << (isLibrary() ? "library" : "application") << "'" << newLine << newLine;
  543. mo << "android {" << newLine;
  544. mo << " compileSdkVersion " << static_cast<int> (androidTargetSDK.get()) << newLine;
  545. // CMake 3.22 will fail to build Android projects that set ANDROID_ARM_MODE unless NDK 24+ is used
  546. mo << " ndkVersion \"25.2.9519653\"" << newLine;
  547. mo << " namespace " << project.getBundleIdentifierString().toLowerCase().quoted() << newLine;
  548. mo << " externalNativeBuild {" << newLine;
  549. mo << " cmake {" << newLine;
  550. mo << " path \"CMakeLists.txt\"" << newLine;
  551. mo << " version \"3.22.1\"" << newLine;
  552. mo << " }" << newLine;
  553. mo << " }" << newLine;
  554. mo << getAndroidSigningConfig() << newLine;
  555. mo << getAndroidDefaultConfig() << newLine;
  556. mo << getAndroidBuildTypes() << newLine;
  557. mo << getAndroidProductFlavours() << newLine;
  558. mo << getAndroidVariantFilter() << newLine;
  559. mo << getAndroidJavaSourceSets (modules) << newLine;
  560. mo << getAndroidRepositories() << newLine;
  561. mo << getAndroidDependencies() << newLine;
  562. mo << androidCustomAppBuildGradleContent.get().toString() << newLine;
  563. mo << getApplyPlugins() << newLine;
  564. mo << "}" << newLine << newLine;
  565. return mo.toString();
  566. }
  567. String getAndroidProductFlavours() const
  568. {
  569. MemoryOutputStream mo;
  570. mo.setNewLineString (getNewLineString());
  571. mo << " flavorDimensions \"default\"" << newLine;
  572. mo << " productFlavors {" << newLine;
  573. for (ConstConfigIterator config (*this); config.next();)
  574. {
  575. auto& cfg = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  576. mo << " " << cfg.getProductFlavourNameIdentifier() << " {" << newLine;
  577. if (cfg.getArchitectures().isNotEmpty())
  578. {
  579. mo << " ndk {" << newLine
  580. << " abiFilters " << toGradleList (StringArray::fromTokens (cfg.getArchitectures(), " ", "")) << newLine
  581. << " }" << newLine;
  582. }
  583. mo << " externalNativeBuild {" << newLine
  584. << " cmake {" << newLine;
  585. if (getProject().getProjectType().isStaticLibrary())
  586. mo << " targets \"" << getNativeModuleBinaryName (cfg) << "\"" << newLine;
  587. mo << " cFlags \"-O" << cfg.getGCCOptimisationFlag() << "\"" << newLine
  588. << " cppFlags \"-O" << cfg.getGCCOptimisationFlag() << "\"" << newLine
  589. << " arguments \"-DJUCE_BUILD_CONFIGURATION=" << cfg.getProductFlavourCMakeIdentifier() << "\"" << newLine
  590. << " }" << newLine
  591. << " }" << newLine
  592. << newLine
  593. << " dimension \"default\"" << newLine
  594. << " }" << newLine;
  595. }
  596. mo << " }" << newLine;
  597. return mo.toString();
  598. }
  599. String getAndroidSigningConfig() const
  600. {
  601. MemoryOutputStream mo;
  602. mo.setNewLineString (getNewLineString());
  603. auto keyStoreFilePath = androidKeyStore.get().toString().replace ("${user.home}", "${System.properties['user.home']}")
  604. .replace ("/", "${File.separator}");
  605. mo << " signingConfigs {" << newLine;
  606. mo << " juceSigning {" << newLine;
  607. mo << " storeFile file(\"" << keyStoreFilePath << "\")" << newLine;
  608. mo << " storePassword \"" << androidKeyStorePass.get().toString() << "\"" << newLine;
  609. mo << " keyAlias \"" << androidKeyAlias.get().toString() << "\"" << newLine;
  610. mo << " keyPassword \"" << androidKeyAliasPass.get().toString() << "\"" << newLine;
  611. mo << " storeType \"jks\"" << newLine;
  612. mo << " }" << newLine;
  613. mo << " }" << newLine;
  614. return mo.toString();
  615. }
  616. String getAndroidDefaultConfig() const
  617. {
  618. auto bundleIdentifier = project.getBundleIdentifierString().toLowerCase();
  619. auto cmakeDefs = getCmakeDefinitions();
  620. auto minSdkVersion = static_cast<int> (androidMinimumSDK.get());
  621. auto targetSdkVersion = static_cast<int> (androidTargetSDK.get());
  622. MemoryOutputStream mo;
  623. mo.setNewLineString (getNewLineString());
  624. mo << " defaultConfig {" << newLine;
  625. if (! isLibrary())
  626. mo << " applicationId \"" << bundleIdentifier << "\"" << newLine;
  627. mo << " minSdkVersion " << minSdkVersion << newLine;
  628. mo << " targetSdkVersion " << targetSdkVersion << newLine;
  629. mo << " externalNativeBuild {" << newLine;
  630. mo << " cmake {" << newLine;
  631. mo << " arguments " << cmakeDefs.joinIntoString (", ") << newLine;
  632. mo << " }" << newLine;
  633. mo << " }" << newLine;
  634. mo << " }" << newLine;
  635. return mo.toString();
  636. }
  637. String getAndroidBuildTypes() const
  638. {
  639. MemoryOutputStream mo;
  640. mo.setNewLineString (getNewLineString());
  641. mo << " buildTypes {" << newLine;
  642. int numDebugConfigs = 0;
  643. auto numConfigs = getNumConfigurations();
  644. for (int i = 0; i < numConfigs; ++i)
  645. {
  646. auto config = getConfiguration(i);
  647. if (config->isDebug()) numDebugConfigs++;
  648. if (numDebugConfigs > 1 || ((numConfigs - numDebugConfigs) > 1))
  649. continue;
  650. mo << " " << (config->isDebug() ? "debug" : "release") << " {" << newLine;
  651. mo << " initWith " << (config->isDebug() ? "debug" : "release") << newLine;
  652. mo << " debuggable " << (config->isDebug() ? "true" : "false") << newLine;
  653. mo << " jniDebuggable " << (config->isDebug() ? "true" : "false") << newLine;
  654. mo << " signingConfig signingConfigs.juceSigning" << newLine;
  655. mo << " }" << newLine;
  656. }
  657. mo << " }" << newLine;
  658. return mo.toString();
  659. }
  660. String getAndroidVariantFilter() const
  661. {
  662. MemoryOutputStream mo;
  663. mo.setNewLineString (getNewLineString());
  664. mo << " variantFilter { variant ->" << newLine;
  665. mo << " def names = variant.flavors*.name" << newLine;
  666. for (ConstConfigIterator config (*this); config.next();)
  667. {
  668. auto& cfg = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  669. mo << " if (names.contains (\"" << cfg.getProductFlavourNameIdentifier() << "\")" << newLine;
  670. mo << " && variant.buildType.name != \"" << (cfg.isDebug() ? "debug" : "release") << "\") {" << newLine;
  671. mo << " setIgnore(true)" << newLine;
  672. mo << " }" << newLine;
  673. }
  674. mo << " }" << newLine;
  675. return mo.toString();
  676. }
  677. String getAndroidProjectRepositories() const
  678. {
  679. MemoryOutputStream mo;
  680. mo.setNewLineString (getNewLineString());
  681. auto repositories = StringArray::fromLines (androidProjectRepositories.get().toString());
  682. if (areRemoteNotificationsEnabled())
  683. repositories.add ("maven { url \"https://maven.google.com\" }");
  684. mo << " repositories {" << newLine;
  685. for (auto& r : repositories)
  686. mo << " " << r << newLine;
  687. mo << " }" << newLine;
  688. return mo.toString();
  689. }
  690. String getAndroidRepositories() const
  691. {
  692. MemoryOutputStream mo;
  693. mo.setNewLineString (getNewLineString());
  694. auto repositories = StringArray::fromLines (androidRepositories.get().toString());
  695. mo << " repositories {" << newLine;
  696. for (auto& r : repositories)
  697. mo << " " << r << newLine;
  698. mo << " }" << newLine;
  699. return mo.toString();
  700. }
  701. String getAndroidDependencies() const
  702. {
  703. MemoryOutputStream mo;
  704. mo.setNewLineString (getNewLineString());
  705. mo << " dependencies {" << newLine;
  706. for (auto& d : StringArray::fromLines (androidDependencies.get().toString()))
  707. mo << " " << d << newLine;
  708. for (auto& d : StringArray::fromLines (androidJavaLibs.get().toString()))
  709. mo << " implementation files('libs/" << File (d).getFileName() << "')" << newLine;
  710. if (isInAppBillingEnabled())
  711. mo << " implementation 'com.android.billingclient:billing:5.0.0'" << newLine;
  712. if (areRemoteNotificationsEnabled())
  713. {
  714. mo << " implementation 'com.google.firebase:firebase-core:16.0.1'" << newLine;
  715. mo << " implementation 'com.google.firebase:firebase-messaging:17.6.0'" << newLine;
  716. }
  717. mo << " }" << newLine;
  718. return mo.toString();
  719. }
  720. String getApplyPlugins() const
  721. {
  722. MemoryOutputStream mo;
  723. mo.setNewLineString (getNewLineString());
  724. if (areRemoteNotificationsEnabled())
  725. mo << "apply plugin: 'com.google.gms.google-services'" << newLine;
  726. return mo.toString();
  727. }
  728. void addModuleJavaFolderToSourceSet(StringArray& javaSourceSets, const File& source) const
  729. {
  730. if (source.isDirectory())
  731. {
  732. auto appFolder = getTargetFolder().getChildFile ("app");
  733. build_tools::RelativePath relativePath (source, appFolder, build_tools::RelativePath::buildTargetFolder);
  734. javaSourceSets.add (relativePath.toUnixStyle());
  735. }
  736. }
  737. void addOptJavaFolderToSourceSetsForModule (StringArray& javaSourceSets,
  738. const OwnedArray<LibraryModule>& modules,
  739. const String& moduleID) const
  740. {
  741. for (auto& m : modules)
  742. {
  743. if (m->getID() == moduleID)
  744. {
  745. auto javaFolder = m->getFolder().getChildFile ("native").getChildFile ("javaopt");
  746. addModuleJavaFolderToSourceSet (javaSourceSets, javaFolder.getChildFile ("app"));
  747. return;
  748. }
  749. }
  750. }
  751. String getAndroidJavaSourceSets (const OwnedArray<LibraryModule>& modules) const
  752. {
  753. auto javaSourceSets = getSourceSetArrayFor (androidAdditionalJavaFolders.get().toString());
  754. auto resourceSets = getSourceSetArrayFor (androidAdditionalResourceFolders.get().toString());
  755. for (auto* module : modules)
  756. {
  757. auto javaFolder = module->getFolder().getChildFile ("native").getChildFile ("javacore");
  758. addModuleJavaFolderToSourceSet (javaSourceSets, javaFolder.getChildFile ("init"));
  759. if (! isLibrary())
  760. addModuleJavaFolderToSourceSet (javaSourceSets, javaFolder.getChildFile ("app"));
  761. }
  762. if (isUsingDefaultActivityClass() || isContentSharingEnabled())
  763. addOptJavaFolderToSourceSetsForModule (javaSourceSets, modules, "juce_gui_basics");
  764. if (areRemoteNotificationsEnabled())
  765. addOptJavaFolderToSourceSetsForModule (javaSourceSets, modules, "juce_gui_extra");
  766. if (isInAppBillingEnabled())
  767. addOptJavaFolderToSourceSetsForModule (javaSourceSets, modules, "juce_product_unlocking");
  768. MemoryOutputStream mo;
  769. mo.setNewLineString (getNewLineString());
  770. mo << " sourceSets {" << newLine;
  771. mo << getSourceSetStringFor ("main.java.srcDirs", javaSourceSets, getNewLineString());
  772. mo << newLine;
  773. mo << getSourceSetStringFor ("main.res.srcDirs", resourceSets, getNewLineString());
  774. mo << " }" << newLine;
  775. return mo.toString();
  776. }
  777. StringArray getSourceSetArrayFor (const String& srcDirs) const
  778. {
  779. StringArray sourceSets;
  780. for (auto folder : StringArray::fromLines (srcDirs))
  781. {
  782. if (File::isAbsolutePath (folder))
  783. {
  784. sourceSets.add (folder);
  785. }
  786. else
  787. {
  788. auto appFolder = getTargetFolder().getChildFile ("app");
  789. auto relativePath = build_tools::RelativePath (folder, build_tools::RelativePath::projectFolder)
  790. .rebased (getProject().getProjectFolder(), appFolder,
  791. build_tools::RelativePath::buildTargetFolder);
  792. sourceSets.add (relativePath.toUnixStyle());
  793. }
  794. }
  795. return sourceSets;
  796. }
  797. static String getSourceSetStringFor (const String& type, const StringArray& srcDirs, const String& newLineString)
  798. {
  799. String s;
  800. s << " " << type << " +=" << newLine;
  801. s << " [";
  802. bool isFirst = true;
  803. for (auto sourceSet : srcDirs)
  804. {
  805. if (! isFirst)
  806. s << "," << newLine << " ";
  807. isFirst = false;
  808. s << "\"" << sourceSet << "\"";
  809. }
  810. s << "]" << newLine;
  811. return replaceLineFeeds (s, newLineString);
  812. }
  813. //==============================================================================
  814. String getLocalPropertiesFileContent() const
  815. {
  816. String props;
  817. props << "sdk.dir=" << sanitisePath (getAppSettings().getStoredPath (Ids::androidSDKPath, TargetOS::getThisOS()).get().toString()) << newLine;
  818. return replaceLineFeeds (props, getNewLineString());
  819. }
  820. String getGradleWrapperPropertiesFileContent() const
  821. {
  822. String props;
  823. props << "distributionUrl=https\\://services.gradle.org/distributions/gradle-"
  824. << gradleVersion.get().toString() << "-all.zip";
  825. return props;
  826. }
  827. //==============================================================================
  828. void createBaseExporterProperties (PropertyListBuilder& props)
  829. {
  830. props.add (new TextPropertyComponent (androidAdditionalJavaFolders, "Java Source code folders", 32768, true),
  831. "Folders inside which additional java source files can be found (one per line). For example, if you "
  832. "are using your own Activity you should place the java files for this into a folder and add the folder "
  833. "path to this field.");
  834. props.add (new TextPropertyComponent (androidAdditionalResourceFolders, "Resource folders", 32768, true),
  835. "Folders inside which additional resource files can be found (one per line). For example, if you "
  836. "want to add your own layout xml files then you should place a layout xml file inside a folder and add "
  837. "the folder path to this field.");
  838. props.add (new TextPropertyComponent (androidJavaLibs, "Java libraries to include", 32768, true),
  839. "Java libs (JAR files) (one per line). These will be copied to app/libs folder and \"implementation files\" "
  840. "dependency will be automatically added to module \"dependencies\" section for each library, so do "
  841. "not add the dependency yourself.");
  842. props.add (new TextPropertyComponent (androidProjectRepositories, "Project Repositories", 32768, true),
  843. "Custom project repositories (one per line). These will be used in project-level gradle file "
  844. "\"allprojects { repositories {\" section instead of default ones.");
  845. props.add (new TextPropertyComponent (androidRepositories, "Module Repositories", 32768, true),
  846. "Module repositories (one per line). These will be added to module-level gradle file repositories section. ");
  847. props.add (new TextPropertyComponent (androidDependencies, "Module Dependencies", 32768, true),
  848. "Module dependencies (one per line). These will be added to module-level gradle file \"dependencies\" section. "
  849. "If adding any java libs in \"Java libraries to include\" setting, do not add them here as "
  850. "they will be added automatically.");
  851. props.add (new TextPropertyComponent (androidCustomAppBuildGradleContent, "Extra module's build.gradle content", 32768, true),
  852. "Additional content to be appended to module's build.gradle inside android { section. ");
  853. props.add (new TextPropertyComponent (androidGradleSettingsContent, "Custom gradle.settings content", 32768, true),
  854. "You can customize the content of settings.gradle here");
  855. props.add (new ChoicePropertyComponent (androidScreenOrientation, "Screen Orientation",
  856. { "Portrait and Landscape", "Portrait", "Landscape" },
  857. { "unspecified", "portrait", "landscape" }),
  858. "The screen orientations that this app should support");
  859. props.add (new TextPropertyComponent (androidCustomActivityClass, "Custom Android Activity", 256, false),
  860. "If not empty, specifies the Android Activity class name stored in the app's manifest which "
  861. "should be used instead of Android's default Activity. If you specify a custom Activity "
  862. "then you should implement onNewIntent() function like the one in com.rmsl.juce.JuceActivity, if "
  863. "you wish to be able to handle push notification events.");
  864. props.add (new TextPropertyComponent (androidCustomApplicationClass, "Custom Android Application", 256, false),
  865. "If not empty, specifies the Android Application class name stored in the app's manifest which "
  866. "should be used instead of JUCE's default JuceApp class. If you specify a custom App then you must "
  867. "call com.rmsl.juce.Java.initialiseJUCE somewhere in your code before calling any JUCE functions.");
  868. props.add (new TextPropertyComponent (androidVersionCode, "Android Version Code", 32, false),
  869. "An integer value that represents the version of the application code, relative to other versions.");
  870. props.add (new TextPropertyComponent (androidMinimumSDK, "Minimum SDK Version", 32, false),
  871. "The number of the minimum version of the Android SDK that the app requires (must be 16 or higher).");
  872. props.add (new TextPropertyComponent (androidTargetSDK, "Target SDK Version", 32, false),
  873. "The number of the version of the Android SDK that the app is targeting.");
  874. props.add (new TextPropertyComponent (androidExtraAssetsFolder, "Extra Android Assets", 256, false),
  875. "A path to a folder (relative to the project folder) which contains extra android assets.");
  876. }
  877. //==============================================================================
  878. void createManifestExporterProperties (PropertyListBuilder& props)
  879. {
  880. props.add (new TextPropertyComponent (androidOboeRepositoryPath, "Custom Oboe Repository", 2048, false),
  881. "Path to the root of Oboe repository. This path can be absolute, or relative to the build directory. "
  882. "Make sure to point Oboe repository to commit with SHA c5c3cc17f78974bf005bf33a2de1a093ac55cc07 before building. "
  883. "Leave blank to use the version of Oboe distributed with JUCE.");
  884. props.add (new ChoicePropertyComponent (androidInternetNeeded, "Internet Access"),
  885. "If enabled, this will set the android.permission.INTERNET flag in the manifest.");
  886. props.add (new ChoicePropertyComponent (androidMicNeeded, "Audio Input Required"),
  887. "If enabled, this will set the android.permission.RECORD_AUDIO flag in the manifest.");
  888. props.add (new ChoicePropertyComponent (androidCameraNeeded, "Camera Required"),
  889. "If enabled, this will set the android.permission.CAMERA flag in the manifest.");
  890. props.add (new ChoicePropertyComponent (androidBluetoothScanNeeded, "Bluetooth Scan Required"),
  891. "If enabled, this will set the android.permission.BLUETOOTH_SCAN, android.permission.BLUETOOTH and android.permission.BLUETOOTH_ADMIN flags in the manifest. This is required for Bluetooth MIDI on Android.");
  892. props.add (new ChoicePropertyComponent (androidBluetoothAdvertiseNeeded, "Bluetooth Advertise Required"),
  893. "If enabled, this will set the android.permission.BLUETOOTH_ADVERTISE, android.permission.BLUETOOTH and android.permission.BLUETOOTH_ADMIN flags in the manifest.");
  894. props.add (new ChoicePropertyComponent (androidBluetoothConnectNeeded, "Bluetooth Connect Required"),
  895. "If enabled, this will set the android.permission.BLUETOOTH_CONNECT, android.permission.BLUETOOTH and android.permission.BLUETOOTH_ADMIN flags in the manifest. This is required for Bluetooth MIDI on Android.");
  896. props.add (new ChoicePropertyComponent (androidReadMediaAudioPermission, "Read Audio From External Storage"),
  897. "If enabled, this will set the android.permission.READ_MEDIA_AUDIO and android.permission.READ_EXTERNAL_STORAGE flags in the manifest.");
  898. props.add (new ChoicePropertyComponent (androidReadMediaImagesPermission, "Read Images From External Storage"),
  899. "If enabled, this will set the android.permission.READ_MEDIA_IMAGES and android.permission.READ_EXTERNAL_STORAGE flags in the manifest.");
  900. props.add (new ChoicePropertyComponent (androidReadMediaVideoPermission, "Read Video From External Storage"),
  901. "If enabled, this will set the android.permission.READ_MEDIA_VIDEO and android.permission.READ_EXTERNAL_STORAGE flags in the manifest.");
  902. props.add (new ChoicePropertyComponent (androidExternalWritePermission, "Write to External Storage"),
  903. "If enabled, this will set the android.permission.WRITE_EXTERNAL_STORAGE flag in the manifest.");
  904. props.add (new ChoicePropertyComponent (androidInAppBillingPermission, "In-App Billing"),
  905. "If enabled, this will set the com.android.vending.BILLING flag in the manifest.");
  906. props.add (new ChoicePropertyComponent (androidVibratePermission, "Vibrate"),
  907. "If enabled, this will set the android.permission.VIBRATE flag in the manifest.");
  908. props.add (new ChoicePropertyComponent (androidEnableContentSharing, "Content Sharing"),
  909. "If enabled, your app will be able to share content with other apps.");
  910. props.add (new TextPropertyComponent (androidOtherPermissions, "Custom Permissions", 2048, false),
  911. "A space-separated list of other permission flags that should be added to the manifest.");
  912. props.add (new ChoicePropertyComponent (androidPushNotifications, "Push Notifications Capability"),
  913. "Enable this to grant your app the capability to receive push notifications.");
  914. props.add (new ChoicePropertyComponentWithEnablement (androidEnableRemoteNotifications, androidPushNotifications, "Remote Notifications"),
  915. "Enable to be able to send remote notifications to devices running your app (min API level 14). Enable the \"Push Notifications Capability\" "
  916. "setting, provide Remote Notifications Config File, configure your app in Firebase Console and ensure you have the latest Google Repository "
  917. "in Android Studio's SDK Manager.");
  918. props.add (new TextPropertyComponent (androidRemoteNotificationsConfigFile.getPropertyAsValue(), "Remote Notifications Config File", 2048, false),
  919. "Path to google-services.json file. This will be the file provided by Firebase when creating a new app in Firebase console.");
  920. props.add (new TextPropertyComponent (androidManifestCustomXmlElements, "Custom Manifest XML Content", 8192, true),
  921. "You can specify custom AndroidManifest.xml content overriding the default one generated by Projucer. "
  922. "Projucer will automatically create any missing and required XML elements and attributes "
  923. "and merge them into your custom content.");
  924. }
  925. //==============================================================================
  926. void createCodeSigningExporterProperties (PropertyListBuilder& props)
  927. {
  928. props.add (new TextPropertyComponent (androidKeyStore, "Key Signing: key.store", 2048, false),
  929. "The key.store value, used when signing the release package.");
  930. props.add (new TextPropertyComponent (androidKeyStorePass, "Key Signing: key.store.password", 2048, false),
  931. "The key.store password, used when signing the release package.");
  932. props.add (new TextPropertyComponent (androidKeyAlias, "Key Signing: key.alias", 2048, false),
  933. "The key.alias value, used when signing the release package.");
  934. props.add (new TextPropertyComponent (androidKeyAliasPass, "Key Signing: key.alias.password", 2048, false),
  935. "The key.alias password, used when signing the release package.");
  936. }
  937. //==============================================================================
  938. void createOtherExporterProperties (PropertyListBuilder& props)
  939. {
  940. props.add (new TextPropertyComponent (androidTheme, "Android Theme", 256, false),
  941. "E.g. @android:style/Theme.NoTitleBar or leave blank for default");
  942. }
  943. //==============================================================================
  944. void copyAdditionalJavaLibs (const File& targetFolder) const
  945. {
  946. auto libFolder = targetFolder.getChildFile ("libs");
  947. libFolder.createDirectory();
  948. auto libPaths = StringArray::fromLines (androidJavaLibs.get().toString());
  949. for (auto& p : libPaths)
  950. {
  951. auto f = getTargetFolder().getChildFile (p);
  952. // Is the path to the java lib correct?
  953. jassert (f.existsAsFile());
  954. f.copyFileTo (libFolder.getChildFile (f.getFileName()));
  955. }
  956. }
  957. void copyExtraResourceFiles() const
  958. {
  959. for (ConstConfigIterator config (*this); config.next();)
  960. {
  961. auto& cfg = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  962. String cfgPath = cfg.isDebug() ? "app/src/debug" : "app/src/release";
  963. String xmlValuesPath = cfg.isDebug() ? "app/src/debug/res/values" : "app/src/release/res/values";
  964. String drawablesPath = cfg.isDebug() ? "app/src/debug/res" : "app/src/release/res";
  965. String rawPath = cfg.isDebug() ? "app/src/debug/res/raw" : "app/src/release/res/raw";
  966. copyExtraResourceFiles (cfg.getAdditionalXmlResources(), xmlValuesPath);
  967. copyExtraResourceFiles (cfg.getAdditionalDrawableResources(), drawablesPath);
  968. copyExtraResourceFiles (cfg.getAdditionalRawResources(), rawPath);
  969. if (areRemoteNotificationsEnabled())
  970. {
  971. auto remoteNotifsConfigFilePath = cfg.getRemoteNotifsConfigFile();
  972. if (remoteNotifsConfigFilePath.isEmpty())
  973. remoteNotifsConfigFilePath = androidRemoteNotificationsConfigFile.get().toString();
  974. File file (getProject().getFile().getChildFile (remoteNotifsConfigFilePath));
  975. // Settings file must be present for remote notifications to work and it must be called google-services.json.
  976. jassert (file.existsAsFile() && file.getFileName() == "google-services.json");
  977. copyExtraResourceFiles (remoteNotifsConfigFilePath, cfgPath);
  978. }
  979. }
  980. }
  981. void copyExtraResourceFiles (const String& resources, const String& dstRelativePath) const
  982. {
  983. auto resourcePaths = StringArray::fromTokens (resources, true);
  984. auto parentFolder = getTargetFolder().getChildFile (dstRelativePath);
  985. parentFolder.createDirectory();
  986. for (auto& path : resourcePaths)
  987. {
  988. auto file = getProject().getFile().getChildFile (path);
  989. jassert (file.exists());
  990. if (file.exists())
  991. file.copyFileTo (parentFolder.getChildFile (file.getFileName()));
  992. }
  993. }
  994. //==============================================================================
  995. String getActivityClassString() const
  996. {
  997. auto customActivityClass = androidCustomActivityClass.get().toString();
  998. if (customActivityClass.isNotEmpty())
  999. return customActivityClass;
  1000. return arePushNotificationsEnabled() ? getDefaultActivityClass() : "android.app.Activity";
  1001. }
  1002. String getApplicationClassString() const { return androidCustomApplicationClass.get(); }
  1003. String getJNIActivityClassName() const { return getActivityClassString().replaceCharacter ('.', '/'); }
  1004. bool isUsingDefaultActivityClass() const { return getActivityClassString() == getDefaultActivityClass(); }
  1005. //==============================================================================
  1006. bool arePushNotificationsEnabled() const
  1007. {
  1008. return project.getEnabledModules().isModuleEnabled ("juce_gui_extra")
  1009. && androidPushNotifications.get();
  1010. }
  1011. bool areRemoteNotificationsEnabled() const
  1012. {
  1013. return arePushNotificationsEnabled()
  1014. && androidEnableRemoteNotifications.get();
  1015. }
  1016. bool isInAppBillingEnabled() const
  1017. {
  1018. return project.getEnabledModules().isModuleEnabled ("juce_product_unlocking")
  1019. && androidInAppBillingPermission.get();
  1020. }
  1021. bool isContentSharingEnabled() const
  1022. {
  1023. return project.getEnabledModules().isModuleEnabled ("juce_gui_basics")
  1024. && androidEnableContentSharing.get();
  1025. }
  1026. //==============================================================================
  1027. String getNativeModuleBinaryName (const AndroidBuildConfiguration& config) const
  1028. {
  1029. return (isLibrary() ? File::createLegalFileName (config.getTargetBinaryNameString().trim()) : "juce_jni");
  1030. }
  1031. String getAppPlatform() const
  1032. {
  1033. return "android-" + androidMinimumSDK.get().toString();
  1034. }
  1035. static String escapeQuotes (const String& str)
  1036. {
  1037. return str.replace ("'", "\\'").replace ("\"", "\\\"");
  1038. }
  1039. //==============================================================================
  1040. void writeStringsXML (const File& folder) const
  1041. {
  1042. for (ConstConfigIterator config (*this); config.next();)
  1043. {
  1044. auto& cfg = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  1045. String customStringsXmlContent ("<resources>\n");
  1046. customStringsXmlContent << "<string name=\"app_name\">" << escapeQuotes (projectName) << "</string>\n";
  1047. customStringsXmlContent << cfg.getCustomStringsXml();
  1048. customStringsXmlContent << "\n</resources>";
  1049. if (auto strings = parseXML (customStringsXmlContent))
  1050. {
  1051. String dir = cfg.isDebug() ? "debug" : "release";
  1052. String subPath = "app/src/" + dir + "/res/values/string.xml";
  1053. writeXmlOrThrow (*strings, folder.getChildFile (subPath), "utf-8", 100, true);
  1054. }
  1055. else
  1056. {
  1057. jassertfalse; // needs handling?
  1058. }
  1059. }
  1060. }
  1061. void writeAndroidManifest (const File& folder) const
  1062. {
  1063. std::unique_ptr<XmlElement> manifest (createManifestXML());
  1064. writeXmlOrThrow (*manifest, folder.getChildFile ("src/main/AndroidManifest.xml"), "utf-8", 100, true);
  1065. }
  1066. void writeIcon (const File& file, const Image& im) const
  1067. {
  1068. if (im.isValid())
  1069. {
  1070. createDirectoryOrThrow (file.getParentDirectory());
  1071. build_tools::writeStreamToFile (file, [&] (MemoryOutputStream& mo)
  1072. {
  1073. mo.setNewLineString (getNewLineString());
  1074. PNGImageFormat png;
  1075. if (! png.writeImageToStream (im, mo))
  1076. throw build_tools::SaveError ("Can't generate Android icon file");
  1077. });
  1078. }
  1079. }
  1080. void writeIcons (const File& folder) const
  1081. {
  1082. const auto icons = getIcons();
  1083. if (icons.big != nullptr && icons.small != nullptr)
  1084. {
  1085. auto step = jmax (icons.big->getWidth(), icons.big->getHeight()) / 8;
  1086. writeIcon (folder.getChildFile ("drawable-xhdpi/icon.png"), build_tools::getBestIconForSize (icons, step * 8, false));
  1087. writeIcon (folder.getChildFile ("drawable-hdpi/icon.png"), build_tools::getBestIconForSize (icons, step * 6, false));
  1088. writeIcon (folder.getChildFile ("drawable-mdpi/icon.png"), build_tools::getBestIconForSize (icons, step * 4, false));
  1089. writeIcon (folder.getChildFile ("drawable-ldpi/icon.png"), build_tools::getBestIconForSize (icons, step * 3, false));
  1090. }
  1091. else if (auto* icon = (icons.big != nullptr ? icons.big.get() : icons.small.get()))
  1092. {
  1093. writeIcon (folder.getChildFile ("drawable-mdpi/icon.png"), build_tools::rescaleImageForIcon (*icon, icon->getWidth()));
  1094. }
  1095. }
  1096. void writeAppIcons (const File& folder) const
  1097. {
  1098. writeIcons (folder.getChildFile ("app/src/main/res/"));
  1099. }
  1100. static String sanitisePath (String path)
  1101. {
  1102. return expandHomeFolderToken (path).replace ("\\", "\\\\");
  1103. }
  1104. static String expandHomeFolderToken (const String& path)
  1105. {
  1106. auto homeFolder = File::getSpecialLocation (File::userHomeDirectory).getFullPathName();
  1107. return path.replace ("${user.home}", homeFolder)
  1108. .replace ("~", homeFolder);
  1109. }
  1110. //==============================================================================
  1111. void addCompileUnits (const Project::Item& projectItem,
  1112. MemoryOutputStream& mo,
  1113. Array<build_tools::RelativePath>& excludeFromBuild,
  1114. Array<std::pair<build_tools::RelativePath, String>>& extraCompilerFlags) const
  1115. {
  1116. if (projectItem.isGroup())
  1117. {
  1118. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  1119. addCompileUnits (projectItem.getChild (i), mo, excludeFromBuild, extraCompilerFlags);
  1120. }
  1121. else if (projectItem.shouldBeAddedToTargetProject() && projectItem.shouldBeAddedToTargetExporter (*this))
  1122. {
  1123. auto f = projectItem.getFile();
  1124. build_tools::RelativePath file (f, getTargetFolder().getChildFile ("app"), build_tools::RelativePath::buildTargetFolder);
  1125. auto targetType = getProject().getTargetTypeFromFilePath (f, true);
  1126. mo << " \"" << file.toUnixStyle() << "\"" << newLine;
  1127. if ((! projectItem.shouldBeCompiled()) || (! shouldFileBeCompiledByDefault (f))
  1128. || (getProject().isAudioPluginProject()
  1129. && targetType != build_tools::ProjectType::Target::SharedCodeTarget
  1130. && targetType != build_tools::ProjectType::Target::StandalonePlugIn))
  1131. {
  1132. excludeFromBuild.add (file);
  1133. }
  1134. else
  1135. {
  1136. auto extraFlags = getCompilerFlagsForProjectItem (projectItem);
  1137. if (extraFlags.isNotEmpty())
  1138. extraCompilerFlags.add ({ file, extraFlags });
  1139. }
  1140. }
  1141. }
  1142. void addCompileUnits (MemoryOutputStream& mo,
  1143. Array<build_tools::RelativePath>& excludeFromBuild,
  1144. Array<std::pair<build_tools::RelativePath, String>>& extraCompilerFlags) const
  1145. {
  1146. for (int i = 0; i < getAllGroups().size(); ++i)
  1147. addCompileUnits (getAllGroups().getReference(i), mo, excludeFromBuild, extraCompilerFlags);
  1148. }
  1149. //==============================================================================
  1150. StringArray getCmakeDefinitions() const
  1151. {
  1152. auto toolchain = gradleToolchain.get().toString();
  1153. bool isClang = (toolchain == "clang");
  1154. StringArray cmakeArgs;
  1155. cmakeArgs.add ("\"-DANDROID_TOOLCHAIN=" + toolchain + "\"");
  1156. cmakeArgs.add ("\"-DANDROID_PLATFORM=" + getAppPlatform() + "\"");
  1157. cmakeArgs.add (String ("\"-DANDROID_STL=") + (isClang ? "c++_static" : "gnustl_static") + "\"");
  1158. cmakeArgs.add ("\"-DANDROID_CPP_FEATURES=exceptions rtti\"");
  1159. cmakeArgs.add ("\"-DANDROID_ARM_MODE=arm\"");
  1160. cmakeArgs.add ("\"-DANDROID_ARM_NEON=TRUE\"");
  1161. auto cppStandard = [this]
  1162. {
  1163. auto projectStandard = project.getCppStandardString();
  1164. if (projectStandard == "latest")
  1165. return project.getLatestNumberedCppStandardString();
  1166. return projectStandard;
  1167. }();
  1168. cmakeArgs.add ("\"-DCMAKE_CXX_STANDARD=" + cppStandard + "\"");
  1169. cmakeArgs.add ("\"-DCMAKE_CXX_EXTENSIONS=" + String (shouldUseGNUExtensions() ? "ON" : "OFF") + "\"");
  1170. return cmakeArgs;
  1171. }
  1172. //==============================================================================
  1173. StringArray getAndroidCompilerFlags() const
  1174. {
  1175. StringArray cFlags;
  1176. cFlags.add ("\"-fsigned-char\"");
  1177. return cFlags;
  1178. }
  1179. StringArray getConfigCompilerFlags (const BuildConfiguration& config) const
  1180. {
  1181. auto cFlags = getAndroidCompilerFlags();
  1182. cFlags.addArray (getEscapedFlags (StringArray::fromTokens (config.getAllCompilerFlagsString(), true)));
  1183. return cFlags;
  1184. }
  1185. //==============================================================================
  1186. StringPairArray getAndroidPreprocessorDefs() const
  1187. {
  1188. StringPairArray defines;
  1189. defines.set ("JUCE_ANDROID", "1");
  1190. defines.set ("JUCE_ANDROID_API_VERSION", androidMinimumSDK.get());
  1191. if (arePushNotificationsEnabled())
  1192. {
  1193. defines.set ("JUCE_PUSH_NOTIFICATIONS", "1");
  1194. defines.set ("JUCE_PUSH_NOTIFICATIONS_ACTIVITY", getJNIActivityClassName().quoted());
  1195. }
  1196. if (isInAppBillingEnabled())
  1197. defines.set ("JUCE_IN_APP_PURCHASES", "1");
  1198. if (isContentSharingEnabled())
  1199. defines.set ("JUCE_CONTENT_SHARING", "1");
  1200. if (supportsGLv3())
  1201. defines.set ("JUCE_ANDROID_GL_ES_VERSION_3_0", "1");
  1202. if (areRemoteNotificationsEnabled())
  1203. {
  1204. defines.set ("JUCE_FIREBASE_INSTANCE_ID_SERVICE_CLASSNAME", "com_rmsl_juce_JuceFirebaseInstanceIdService");
  1205. defines.set ("JUCE_FIREBASE_MESSAGING_SERVICE_CLASSNAME", "com_rmsl_juce_JuceFirebaseMessagingService");
  1206. }
  1207. return defines;
  1208. }
  1209. StringPairArray getProjectPreprocessorDefs() const
  1210. {
  1211. auto defines = getAndroidPreprocessorDefs();
  1212. return mergePreprocessorDefs (defines, getAllPreprocessorDefs());
  1213. }
  1214. StringPairArray getConfigPreprocessorDefs (const BuildConfiguration& config) const
  1215. {
  1216. auto cfgDefines = getAllPreprocessorDefs (config, build_tools::ProjectType::Target::unspecified);
  1217. if (config.isDebug())
  1218. {
  1219. cfgDefines.set ("DEBUG", "1");
  1220. cfgDefines.set ("_DEBUG", "1");
  1221. }
  1222. else
  1223. {
  1224. cfgDefines.set ("NDEBUG", "1");
  1225. }
  1226. return cfgDefines;
  1227. }
  1228. //==============================================================================
  1229. StringArray getUserLibraries() const
  1230. {
  1231. auto userLibraries = StringArray::fromTokens (getExternalLibrariesString(), ";", "");
  1232. userLibraries = getCleanedStringArray (userLibraries);
  1233. const auto ppDefs = getAllPreprocessorDefs();
  1234. for (auto& lib : userLibraries)
  1235. lib = build_tools::replacePreprocessorDefs (ppDefs, lib);
  1236. userLibraries.addArray (androidLibs);
  1237. return userLibraries;
  1238. }
  1239. StringArray getAndroidLibraries() const
  1240. {
  1241. StringArray libraries;
  1242. libraries.add ("log");
  1243. libraries.add ("android");
  1244. libraries.add (supportsGLv3() ? "GLESv3" : "GLESv2");
  1245. libraries.add ("EGL");
  1246. return libraries;
  1247. }
  1248. //==============================================================================
  1249. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  1250. {
  1251. auto paths = extraSearchPaths;
  1252. paths.addArray (config.getHeaderSearchPaths());
  1253. paths = getCleanedStringArray (paths);
  1254. return paths;
  1255. }
  1256. //==============================================================================
  1257. String escapeDirectoryForCmake (const String& path) const
  1258. {
  1259. auto relative =
  1260. build_tools::RelativePath (path, build_tools::RelativePath::buildTargetFolder)
  1261. .rebased (getTargetFolder(), getTargetFolder().getChildFile ("app"), build_tools::RelativePath::buildTargetFolder);
  1262. return relative.toUnixStyle();
  1263. }
  1264. void writeCmakePathLines (MemoryOutputStream& mo, const String& prefix, const String& firstLine, const StringArray& paths,
  1265. const String& suffix = ")") const
  1266. {
  1267. if (paths.size() > 0)
  1268. {
  1269. mo << prefix << firstLine << newLine;
  1270. for (auto& path : paths)
  1271. mo << prefix << " \"" << escapeDirectoryForCmake (path) << "\"" << newLine;
  1272. mo << prefix << suffix << newLine << newLine;
  1273. }
  1274. }
  1275. static StringArray getEscapedPreprocessorDefs (const StringPairArray& defs)
  1276. {
  1277. StringArray escapedDefs;
  1278. for (int i = 0; i < defs.size(); ++i)
  1279. {
  1280. auto escaped = "[[-D" + defs.getAllKeys()[i];
  1281. auto value = defs.getAllValues()[i];
  1282. if (value.isNotEmpty())
  1283. escaped += ("=" + value);
  1284. escapedDefs.add (escaped + "]]");
  1285. }
  1286. return escapedDefs;
  1287. }
  1288. static StringArray getEscapedFlags (const StringArray& flags)
  1289. {
  1290. StringArray escaped;
  1291. for (auto& flag : flags)
  1292. escaped.add ("[[" + flag + "]]");
  1293. return escaped;
  1294. }
  1295. //==============================================================================
  1296. std::unique_ptr<XmlElement> createManifestXML() const
  1297. {
  1298. auto manifest = createManifestElement();
  1299. createSupportsScreensElement (*manifest);
  1300. createPermissionElements (*manifest);
  1301. createOpenGlFeatureElement (*manifest);
  1302. if (! isLibrary())
  1303. {
  1304. auto* app = createApplicationElement (*manifest);
  1305. auto* receiver = getOrCreateChildWithName (*app, "receiver");
  1306. setAttributeIfNotPresent (*receiver, "android:name", "com.rmsl.juce.Receiver");
  1307. setAttributeIfNotPresent (*receiver, "android:exported", "false");
  1308. auto* act = createActivityElement (*app);
  1309. createIntentElement (*act);
  1310. createServiceElements (*app);
  1311. createProviderElement (*app);
  1312. }
  1313. return manifest;
  1314. }
  1315. std::unique_ptr<XmlElement> createManifestElement() const
  1316. {
  1317. auto manifest = parseXML (androidManifestCustomXmlElements.get());
  1318. if (manifest == nullptr)
  1319. manifest = std::make_unique<XmlElement> ("manifest");
  1320. setAttributeIfNotPresent (*manifest, "xmlns:android", "http://schemas.android.com/apk/res/android");
  1321. setAttributeIfNotPresent (*manifest, "android:versionCode", androidVersionCode.get());
  1322. setAttributeIfNotPresent (*manifest, "android:versionName", project.getVersionString());
  1323. return manifest;
  1324. }
  1325. void createSupportsScreensElement (XmlElement& manifest) const
  1326. {
  1327. if (! isLibrary())
  1328. {
  1329. if (manifest.getChildByName ("supports-screens") == nullptr)
  1330. {
  1331. auto* screens = manifest.createNewChildElement ("supports-screens");
  1332. screens->setAttribute ("android:smallScreens", "true");
  1333. screens->setAttribute ("android:normalScreens", "true");
  1334. screens->setAttribute ("android:largeScreens", "true");
  1335. screens->setAttribute ("android:anyDensity", "true");
  1336. screens->setAttribute ("android:xlargeScreens", "true");
  1337. }
  1338. }
  1339. }
  1340. void createPermissionElements (XmlElement& manifest) const
  1341. {
  1342. auto permissions = getPermissionsRequired();
  1343. for (auto* child : manifest.getChildWithTagNameIterator ("uses-permission"))
  1344. {
  1345. permissions.removeString (child->getStringAttribute ("android:name"), false);
  1346. }
  1347. for (int i = permissions.size(); --i >= 0;)
  1348. {
  1349. const auto permission = permissions[i];
  1350. auto* usesPermission = manifest.createNewChildElement ("uses-permission");
  1351. usesPermission->setAttribute ("android:name", permission);
  1352. // This permission only has an effect on SDK version 28 and lower
  1353. if (permission == "android.permission.WRITE_EXTERNAL_STORAGE")
  1354. usesPermission->setAttribute ("android:maxSdkVersion", "28");
  1355. // https://developer.android.com/training/data-storage/shared/documents-files
  1356. // If the SDK version is <= 28, READ_EXTERNAL_STORAGE is required to access any
  1357. // media file, including files created by the current app.
  1358. // If the SDK version is <= 32, READ_EXTERNAL_STORAGE is required to access other
  1359. // apps' media files.
  1360. // This permission has no effect on later Android versions.
  1361. if (permission == "android.permission.READ_EXTERNAL_STORAGE")
  1362. usesPermission->setAttribute ("android:maxSdkVersion", "32");
  1363. if (permission == "android.permission.BLUETOOTH_SCAN")
  1364. usesPermission->setAttribute ("android:usesPermissionFlags", "neverForLocation");
  1365. // These permissions are obsoleted by new more fine-grained permissions in API level 31
  1366. if (permission == "android.permission.BLUETOOTH"
  1367. || permission == "android.permission.BLUETOOTH_ADMIN"
  1368. || permission == "android.permission.ACCESS_FINE_LOCATION"
  1369. || permission == "android.permission.ACCESS_COARSE_LOCATION")
  1370. {
  1371. usesPermission->setAttribute ("android:maxSdkVersion", "30");
  1372. }
  1373. }
  1374. }
  1375. void createOpenGlFeatureElement (XmlElement& manifest) const
  1376. {
  1377. if (project.getEnabledModules().isModuleEnabled ("juce_opengl"))
  1378. {
  1379. XmlElement* glVersion = nullptr;
  1380. for (auto* child : manifest.getChildWithTagNameIterator ("uses-feature"))
  1381. {
  1382. if (child->getStringAttribute ("android:glEsVersion").isNotEmpty())
  1383. {
  1384. glVersion = child;
  1385. break;
  1386. }
  1387. }
  1388. if (glVersion == nullptr)
  1389. glVersion = manifest.createNewChildElement ("uses-feature");
  1390. setAttributeIfNotPresent (*glVersion, "android:glEsVersion", (static_cast<int> (androidMinimumSDK.get()) >= 18 ? "0x00030000" : "0x00020000"));
  1391. setAttributeIfNotPresent (*glVersion, "android:required", "true");
  1392. }
  1393. }
  1394. XmlElement* createApplicationElement (XmlElement& manifest) const
  1395. {
  1396. auto* app = getOrCreateChildWithName (manifest, "application");
  1397. setAttributeIfNotPresent (*app, "android:label", "@string/app_name");
  1398. setAttributeIfNotPresent (*app, "android:name", getApplicationClassString());
  1399. if (androidTheme.get().toString().isNotEmpty())
  1400. setAttributeIfNotPresent (*app, "android:theme", androidTheme.get());
  1401. if (! app->hasAttribute ("android:icon"))
  1402. {
  1403. std::unique_ptr<Drawable> bigIcon (getBigIcon()), smallIcon (getSmallIcon());
  1404. if (bigIcon != nullptr || smallIcon != nullptr)
  1405. app->setAttribute ("android:icon", "@drawable/icon");
  1406. }
  1407. if (! app->hasAttribute ("android:hardwareAccelerated"))
  1408. app->setAttribute ("android:hardwareAccelerated", "false"); // (using the 2D acceleration slows down openGL)
  1409. return app;
  1410. }
  1411. XmlElement* createActivityElement (XmlElement& application) const
  1412. {
  1413. auto* act = getOrCreateChildWithName (application, "activity");
  1414. setAttributeIfNotPresent (*act, "android:name", getActivityClassString());
  1415. if (! act->hasAttribute ("android:configChanges"))
  1416. act->setAttribute ("android:configChanges", "keyboard|keyboardHidden|orientation|screenSize|navigation");
  1417. if (androidScreenOrientation.get() == "landscape")
  1418. {
  1419. setAttributeIfNotPresent (*act, "android:screenOrientation",
  1420. static_cast<int> (androidMinimumSDK.get()) < 18 ? "sensorLandscape" : "userLandscape");
  1421. }
  1422. else
  1423. {
  1424. setAttributeIfNotPresent (*act, "android:screenOrientation", androidScreenOrientation.get());
  1425. }
  1426. setAttributeIfNotPresent (*act, "android:launchMode", "singleTask");
  1427. // Using the 2D acceleration slows down OpenGL. We *do* enable it here for the activity though, and we disable it
  1428. // in each ComponentPeerView instead. This way any embedded native views, which are not children of ComponentPeerView,
  1429. // can still use hardware acceleration if needed (e.g. web view).
  1430. if (! act->hasAttribute ("android:hardwareAccelerated"))
  1431. act->setAttribute ("android:hardwareAccelerated", "true"); // (using the 2D acceleration slows down openGL)
  1432. act->setAttribute ("android:exported", "true");
  1433. return act;
  1434. }
  1435. void createIntentElement (XmlElement& application) const
  1436. {
  1437. auto* intent = getOrCreateChildWithName (application, "intent-filter");
  1438. auto* action = getOrCreateChildWithName (*intent, "action");
  1439. setAttributeIfNotPresent (*action, "android:name", "android.intent.action.MAIN");
  1440. auto* category = getOrCreateChildWithName (*intent, "category");
  1441. setAttributeIfNotPresent (*category, "android:name", "android.intent.category.LAUNCHER");
  1442. }
  1443. void createServiceElements (XmlElement& application) const
  1444. {
  1445. if (areRemoteNotificationsEnabled())
  1446. {
  1447. auto* service = application.createNewChildElement ("service");
  1448. service->setAttribute ("android:name", "com.rmsl.juce.JuceFirebaseMessagingService");
  1449. auto* intentFilter = service->createNewChildElement ("intent-filter");
  1450. intentFilter->createNewChildElement ("action")->setAttribute ("android:name", "com.google.firebase.MESSAGING_EVENT");
  1451. service = application.createNewChildElement ("service");
  1452. service->setAttribute ("android:name", "com.rmsl.juce.JuceFirebaseInstanceIdService");
  1453. intentFilter = service->createNewChildElement ("intent-filter");
  1454. intentFilter->createNewChildElement ("action")->setAttribute ("android:name", "com.google.firebase.INSTANCE_ID_EVENT");
  1455. auto* metaData = application.createNewChildElement ("meta-data");
  1456. metaData->setAttribute ("android:name", "firebase_analytics_collection_deactivated");
  1457. metaData->setAttribute ("android:value", "true");
  1458. }
  1459. }
  1460. void createProviderElement (XmlElement& application) const
  1461. {
  1462. if (isContentSharingEnabled())
  1463. {
  1464. auto* provider = application.createNewChildElement ("provider");
  1465. provider->setAttribute ("android:name", "com.rmsl.juce.JuceSharingContentProvider");
  1466. provider->setAttribute ("android:authorities", project.getBundleIdentifierString().toLowerCase() + ".sharingcontentprovider");
  1467. provider->setAttribute ("android:grantUriPermissions", "true");
  1468. provider->setAttribute ("android:exported", "true");
  1469. }
  1470. }
  1471. static XmlElement* getOrCreateChildWithName (XmlElement& element, const String& childName)
  1472. {
  1473. auto* child = element.getChildByName (childName);
  1474. if (child == nullptr)
  1475. child = element.createNewChildElement (childName);
  1476. return child;
  1477. }
  1478. static void setAttributeIfNotPresent (XmlElement& element, const Identifier& attribute, const String& value)
  1479. {
  1480. if (! element.hasAttribute (attribute.toString()))
  1481. element.setAttribute (attribute, value);
  1482. }
  1483. StringArray getPermissionsRequired() const
  1484. {
  1485. StringArray s = StringArray::fromTokens (androidOtherPermissions.get().toString(), ", ", {});
  1486. if (androidInternetNeeded.get())
  1487. {
  1488. s.add ("android.permission.INTERNET");
  1489. s.add ("android.permission.CHANGE_WIFI_MULTICAST_STATE");
  1490. }
  1491. if (androidMicNeeded.get())
  1492. s.add ("android.permission.RECORD_AUDIO");
  1493. if (androidCameraNeeded.get())
  1494. s.add ("android.permission.CAMERA");
  1495. if (androidBluetoothScanNeeded.get())
  1496. s.add ("android.permission.BLUETOOTH_SCAN");
  1497. if (androidBluetoothAdvertiseNeeded.get())
  1498. s.add ("android.permission.BLUETOOTH_ADVERTISE");
  1499. if (androidBluetoothConnectNeeded.get())
  1500. s.add ("android.permission.BLUETOOTH_CONNECT");
  1501. if ( androidBluetoothScanNeeded.get()
  1502. || androidBluetoothAdvertiseNeeded.get()
  1503. || androidBluetoothConnectNeeded.get())
  1504. {
  1505. s.add ("android.permission.BLUETOOTH");
  1506. s.add ("android.permission.BLUETOOTH_ADMIN");
  1507. s.add ("android.permission.ACCESS_FINE_LOCATION");
  1508. s.add ("android.permission.ACCESS_COARSE_LOCATION");
  1509. }
  1510. if (androidReadMediaAudioPermission.get())
  1511. s.add ("android.permission.READ_MEDIA_AUDIO");
  1512. if (androidReadMediaImagesPermission.get())
  1513. s.add ("android.permission.READ_MEDIA_IMAGES");
  1514. if (androidReadMediaVideoPermission.get())
  1515. s.add ("android.permission.READ_MEDIA_VIDEO");
  1516. if ( androidReadMediaAudioPermission.get()
  1517. || androidReadMediaImagesPermission.get()
  1518. || androidReadMediaVideoPermission.get())
  1519. {
  1520. s.add ("android.permission.READ_EXTERNAL_STORAGE");
  1521. }
  1522. if (androidExternalWritePermission.get())
  1523. s.add ("android.permission.WRITE_EXTERNAL_STORAGE");
  1524. if (isInAppBillingEnabled())
  1525. s.add ("com.android.vending.BILLING");
  1526. if (androidVibratePermission.get())
  1527. s.add ("android.permission.VIBRATE");
  1528. return getCleanedStringArray (s);
  1529. }
  1530. //==============================================================================
  1531. bool isLibrary() const
  1532. {
  1533. return getProject().getProjectType().isDynamicLibrary()
  1534. || getProject().getProjectType().isStaticLibrary();
  1535. }
  1536. static String toGradleList (const StringArray& array)
  1537. {
  1538. StringArray escapedArray;
  1539. for (auto element : array)
  1540. escapedArray.add ("\"" + element.replace ("\\", "\\\\").replace ("\"", "\\\"") + "\"");
  1541. return escapedArray.joinIntoString (", ");
  1542. }
  1543. bool supportsGLv3() const
  1544. {
  1545. return (static_cast<int> (androidMinimumSDK.get()) >= 18);
  1546. }
  1547. //==============================================================================
  1548. const File AndroidExecutable;
  1549. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter)
  1550. };