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.

719 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() = 10;
  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. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  187. void createConfigProperties (PropertyListBuilder& props) override
  188. {
  189. addGCCOptimisationProperty (props);
  190. props.add (new TextPropertyComponent (getArchitecturesValue(), "Architectures", 256, false),
  191. "A list of the ARM architectures to build (for a fat binary).");
  192. }
  193. };
  194. BuildConfiguration::Ptr createBuildConfig (const ValueTree& v) const
  195. {
  196. return new AndroidBuildConfiguration (project, v);
  197. }
  198. private:
  199. //==============================================================================
  200. XmlElement* createManifestXML() const
  201. {
  202. XmlElement* manifest = new XmlElement ("manifest");
  203. manifest->setAttribute ("xmlns:android", "http://schemas.android.com/apk/res/android");
  204. manifest->setAttribute ("android:versionCode", "1");
  205. manifest->setAttribute ("android:versionName", "1.0");
  206. manifest->setAttribute ("package", getActivityClassPackage());
  207. XmlElement* screens = manifest->createNewChildElement ("supports-screens");
  208. screens->setAttribute ("android:smallScreens", "true");
  209. screens->setAttribute ("android:normalScreens", "true");
  210. screens->setAttribute ("android:largeScreens", "true");
  211. //screens->setAttribute ("android:xlargeScreens", "true");
  212. screens->setAttribute ("android:anyDensity", "true");
  213. XmlElement* sdk = manifest->createNewChildElement ("uses-sdk");
  214. sdk->setAttribute ("android:minSdkVersion", getMinimumSDKVersionString());
  215. sdk->setAttribute ("android:targetSdkVersion", "11");
  216. {
  217. const StringArray permissions (getPermissionsRequired());
  218. for (int i = permissions.size(); --i >= 0;)
  219. manifest->createNewChildElement ("uses-permission")->setAttribute ("android:name", permissions[i]);
  220. }
  221. if (project.getModules().isModuleEnabled ("juce_opengl"))
  222. {
  223. XmlElement* feature = manifest->createNewChildElement ("uses-feature");
  224. feature->setAttribute ("android:glEsVersion", "0x00020000");
  225. feature->setAttribute ("android:required", "true");
  226. }
  227. XmlElement* app = manifest->createNewChildElement ("application");
  228. app->setAttribute ("android:label", "@string/app_name");
  229. String androidThemeString (getThemeString());
  230. if (androidThemeString.isNotEmpty())
  231. app->setAttribute ("android:theme", androidThemeString);
  232. {
  233. ScopedPointer<Drawable> bigIcon (getBigIcon()), smallIcon (getSmallIcon());
  234. if (bigIcon != nullptr || smallIcon != nullptr)
  235. app->setAttribute ("android:icon", "@drawable/icon");
  236. }
  237. if (getMinimumSDKVersionString().getIntValue() >= 11)
  238. app->setAttribute ("android:hardwareAccelerated", "false"); // (using the 2D acceleration slows down openGL)
  239. XmlElement* act = app->createNewChildElement ("activity");
  240. act->setAttribute ("android:name", getActivityName());
  241. act->setAttribute ("android:label", "@string/app_name");
  242. act->setAttribute ("android:configChanges", "keyboardHidden|orientation");
  243. XmlElement* intent = act->createNewChildElement ("intent-filter");
  244. intent->createNewChildElement ("action")->setAttribute ("android:name", "android.intent.action.MAIN");
  245. intent->createNewChildElement ("category")->setAttribute ("android:name", "android.intent.category.LAUNCHER");
  246. return manifest;
  247. }
  248. StringArray getPermissionsRequired() const
  249. {
  250. StringArray s;
  251. s.addTokens (getOtherPermissions(), ", ", "");
  252. if (getInternetNeeded()) s.add ("android.permission.INTERNET");
  253. if (getAudioRecordNeeded()) s.add ("android.permission.RECORD_AUDIO");
  254. s.trim();
  255. s.removeDuplicates (false);
  256. return s;
  257. }
  258. //==============================================================================
  259. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  260. {
  261. if (projectItem.isGroup())
  262. {
  263. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  264. findAllFilesToCompile (projectItem.getChild(i), results);
  265. }
  266. else
  267. {
  268. if (projectItem.shouldBeCompiled())
  269. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  270. }
  271. }
  272. //==============================================================================
  273. String getActivityName() const
  274. {
  275. return getActivityClassPath().fromLastOccurrenceOf (".", false, false);
  276. }
  277. String getActivityClassPackage() const
  278. {
  279. return getActivityClassPath().upToLastOccurrenceOf (".", false, false);
  280. }
  281. String getJNIActivityClassName() const
  282. {
  283. return getActivityClassPath().replaceCharacter ('.', '/');
  284. }
  285. static LibraryModule* getCoreModule (const OwnedArray<LibraryModule>& modules)
  286. {
  287. for (int i = modules.size(); --i >= 0;)
  288. if (modules.getUnchecked(i)->getID() == "juce_core")
  289. return modules.getUnchecked(i);
  290. return nullptr;
  291. }
  292. void copyActivityJavaFiles (const OwnedArray<LibraryModule>& modules) const
  293. {
  294. const String className (getActivityName());
  295. const String package (getActivityClassPackage());
  296. String path (package.replaceCharacter ('.', File::separator));
  297. if (path.isEmpty() || className.isEmpty())
  298. throw SaveError ("Invalid Android Activity class name: " + getActivityClassPath());
  299. const File classFolder (getTargetFolder().getChildFile ("src")
  300. .getChildFile (path));
  301. createDirectoryOrThrow (classFolder);
  302. LibraryModule* const coreModule = getCoreModule (modules);
  303. if (coreModule != nullptr)
  304. {
  305. File javaDestFile (classFolder.getChildFile (className + ".java"));
  306. File javaSourceFile (coreModule->getFolder().getChildFile ("native")
  307. .getChildFile ("java")
  308. .getChildFile ("JuceAppActivity.java"));
  309. MemoryOutputStream newFile;
  310. newFile << javaSourceFile.loadFileAsString()
  311. .replace ("JuceAppActivity", className)
  312. .replace ("package com.juce;", "package " + package + ";");
  313. overwriteFileIfDifferentOrThrow (javaDestFile, newFile);
  314. }
  315. }
  316. String getABIs (bool forDebug) const
  317. {
  318. for (ConstConfigIterator config (*this); config.next();)
  319. {
  320. const AndroidBuildConfiguration& androidConfig = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  321. if (config->isDebug() == forDebug)
  322. return androidConfig.getArchitectures();
  323. }
  324. return String();
  325. }
  326. String getCppFlags() const
  327. {
  328. String flags ("-fsigned-char -fexceptions -frtti");
  329. if (! getNDKToolchainVersionString().startsWithIgnoreCase ("clang"))
  330. flags << " -Wno-psabi";
  331. return flags;
  332. }
  333. String getToolchainVersion() const
  334. {
  335. String v (getNDKToolchainVersionString());
  336. return v.isNotEmpty() ? v : "4.8";
  337. }
  338. void writeApplicationMk (const File& file) const
  339. {
  340. MemoryOutputStream mo;
  341. mo << "# Automatically generated makefile, created by the Introjucer" << newLine
  342. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  343. << newLine
  344. << "APP_STL := gnustl_static" << newLine
  345. << "APP_CPPFLAGS += " << getCppFlags() << newLine
  346. << "APP_PLATFORM := " << getAppPlatform() << newLine
  347. << "NDK_TOOLCHAIN_VERSION := " << getToolchainVersion() << newLine
  348. << newLine
  349. << "ifeq ($(NDK_DEBUG),1)" << newLine
  350. << " APP_ABI := " << getABIs (true) << newLine
  351. << "else" << newLine
  352. << " APP_ABI := " << getABIs (false) << newLine
  353. << "endif" << newLine;
  354. overwriteFileIfDifferentOrThrow (file, mo);
  355. }
  356. void writeAndroidMk (const File& file) const
  357. {
  358. Array<RelativePath> files;
  359. for (int i = 0; i < getAllGroups().size(); ++i)
  360. findAllFilesToCompile (getAllGroups().getReference(i), files);
  361. MemoryOutputStream mo;
  362. writeAndroidMk (mo, files);
  363. overwriteFileIfDifferentOrThrow (file, mo);
  364. }
  365. void writeAndroidMk (OutputStream& out, const Array<RelativePath>& files) const
  366. {
  367. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  368. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  369. << newLine
  370. << "LOCAL_PATH := $(call my-dir)" << newLine
  371. << newLine
  372. << "include $(CLEAR_VARS)" << newLine
  373. << newLine
  374. << "ifeq ($(TARGET_ARCH_ABI), armeabi-v7a)" << newLine
  375. << " LOCAL_ARM_MODE := arm" << newLine
  376. << "endif" << newLine
  377. << newLine
  378. << "LOCAL_MODULE := juce_jni" << newLine
  379. << "LOCAL_SRC_FILES := \\" << newLine;
  380. for (int i = 0; i < files.size(); ++i)
  381. out << " " << (files.getReference(i).isAbsolute() ? "" : "../")
  382. << escapeSpaces (files.getReference(i).toUnixStyle()) << "\\" << newLine;
  383. out << newLine
  384. << "ifeq ($(NDK_DEBUG),1)" << newLine;
  385. writeConfigSettings (out, true);
  386. out << "else" << newLine;
  387. writeConfigSettings (out, false);
  388. out << "endif" << newLine
  389. << newLine
  390. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  391. const StringArray importModules (getCommaOrWhitespaceSeparatedItems (getImportModulesString()));
  392. for (int i = 0; i < importModules.size(); ++i)
  393. out << "$(call import-module," << importModules[i] << ")" << newLine;
  394. }
  395. void writeConfigSettings (OutputStream& out, bool forDebug) const
  396. {
  397. for (ConstConfigIterator config (*this); config.next();)
  398. {
  399. if (config->isDebug() == forDebug)
  400. {
  401. const AndroidBuildConfiguration& androidConfig = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  402. String cppFlags;
  403. cppFlags << createCPPFlags (androidConfig)
  404. << (" " + replacePreprocessorTokens (androidConfig, getExtraCompilerFlagsString()).trim()).trimEnd()
  405. << newLine
  406. << getLDLIBS (androidConfig).trimEnd()
  407. << newLine;
  408. out << " LOCAL_CPPFLAGS += " << cppFlags;
  409. out << " LOCAL_CFLAGS += " << cppFlags;
  410. break;
  411. }
  412. }
  413. }
  414. String getLDLIBS (const AndroidBuildConfiguration& config) const
  415. {
  416. return " LOCAL_LDLIBS :=" + config.getGCCLibraryPathFlags()
  417. + " -llog -lGLESv2 " + getExternalLibraryFlags (config)
  418. + " " + replacePreprocessorTokens (config, getExtraLinkerFlagsString());
  419. }
  420. String createIncludePathFlags (const BuildConfiguration& config) const
  421. {
  422. String flags;
  423. StringArray searchPaths (extraSearchPaths);
  424. searchPaths.addArray (config.getHeaderSearchPaths());
  425. searchPaths.removeDuplicates (false);
  426. for (int i = 0; i < searchPaths.size(); ++i)
  427. flags << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])).quoted();
  428. return flags;
  429. }
  430. String createCPPFlags (const BuildConfiguration& config) const
  431. {
  432. StringPairArray defines;
  433. defines.set ("JUCE_ANDROID", "1");
  434. defines.set ("JUCE_ANDROID_API_VERSION", getMinimumSDKVersionString());
  435. defines.set ("JUCE_ANDROID_ACTIVITY_CLASSNAME", getJNIActivityClassName().replaceCharacter ('/', '_'));
  436. defines.set ("JUCE_ANDROID_ACTIVITY_CLASSPATH", "\\\"" + getJNIActivityClassName() + "\\\"");
  437. String flags ("-fsigned-char -fexceptions -frtti");
  438. if (config.isDebug())
  439. {
  440. flags << " -g";
  441. defines.set ("DEBUG", "1");
  442. defines.set ("_DEBUG", "1");
  443. }
  444. else
  445. {
  446. defines.set ("NDEBUG", "1");
  447. }
  448. flags << createIncludePathFlags (config)
  449. << " -O" << config.getGCCOptimisationFlag();
  450. if (isCPP11Enabled())
  451. flags << " -std=c++11 -std=gnu++11"; // these flags seem to enable slightly different things on gcc, and both seem to be needed
  452. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  453. return flags + createGCCPreprocessorFlags (defines);
  454. }
  455. //==============================================================================
  456. XmlElement* createAntBuildXML() const
  457. {
  458. XmlElement* proj = new XmlElement ("project");
  459. proj->setAttribute ("name", projectName);
  460. proj->setAttribute ("default", "debug");
  461. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "local.properties");
  462. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "project.properties");
  463. {
  464. XmlElement* target = proj->createNewChildElement ("target");
  465. target->setAttribute ("name", "clean");
  466. target->setAttribute ("depends", "android_rules.clean");
  467. target->createNewChildElement ("delete")->setAttribute ("dir", "libs");
  468. target->createNewChildElement ("delete")->setAttribute ("dir", "obj");
  469. XmlElement* executable = target->createNewChildElement ("exec");
  470. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  471. executable->setAttribute ("dir", "${basedir}");
  472. executable->setAttribute ("failonerror", "true");
  473. executable->createNewChildElement ("arg")->setAttribute ("value", "clean");
  474. }
  475. {
  476. XmlElement* target = proj->createNewChildElement ("target");
  477. target->setAttribute ("name", "-pre-build");
  478. addDebugConditionClause (target, "makefileConfig", "Debug", "Release");
  479. addDebugConditionClause (target, "ndkDebugValue", "NDK_DEBUG=1", "NDK_DEBUG=0");
  480. String debugABIs, releaseABIs;
  481. for (ConstConfigIterator config (*this); config.next();)
  482. {
  483. const AndroidBuildConfiguration& androidConfig = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  484. if (config->isDebug())
  485. debugABIs = androidConfig.getArchitectures();
  486. else
  487. releaseABIs = androidConfig.getArchitectures();
  488. }
  489. addDebugConditionClause (target, "app_abis", debugABIs, releaseABIs);
  490. XmlElement* executable = target->createNewChildElement ("exec");
  491. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  492. executable->setAttribute ("dir", "${basedir}");
  493. executable->setAttribute ("failonerror", "true");
  494. executable->createNewChildElement ("arg")->setAttribute ("value", "--jobs=2");
  495. executable->createNewChildElement ("arg")->setAttribute ("value", "CONFIG=${makefileConfig}");
  496. executable->createNewChildElement ("arg")->setAttribute ("value", "${ndkDebugValue}");
  497. executable->createNewChildElement ("arg")->setAttribute ("value", "APP_ABI=${app_abis}");
  498. target->createNewChildElement ("delete")->setAttribute ("file", "${out.final.file}");
  499. target->createNewChildElement ("delete")->setAttribute ("file", "${out.packaged.file}");
  500. }
  501. proj->createNewChildElement ("import")->setAttribute ("file", "${sdk.dir}/tools/ant/build.xml");
  502. return proj;
  503. }
  504. void addDebugConditionClause (XmlElement* target, const String& property,
  505. const String& debugValue, const String& releaseValue) const
  506. {
  507. XmlElement* condition = target->createNewChildElement ("condition");
  508. condition->setAttribute ("property", property);
  509. condition->setAttribute ("value", debugValue);
  510. condition->setAttribute ("else", releaseValue);
  511. XmlElement* equals = condition->createNewChildElement ("equals");
  512. equals->setAttribute ("arg1", "${ant.project.invoked-targets}");
  513. equals->setAttribute ("arg2", "debug");
  514. }
  515. String getAppPlatform() const
  516. {
  517. int ndkVersion = getMinimumSDKVersionString().getIntValue();
  518. if (ndkVersion == 9)
  519. ndkVersion = 10; // (doesn't seem to be a version '9')
  520. return "android-" + String (ndkVersion);
  521. }
  522. void writeProjectPropertiesFile (const File& file) const
  523. {
  524. MemoryOutputStream mo;
  525. mo << "# This file is used to override default values used by the Ant build system." << newLine
  526. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  527. << newLine
  528. << "target=" << getAppPlatform() << newLine
  529. << newLine;
  530. overwriteFileIfDifferentOrThrow (file, mo);
  531. }
  532. void writeLocalPropertiesFile (const File& file) const
  533. {
  534. MemoryOutputStream mo;
  535. mo << "# This file is used to override default values used by the Ant build system." << newLine
  536. << "# It is automatically generated by the Introjucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  537. << newLine
  538. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPathString())) << newLine
  539. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPathString())) << newLine
  540. << "key.store=" << getKeyStoreString() << newLine
  541. << "key.alias=" << getKeyAliasString() << newLine
  542. << "key.store.password=" << getKeyStorePassString() << newLine
  543. << "key.alias.password=" << getKeyAliasPassString() << newLine
  544. << newLine;
  545. overwriteFileIfDifferentOrThrow (file, mo);
  546. }
  547. void writeIcon (const File& file, const Image& im) const
  548. {
  549. if (im.isValid())
  550. {
  551. createDirectoryOrThrow (file.getParentDirectory());
  552. PNGImageFormat png;
  553. MemoryOutputStream mo;
  554. if (! png.writeImageToStream (im, mo))
  555. throw SaveError ("Can't generate Android icon file");
  556. overwriteFileIfDifferentOrThrow (file, mo);
  557. }
  558. }
  559. void writeStringsFile (const File& file) const
  560. {
  561. XmlElement strings ("resources");
  562. XmlElement* resourceName = strings.createNewChildElement ("string");
  563. resourceName->setAttribute ("name", "app_name");
  564. resourceName->addTextElement (projectName);
  565. writeXmlOrThrow (strings, file, "utf-8", 100);
  566. }
  567. //==============================================================================
  568. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter)
  569. };