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.

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