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.

696 lines
31KB

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