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.

745 lines
34KB

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