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.

715 lines
32KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class AndroidProjectExporter : public ProjectExporter
  18. {
  19. public:
  20. //==============================================================================
  21. static const char* getNameAndroid() { return "Android Project"; }
  22. static const char* getValueTreeTypeName() { return "ANDROID"; }
  23. static AndroidProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  24. {
  25. if (settings.hasType (getValueTreeTypeName()))
  26. return new AndroidProjectExporter (project, settings);
  27. return nullptr;
  28. }
  29. //==============================================================================
  30. AndroidProjectExporter (Project& p, const ValueTree& t) : ProjectExporter (p, t)
  31. {
  32. name = getNameAndroid();
  33. if (getTargetLocationString().isEmpty())
  34. getTargetLocationValue() = getDefaultBuildsRootFolder() + "Android";
  35. if (getActivityClassPath().isEmpty())
  36. getActivityClassPathValue() = createDefaultClassName();
  37. if (getSDKPathString().isEmpty()) getSDKPathValue() = "${user.home}/SDKs/android-sdk";
  38. if (getNDKPathString().isEmpty()) getNDKPathValue() = "${user.home}/SDKs/android-ndk";
  39. if (getMinimumSDKVersionString().isEmpty())
  40. getMinimumSDKVersionValue() = 8;
  41. if (getInternetNeededValue().toString().isEmpty())
  42. getInternetNeededValue() = true;
  43. if (getKeyStoreValue().getValue().isVoid()) getKeyStoreValue() = "${user.home}/.android/debug.keystore";
  44. if (getKeyStorePassValue().getValue().isVoid()) getKeyStorePassValue() = "android";
  45. if (getKeyAliasValue().getValue().isVoid()) getKeyAliasValue() = "androiddebugkey";
  46. if (getKeyAliasPassValue().getValue().isVoid()) getKeyAliasPassValue() = "android";
  47. if (getCPP11EnabledValue().getValue().isVoid()) getCPP11EnabledValue() = true;
  48. }
  49. //==============================================================================
  50. bool canLaunchProject() override { return false; }
  51. bool launchProject() override { return false; }
  52. bool isAndroid() const override { return true; }
  53. bool usesMMFiles() const override { return false; }
  54. bool canCopeWithDuplicateFiles() override { return false; }
  55. void createExporterProperties (PropertyListBuilder& props) override
  56. {
  57. props.add (new TextPropertyComponent (getActivityClassPathValue(), "Android Activity class name", 256, false),
  58. "The full java class name to use for the app's Activity class.");
  59. props.add (new TextPropertyComponent (getSDKPathValue(), "Android SDK Path", 1024, false),
  60. "The path to the Android SDK folder on the target build machine");
  61. props.add (new TextPropertyComponent (getNDKPathValue(), "Android NDK Path", 1024, false),
  62. "The path to the Android NDK folder on the target build machine");
  63. props.add (new TextPropertyComponent (getMinimumSDKVersionValue(), "Minimum SDK version", 32, false),
  64. "The number of the minimum version of the Android SDK that the app requires");
  65. props.add (new TextPropertyComponent (getNDKToolchainVersionValue(), "NDK Toolchain version", 32, false),
  66. "The variable NDK_TOOLCHAIN_VERSION in Application.mk - leave blank for a default value");
  67. props.add (new BooleanPropertyComponent (getCPP11EnabledValue(), "Enable C++11 features", "Enable the -std=c++11 flag"),
  68. "If enabled, this will set the -std=c++11 flag for the build.");
  69. props.add (new BooleanPropertyComponent (getInternetNeededValue(), "Internet Access", "Specify internet access permission in the manifest"),
  70. "If enabled, this will set the android.permission.INTERNET flag in the manifest.");
  71. props.add (new BooleanPropertyComponent (getAudioRecordNeededValue(), "Audio Input Required", "Specify audio record permission in the manifest"),
  72. "If enabled, this will set the android.permission.RECORD_AUDIO flag in the manifest.");
  73. props.add (new TextPropertyComponent (getOtherPermissionsValue(), "Custom permissions", 2048, false),
  74. "A space-separated list of other permission flags that should be added to the manifest.");
  75. props.add (new TextPropertyComponent (getImportModulesValue(), "Import modules", 8192, true),
  76. "Comma or whitespace delimited list of import-module calls.");
  77. props.add (new TextPropertyComponent (getThemeValue(), "Android Theme", 256, false),
  78. "E.g. @android:style/Theme.NoTitleBar or leave blank for default");
  79. props.add (new TextPropertyComponent (getKeyStoreValue(), "Key Signing: key.store", 2048, false),
  80. "The key.store value, used when signing the package.");
  81. props.add (new TextPropertyComponent (getKeyStorePassValue(), "Key Signing: key.store.password", 2048, false),
  82. "The key.store password, used when signing the package.");
  83. props.add (new TextPropertyComponent (getKeyAliasValue(), "Key Signing: key.alias", 2048, false),
  84. "The key.alias value, used when signing the package.");
  85. props.add (new TextPropertyComponent (getKeyAliasPassValue(), "Key Signing: key.alias.password", 2048, false),
  86. "The key.alias password, used when signing the package.");
  87. }
  88. Value getActivityClassPathValue() { return getSetting (Ids::androidActivityClass); }
  89. String getActivityClassPath() const { return settings [Ids::androidActivityClass]; }
  90. Value getSDKPathValue() { return getSetting (Ids::androidSDKPath); }
  91. String getSDKPathString() const { return settings [Ids::androidSDKPath]; }
  92. Value getNDKPathValue() { return getSetting (Ids::androidNDKPath); }
  93. String getNDKPathString() const { return settings [Ids::androidNDKPath]; }
  94. Value getNDKToolchainVersionValue() { return getSetting (Ids::toolset); }
  95. String getNDKToolchainVersionString() const { return settings [Ids::toolset]; }
  96. Value getKeyStoreValue() { return getSetting (Ids::androidKeyStore); }
  97. String getKeyStoreString() const { return settings [Ids::androidKeyStore]; }
  98. Value getKeyStorePassValue() { return getSetting (Ids::androidKeyStorePass); }
  99. String getKeyStorePassString() const { return settings [Ids::androidKeyStorePass]; }
  100. Value getKeyAliasValue() { return getSetting (Ids::androidKeyAlias); }
  101. String getKeyAliasString() const { return settings [Ids::androidKeyAlias]; }
  102. Value getKeyAliasPassValue() { return getSetting (Ids::androidKeyAliasPass); }
  103. String getKeyAliasPassString() const { return settings [Ids::androidKeyAliasPass]; }
  104. Value getInternetNeededValue() { return getSetting (Ids::androidInternetNeeded); }
  105. bool getInternetNeeded() const { return settings [Ids::androidInternetNeeded]; }
  106. Value getAudioRecordNeededValue() { return getSetting (Ids::androidMicNeeded); }
  107. bool getAudioRecordNeeded() const { return settings [Ids::androidMicNeeded]; }
  108. Value getMinimumSDKVersionValue() { return getSetting (Ids::androidMinimumSDK); }
  109. String getMinimumSDKVersionString() const { return settings [Ids::androidMinimumSDK]; }
  110. Value getOtherPermissionsValue() { return getSetting (Ids::androidOtherPermissions); }
  111. String getOtherPermissions() const { return settings [Ids::androidOtherPermissions]; }
  112. Value getThemeValue() { return getSetting (Ids::androidTheme); }
  113. String getThemeString() const { return settings [Ids::androidTheme]; }
  114. Value getImportModulesValue() { return getSetting (Ids::androidImportModules); }
  115. String getImportModulesString() const { return settings [Ids::androidImportModules]; }
  116. Value getCPP11EnabledValue() { return getSetting (Ids::androidCpp11); }
  117. bool isCPP11Enabled() const { return settings [Ids::androidCpp11]; }
  118. String createDefaultClassName() const
  119. {
  120. String s (project.getBundleIdentifier().toString().toLowerCase());
  121. if (s.length() > 5
  122. && s.containsChar ('.')
  123. && s.containsOnly ("abcdefghijklmnopqrstuvwxyz_.")
  124. && ! s.startsWithChar ('.'))
  125. {
  126. if (! s.endsWithChar ('.'))
  127. s << ".";
  128. }
  129. else
  130. {
  131. s = "com.yourcompany.";
  132. }
  133. return s + CodeHelpers::makeValidIdentifier (project.getProjectFilenameRoot(), false, true, false);
  134. }
  135. //==============================================================================
  136. void create (const OwnedArray<LibraryModule>& modules) const override
  137. {
  138. const File target (getTargetFolder());
  139. const File jniFolder (target.getChildFile ("jni"));
  140. copyActivityJavaFiles (modules);
  141. createDirectoryOrThrow (jniFolder);
  142. createDirectoryOrThrow (target.getChildFile ("res").getChildFile ("values"));
  143. createDirectoryOrThrow (target.getChildFile ("libs"));
  144. createDirectoryOrThrow (target.getChildFile ("bin"));
  145. {
  146. ScopedPointer<XmlElement> manifest (createManifestXML());
  147. writeXmlOrThrow (*manifest, target.getChildFile ("AndroidManifest.xml"), "utf-8", 100, true);
  148. }
  149. writeApplicationMk (jniFolder.getChildFile ("Application.mk"));
  150. writeAndroidMk (jniFolder.getChildFile ("Android.mk"));
  151. {
  152. ScopedPointer<XmlElement> antBuildXml (createAntBuildXML());
  153. writeXmlOrThrow (*antBuildXml, target.getChildFile ("build.xml"), "UTF-8", 100);
  154. }
  155. writeProjectPropertiesFile (target.getChildFile ("project.properties"));
  156. writeLocalPropertiesFile (target.getChildFile ("local.properties"));
  157. writeStringsFile (target.getChildFile ("res/values/strings.xml"));
  158. ScopedPointer<Drawable> bigIcon (getBigIcon());
  159. ScopedPointer<Drawable> smallIcon (getSmallIcon());
  160. if (bigIcon != nullptr && smallIcon != nullptr)
  161. {
  162. const int step = jmax (bigIcon->getWidth(), bigIcon->getHeight()) / 8;
  163. writeIcon (target.getChildFile ("res/drawable-xhdpi/icon.png"), getBestIconForSize (step * 8, false));
  164. writeIcon (target.getChildFile ("res/drawable-hdpi/icon.png"), getBestIconForSize (step * 6, false));
  165. writeIcon (target.getChildFile ("res/drawable-mdpi/icon.png"), getBestIconForSize (step * 4, false));
  166. writeIcon (target.getChildFile ("res/drawable-ldpi/icon.png"), getBestIconForSize (step * 3, false));
  167. }
  168. else if (Drawable* icon = bigIcon != nullptr ? bigIcon : smallIcon)
  169. {
  170. writeIcon (target.getChildFile ("res/drawable-mdpi/icon.png"), rescaleImageForIcon (*icon, icon->getWidth()));
  171. }
  172. }
  173. protected:
  174. //==============================================================================
  175. class AndroidBuildConfiguration : public BuildConfiguration
  176. {
  177. public:
  178. AndroidBuildConfiguration (Project& p, const ValueTree& settings)
  179. : BuildConfiguration (p, settings)
  180. {
  181. if (getArchitectures().isEmpty())
  182. getArchitecturesValue() = "armeabi armeabi-v7a";
  183. }
  184. Value getArchitecturesValue() { return getValue (Ids::androidArchitectures); }
  185. String getArchitectures() const { return config [Ids::androidArchitectures]; }
  186. void createConfigProperties (PropertyListBuilder& props)
  187. {
  188. props.add (new TextPropertyComponent (getArchitecturesValue(), "Architectures", 256, false),
  189. "A list of the ARM architectures to build (for a fat binary).");
  190. }
  191. };
  192. BuildConfiguration::Ptr createBuildConfig (const ValueTree& v) const
  193. {
  194. return new AndroidBuildConfiguration (project, v);
  195. }
  196. private:
  197. //==============================================================================
  198. XmlElement* createManifestXML() const
  199. {
  200. XmlElement* manifest = new XmlElement ("manifest");
  201. manifest->setAttribute ("xmlns:android", "http://schemas.android.com/apk/res/android");
  202. manifest->setAttribute ("android:versionCode", "1");
  203. manifest->setAttribute ("android:versionName", "1.0");
  204. manifest->setAttribute ("package", getActivityClassPackage());
  205. XmlElement* screens = manifest->createNewChildElement ("supports-screens");
  206. screens->setAttribute ("android:smallScreens", "true");
  207. screens->setAttribute ("android:normalScreens", "true");
  208. screens->setAttribute ("android:largeScreens", "true");
  209. //screens->setAttribute ("android:xlargeScreens", "true");
  210. screens->setAttribute ("android:anyDensity", "true");
  211. XmlElement* sdk = manifest->createNewChildElement ("uses-sdk");
  212. sdk->setAttribute ("android:minSdkVersion", getMinimumSDKVersionString());
  213. sdk->setAttribute ("android:targetSdkVersion", "11");
  214. {
  215. const StringArray permissions (getPermissionsRequired());
  216. for (int i = permissions.size(); --i >= 0;)
  217. manifest->createNewChildElement ("uses-permission")->setAttribute ("android:name", permissions[i]);
  218. }
  219. if (project.getModules().isModuleEnabled ("juce_opengl"))
  220. {
  221. XmlElement* feature = manifest->createNewChildElement ("uses-feature");
  222. feature->setAttribute ("android:glEsVersion", "0x00020000");
  223. feature->setAttribute ("android:required", "true");
  224. }
  225. XmlElement* app = manifest->createNewChildElement ("application");
  226. app->setAttribute ("android:label", "@string/app_name");
  227. String androidThemeString (getThemeString());
  228. if (androidThemeString.isNotEmpty())
  229. app->setAttribute ("android:theme", androidThemeString);
  230. {
  231. ScopedPointer<Drawable> bigIcon (getBigIcon()), smallIcon (getSmallIcon());
  232. if (bigIcon != nullptr || smallIcon != nullptr)
  233. app->setAttribute ("android:icon", "@drawable/icon");
  234. }
  235. if (getMinimumSDKVersionString().getIntValue() >= 11)
  236. app->setAttribute ("android:hardwareAccelerated", "false"); // (using the 2D acceleration slows down openGL)
  237. XmlElement* act = app->createNewChildElement ("activity");
  238. act->setAttribute ("android:name", getActivityName());
  239. act->setAttribute ("android:label", "@string/app_name");
  240. act->setAttribute ("android:configChanges", "keyboardHidden|orientation");
  241. XmlElement* intent = act->createNewChildElement ("intent-filter");
  242. intent->createNewChildElement ("action")->setAttribute ("android:name", "android.intent.action.MAIN");
  243. intent->createNewChildElement ("category")->setAttribute ("android:name", "android.intent.category.LAUNCHER");
  244. return manifest;
  245. }
  246. StringArray getPermissionsRequired() const
  247. {
  248. StringArray s;
  249. s.addTokens (getOtherPermissions(), ", ", "");
  250. if (getInternetNeeded()) s.add ("android.permission.INTERNET");
  251. if (getAudioRecordNeeded()) s.add ("android.permission.RECORD_AUDIO");
  252. s.trim();
  253. s.removeDuplicates (false);
  254. return s;
  255. }
  256. //==============================================================================
  257. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  258. {
  259. if (projectItem.isGroup())
  260. {
  261. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  262. findAllFilesToCompile (projectItem.getChild(i), results);
  263. }
  264. else
  265. {
  266. if (projectItem.shouldBeCompiled())
  267. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  268. }
  269. }
  270. //==============================================================================
  271. String getActivityName() const
  272. {
  273. return getActivityClassPath().fromLastOccurrenceOf (".", false, false);
  274. }
  275. String getActivityClassPackage() const
  276. {
  277. return getActivityClassPath().upToLastOccurrenceOf (".", false, false);
  278. }
  279. String getJNIActivityClassName() const
  280. {
  281. return getActivityClassPath().replaceCharacter ('.', '/');
  282. }
  283. static LibraryModule* getCoreModule (const OwnedArray<LibraryModule>& modules)
  284. {
  285. for (int i = modules.size(); --i >= 0;)
  286. if (modules.getUnchecked(i)->getID() == "juce_core")
  287. return modules.getUnchecked(i);
  288. return nullptr;
  289. }
  290. void copyActivityJavaFiles (const OwnedArray<LibraryModule>& modules) const
  291. {
  292. const String className (getActivityName());
  293. const String package (getActivityClassPackage());
  294. String path (package.replaceCharacter ('.', File::separator));
  295. if (path.isEmpty() || className.isEmpty())
  296. throw SaveError ("Invalid Android Activity class name: " + getActivityClassPath());
  297. const File classFolder (getTargetFolder().getChildFile ("src")
  298. .getChildFile (path));
  299. createDirectoryOrThrow (classFolder);
  300. LibraryModule* const coreModule = getCoreModule (modules);
  301. if (coreModule != nullptr)
  302. {
  303. File javaDestFile (classFolder.getChildFile (className + ".java"));
  304. File javaSourceFile (coreModule->getFolder().getChildFile ("native")
  305. .getChildFile ("java")
  306. .getChildFile ("JuceAppActivity.java"));
  307. MemoryOutputStream newFile;
  308. newFile << javaSourceFile.loadFileAsString()
  309. .replace ("JuceAppActivity", className)
  310. .replace ("package com.juce;", "package " + package + ";");
  311. overwriteFileIfDifferentOrThrow (javaDestFile, newFile);
  312. }
  313. }
  314. String getABIs (bool forDebug) const
  315. {
  316. for (ConstConfigIterator config (*this); config.next();)
  317. {
  318. const AndroidBuildConfiguration& androidConfig = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  319. if (config->isDebug() == forDebug)
  320. return androidConfig.getArchitectures();
  321. }
  322. return String();
  323. }
  324. String getCppFlags() const
  325. {
  326. String flags ("-fsigned-char -fexceptions -frtti");
  327. if (! getNDKToolchainVersionString().startsWithIgnoreCase ("clang"))
  328. flags << " -Wno-psabi";
  329. return flags;
  330. }
  331. String getToolchainVersion() const
  332. {
  333. String v (getNDKToolchainVersionString());
  334. return v.isNotEmpty() ? v : "4.8";
  335. }
  336. void writeApplicationMk (const File& file) const
  337. {
  338. MemoryOutputStream mo;
  339. mo << "# Automatically generated makefile, created by the Introjucer" << newLine
  340. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  341. << newLine
  342. << "APP_STL := gnustl_static" << newLine
  343. << "APP_CPPFLAGS += " << getCppFlags() << newLine
  344. << "APP_PLATFORM := " << getAppPlatform() << newLine
  345. << "NDK_TOOLCHAIN_VERSION := " << getToolchainVersion() << newLine
  346. << newLine
  347. << "ifeq ($(NDK_DEBUG),1)" << newLine
  348. << " APP_ABI := " << getABIs (true) << newLine
  349. << "else" << newLine
  350. << " APP_ABI := " << getABIs (false) << newLine
  351. << "endif" << newLine;
  352. overwriteFileIfDifferentOrThrow (file, mo);
  353. }
  354. void writeAndroidMk (const File& file) const
  355. {
  356. Array<RelativePath> files;
  357. for (int i = 0; i < getAllGroups().size(); ++i)
  358. findAllFilesToCompile (getAllGroups().getReference(i), files);
  359. MemoryOutputStream mo;
  360. writeAndroidMk (mo, files);
  361. overwriteFileIfDifferentOrThrow (file, mo);
  362. }
  363. void writeAndroidMk (OutputStream& out, const Array<RelativePath>& files) const
  364. {
  365. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  366. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  367. << newLine
  368. << "LOCAL_PATH := $(call my-dir)" << newLine
  369. << newLine
  370. << "include $(CLEAR_VARS)" << newLine
  371. << newLine
  372. << "ifeq ($(TARGET_ARCH_ABI), armeabi-v7a)" << newLine
  373. << " LOCAL_ARM_MODE := arm" << newLine
  374. << "endif" << newLine
  375. << newLine
  376. << "LOCAL_MODULE := juce_jni" << newLine
  377. << "LOCAL_SRC_FILES := \\" << newLine;
  378. for (int i = 0; i < files.size(); ++i)
  379. out << " " << (files.getReference(i).isAbsolute() ? "" : "../")
  380. << escapeSpaces (files.getReference(i).toUnixStyle()) << "\\" << newLine;
  381. out << newLine
  382. << "ifeq ($(NDK_DEBUG),1)" << newLine;
  383. writeConfigSettings (out, true);
  384. out << "else" << newLine;
  385. writeConfigSettings (out, false);
  386. out << "endif" << newLine
  387. << newLine
  388. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  389. const StringArray importModules (getCommaOrWhitespaceSeparatedItems (getImportModulesString()));
  390. for (int i = 0; i < importModules.size(); ++i)
  391. out << "$(call import-module," << importModules[i] << ")" << newLine;
  392. }
  393. void writeConfigSettings (OutputStream& out, bool forDebug) const
  394. {
  395. for (ConstConfigIterator config (*this); config.next();)
  396. {
  397. if (config->isDebug() == forDebug)
  398. {
  399. const AndroidBuildConfiguration& androidConfig = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  400. String cppFlags;
  401. cppFlags << createCPPFlags (androidConfig)
  402. << (" " + replacePreprocessorTokens (androidConfig, getExtraCompilerFlagsString()).trim()).trimEnd()
  403. << newLine
  404. << getLDLIBS (androidConfig).trimEnd()
  405. << newLine;
  406. out << " LOCAL_CPPFLAGS += " << cppFlags;
  407. out << " LOCAL_CFLAGS += " << cppFlags;
  408. break;
  409. }
  410. }
  411. }
  412. String getLDLIBS (const AndroidBuildConfiguration& config) const
  413. {
  414. return " LOCAL_LDLIBS :=" + config.getGCCLibraryPathFlags()
  415. + " -llog -lGLESv2 " + getExternalLibraryFlags (config)
  416. + " " + replacePreprocessorTokens (config, getExtraLinkerFlagsString());
  417. }
  418. String createIncludePathFlags (const BuildConfiguration& config) const
  419. {
  420. String flags;
  421. StringArray searchPaths (extraSearchPaths);
  422. searchPaths.addArray (config.getHeaderSearchPaths());
  423. searchPaths.removeDuplicates (false);
  424. for (int i = 0; i < searchPaths.size(); ++i)
  425. flags << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])).quoted();
  426. return flags;
  427. }
  428. String createCPPFlags (const BuildConfiguration& config) const
  429. {
  430. StringPairArray defines;
  431. defines.set ("JUCE_ANDROID", "1");
  432. defines.set ("JUCE_ANDROID_API_VERSION", getMinimumSDKVersionString());
  433. defines.set ("JUCE_ANDROID_ACTIVITY_CLASSNAME", getJNIActivityClassName().replaceCharacter ('/', '_'));
  434. defines.set ("JUCE_ANDROID_ACTIVITY_CLASSPATH", "\\\"" + getJNIActivityClassName() + "\\\"");
  435. String flags ("-fsigned-char -fexceptions -frtti");
  436. if (config.isDebug())
  437. {
  438. flags << " -g";
  439. defines.set ("DEBUG", "1");
  440. defines.set ("_DEBUG", "1");
  441. }
  442. else
  443. {
  444. defines.set ("NDEBUG", "1");
  445. }
  446. flags << createIncludePathFlags (config)
  447. << " -O" << config.getGCCOptimisationFlag();
  448. if (isCPP11Enabled())
  449. flags << " -std=c++11 -std=gnu++11"; // these flags seem to enable slightly different things on gcc, and both seem to be needed
  450. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  451. return flags + createGCCPreprocessorFlags (defines);
  452. }
  453. //==============================================================================
  454. XmlElement* createAntBuildXML() const
  455. {
  456. XmlElement* proj = new XmlElement ("project");
  457. proj->setAttribute ("name", projectName);
  458. proj->setAttribute ("default", "debug");
  459. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "local.properties");
  460. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "project.properties");
  461. {
  462. XmlElement* target = proj->createNewChildElement ("target");
  463. target->setAttribute ("name", "clean");
  464. target->setAttribute ("depends", "android_rules.clean");
  465. target->createNewChildElement ("delete")->setAttribute ("dir", "libs");
  466. target->createNewChildElement ("delete")->setAttribute ("dir", "obj");
  467. XmlElement* executable = target->createNewChildElement ("exec");
  468. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  469. executable->setAttribute ("dir", "${basedir}");
  470. executable->setAttribute ("failonerror", "true");
  471. executable->createNewChildElement ("arg")->setAttribute ("value", "clean");
  472. }
  473. {
  474. XmlElement* target = proj->createNewChildElement ("target");
  475. target->setAttribute ("name", "-pre-build");
  476. addDebugConditionClause (target, "makefileConfig", "Debug", "Release");
  477. addDebugConditionClause (target, "ndkDebugValue", "NDK_DEBUG=1", "NDK_DEBUG=0");
  478. String debugABIs, releaseABIs;
  479. for (ConstConfigIterator config (*this); config.next();)
  480. {
  481. const AndroidBuildConfiguration& androidConfig = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  482. if (config->isDebug())
  483. debugABIs = androidConfig.getArchitectures();
  484. else
  485. releaseABIs = androidConfig.getArchitectures();
  486. }
  487. addDebugConditionClause (target, "app_abis", debugABIs, releaseABIs);
  488. XmlElement* executable = target->createNewChildElement ("exec");
  489. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  490. executable->setAttribute ("dir", "${basedir}");
  491. executable->setAttribute ("failonerror", "true");
  492. executable->createNewChildElement ("arg")->setAttribute ("value", "--jobs=2");
  493. executable->createNewChildElement ("arg")->setAttribute ("value", "CONFIG=${makefileConfig}");
  494. executable->createNewChildElement ("arg")->setAttribute ("value", "${ndkDebugValue}");
  495. executable->createNewChildElement ("arg")->setAttribute ("value", "APP_ABI=${app_abis}");
  496. target->createNewChildElement ("delete")->setAttribute ("file", "${out.final.file}");
  497. target->createNewChildElement ("delete")->setAttribute ("file", "${out.packaged.file}");
  498. }
  499. proj->createNewChildElement ("import")->setAttribute ("file", "${sdk.dir}/tools/ant/build.xml");
  500. return proj;
  501. }
  502. void addDebugConditionClause (XmlElement* target, const String& property,
  503. const String& debugValue, const String& releaseValue) const
  504. {
  505. XmlElement* condition = target->createNewChildElement ("condition");
  506. condition->setAttribute ("property", property);
  507. condition->setAttribute ("value", debugValue);
  508. condition->setAttribute ("else", releaseValue);
  509. XmlElement* equals = condition->createNewChildElement ("equals");
  510. equals->setAttribute ("arg1", "${ant.project.invoked-targets}");
  511. equals->setAttribute ("arg2", "debug");
  512. }
  513. String getAppPlatform() const
  514. {
  515. int ndkVersion = getMinimumSDKVersionString().getIntValue();
  516. if (ndkVersion == 9)
  517. ndkVersion = 10; // (doesn't seem to be a version '9')
  518. return "android-" + String (ndkVersion);
  519. }
  520. void writeProjectPropertiesFile (const File& file) const
  521. {
  522. MemoryOutputStream mo;
  523. mo << "# This file is used to override default values used by the Ant build system." << newLine
  524. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  525. << newLine
  526. << "target=" << getAppPlatform() << newLine
  527. << newLine;
  528. overwriteFileIfDifferentOrThrow (file, mo);
  529. }
  530. void writeLocalPropertiesFile (const File& file) const
  531. {
  532. MemoryOutputStream mo;
  533. mo << "# This file is used to override default values used by the Ant build system." << newLine
  534. << "# It is automatically generated by the Introjucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  535. << newLine
  536. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPathString())) << newLine
  537. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPathString())) << newLine
  538. << "key.store=" << getKeyStoreString() << newLine
  539. << "key.alias=" << getKeyAliasString() << newLine
  540. << "key.store.password=" << getKeyStorePassString() << newLine
  541. << "key.alias.password=" << getKeyAliasPassString() << newLine
  542. << newLine;
  543. overwriteFileIfDifferentOrThrow (file, mo);
  544. }
  545. void writeIcon (const File& file, const Image& im) const
  546. {
  547. if (im.isValid())
  548. {
  549. createDirectoryOrThrow (file.getParentDirectory());
  550. PNGImageFormat png;
  551. MemoryOutputStream mo;
  552. if (! png.writeImageToStream (im, mo))
  553. throw SaveError ("Can't generate Android icon file");
  554. overwriteFileIfDifferentOrThrow (file, mo);
  555. }
  556. }
  557. void writeStringsFile (const File& file) const
  558. {
  559. XmlElement strings ("resources");
  560. XmlElement* resourceName = strings.createNewChildElement ("string");
  561. resourceName->setAttribute ("name", "app_name");
  562. resourceName->addTextElement (projectName);
  563. writeXmlOrThrow (strings, file, "utf-8", 100);
  564. }
  565. //==============================================================================
  566. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter)
  567. };