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.

744 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. return getCleanedStringArray (s);
  265. }
  266. //==============================================================================
  267. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  268. {
  269. if (projectItem.isGroup())
  270. {
  271. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  272. findAllFilesToCompile (projectItem.getChild(i), results);
  273. }
  274. else
  275. {
  276. if (projectItem.shouldBeCompiled())
  277. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  278. }
  279. }
  280. //==============================================================================
  281. String getActivityName() const
  282. {
  283. return getActivityClassPath().fromLastOccurrenceOf (".", false, false);
  284. }
  285. String getActivityClassPackage() const
  286. {
  287. return getActivityClassPath().upToLastOccurrenceOf (".", false, false);
  288. }
  289. String getJNIActivityClassName() const
  290. {
  291. return getActivityClassPath().replaceCharacter ('.', '/');
  292. }
  293. static LibraryModule* getCoreModule (const OwnedArray<LibraryModule>& modules)
  294. {
  295. for (int i = modules.size(); --i >= 0;)
  296. if (modules.getUnchecked(i)->getID() == "juce_core")
  297. return modules.getUnchecked(i);
  298. return nullptr;
  299. }
  300. void copyActivityJavaFiles (const OwnedArray<LibraryModule>& modules) const
  301. {
  302. const String className (getActivityName());
  303. const String package (getActivityClassPackage());
  304. String path (package.replaceCharacter ('.', File::separator));
  305. if (path.isEmpty() || className.isEmpty())
  306. throw SaveError ("Invalid Android Activity class name: " + getActivityClassPath());
  307. const File classFolder (getTargetFolder().getChildFile ("src")
  308. .getChildFile (path));
  309. createDirectoryOrThrow (classFolder);
  310. LibraryModule* const coreModule = getCoreModule (modules);
  311. if (coreModule != nullptr)
  312. {
  313. File javaDestFile (classFolder.getChildFile (className + ".java"));
  314. File javaSourceFile (coreModule->getFolder().getChildFile ("native")
  315. .getChildFile ("java")
  316. .getChildFile ("JuceAppActivity.java"));
  317. MemoryOutputStream newFile;
  318. newFile << javaSourceFile.loadFileAsString()
  319. .replace ("JuceAppActivity", className)
  320. .replace ("package com.juce;", "package " + package + ";");
  321. overwriteFileIfDifferentOrThrow (javaDestFile, newFile);
  322. }
  323. }
  324. String getABIs (bool forDebug) const
  325. {
  326. for (ConstConfigIterator config (*this); config.next();)
  327. {
  328. const AndroidBuildConfiguration& androidConfig = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  329. if (config->isDebug() == forDebug)
  330. return androidConfig.getArchitectures();
  331. }
  332. return String();
  333. }
  334. String getCppFlags() const
  335. {
  336. String flags ("-fsigned-char -fexceptions -frtti");
  337. if (! getNDKToolchainVersionString().startsWithIgnoreCase ("clang"))
  338. flags << " -Wno-psabi";
  339. return flags;
  340. }
  341. String getToolchainVersion() const
  342. {
  343. String v (getNDKToolchainVersionString());
  344. return v.isNotEmpty() ? v : "4.8";
  345. }
  346. void writeApplicationMk (const File& file) const
  347. {
  348. MemoryOutputStream mo;
  349. mo << "# Automatically generated makefile, created by the Introjucer" << newLine
  350. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  351. << newLine
  352. << "APP_STL := gnustl_static" << newLine
  353. << "APP_CPPFLAGS += " << getCppFlags() << newLine
  354. << "APP_PLATFORM := " << getAppPlatform() << newLine
  355. << "NDK_TOOLCHAIN_VERSION := " << getToolchainVersion() << newLine
  356. << newLine
  357. << "ifeq ($(NDK_DEBUG),1)" << newLine
  358. << " APP_ABI := " << getABIs (true) << newLine
  359. << "else" << newLine
  360. << " APP_ABI := " << getABIs (false) << newLine
  361. << "endif" << newLine;
  362. overwriteFileIfDifferentOrThrow (file, mo);
  363. }
  364. void writeAndroidMk (const File& file) const
  365. {
  366. Array<RelativePath> files;
  367. for (int i = 0; i < getAllGroups().size(); ++i)
  368. findAllFilesToCompile (getAllGroups().getReference(i), files);
  369. MemoryOutputStream mo;
  370. writeAndroidMk (mo, files);
  371. overwriteFileIfDifferentOrThrow (file, mo);
  372. }
  373. void writeAndroidMkVariableList (OutputStream& out, const String& variableName, const String& settingsValue) const
  374. {
  375. const StringArray separatedItems (getCommaOrWhitespaceSeparatedItems (settingsValue));
  376. if (separatedItems.size() > 0)
  377. out << newLine << variableName << " := " << separatedItems.joinIntoString (" ") << newLine;
  378. }
  379. void writeAndroidMk (OutputStream& out, const Array<RelativePath>& files) const
  380. {
  381. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  382. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  383. << newLine
  384. << "LOCAL_PATH := $(call my-dir)" << newLine
  385. << newLine
  386. << "include $(CLEAR_VARS)" << newLine
  387. << newLine
  388. << "ifeq ($(TARGET_ARCH_ABI), armeabi-v7a)" << newLine
  389. << " LOCAL_ARM_MODE := arm" << newLine
  390. << "endif" << newLine
  391. << newLine
  392. << "LOCAL_MODULE := juce_jni" << newLine
  393. << "LOCAL_SRC_FILES := \\" << newLine;
  394. for (int i = 0; i < files.size(); ++i)
  395. out << " " << (files.getReference(i).isAbsolute() ? "" : "../")
  396. << escapeSpaces (files.getReference(i).toUnixStyle()) << "\\" << newLine;
  397. writeAndroidMkVariableList (out, "LOCAL_STATIC_LIBRARIES", getStaticLibrariesString());
  398. writeAndroidMkVariableList (out, "LOCAL_SHARED_LIBRARIES", getSharedLibrariesString());
  399. out << newLine
  400. << "ifeq ($(NDK_DEBUG),1)" << newLine;
  401. writeConfigSettings (out, true);
  402. out << "else" << newLine;
  403. writeConfigSettings (out, false);
  404. out << "endif" << newLine
  405. << newLine
  406. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  407. StringArray importModules (getCommaOrWhitespaceSeparatedItems (getStaticLibrariesString()));
  408. importModules.addArray (getCommaOrWhitespaceSeparatedItems (getSharedLibrariesString()));
  409. for (int i = 0; i < importModules.size(); ++i)
  410. out << "$(call import-module," << importModules[i] << ")" << newLine;
  411. }
  412. void writeConfigSettings (OutputStream& out, bool forDebug) const
  413. {
  414. for (ConstConfigIterator config (*this); config.next();)
  415. {
  416. if (config->isDebug() == forDebug)
  417. {
  418. const AndroidBuildConfiguration& androidConfig = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  419. String cppFlags;
  420. cppFlags << createCPPFlags (androidConfig)
  421. << (" " + replacePreprocessorTokens (androidConfig, getExtraCompilerFlagsString()).trim()).trimEnd()
  422. << newLine
  423. << getLDLIBS (androidConfig).trimEnd()
  424. << newLine;
  425. out << " LOCAL_CPPFLAGS += " << cppFlags;
  426. out << " LOCAL_CFLAGS += " << cppFlags;
  427. break;
  428. }
  429. }
  430. }
  431. String getLDLIBS (const AndroidBuildConfiguration& config) const
  432. {
  433. return " LOCAL_LDLIBS :=" + config.getGCCLibraryPathFlags()
  434. + " -llog -lGLESv2 " + getExternalLibraryFlags (config)
  435. + " " + replacePreprocessorTokens (config, getExtraLinkerFlagsString());
  436. }
  437. String createIncludePathFlags (const BuildConfiguration& config) const
  438. {
  439. String flags;
  440. StringArray searchPaths (extraSearchPaths);
  441. searchPaths.addArray (config.getHeaderSearchPaths());
  442. searchPaths = getCleanedStringArray (searchPaths);
  443. for (int i = 0; i < searchPaths.size(); ++i)
  444. flags << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])).quoted();
  445. return flags;
  446. }
  447. String createCPPFlags (const BuildConfiguration& config) const
  448. {
  449. StringPairArray defines;
  450. defines.set ("JUCE_ANDROID", "1");
  451. defines.set ("JUCE_ANDROID_API_VERSION", getMinimumSDKVersionString());
  452. defines.set ("JUCE_ANDROID_ACTIVITY_CLASSNAME", getJNIActivityClassName().replaceCharacter ('/', '_'));
  453. defines.set ("JUCE_ANDROID_ACTIVITY_CLASSPATH", "\\\"" + getJNIActivityClassName() + "\\\"");
  454. String flags ("-fsigned-char -fexceptions -frtti");
  455. if (config.isDebug())
  456. {
  457. flags << " -g";
  458. defines.set ("DEBUG", "1");
  459. defines.set ("_DEBUG", "1");
  460. }
  461. else
  462. {
  463. defines.set ("NDEBUG", "1");
  464. }
  465. flags << createIncludePathFlags (config)
  466. << " -O" << config.getGCCOptimisationFlag();
  467. if (isCPP11Enabled())
  468. flags << " -std=c++11 -std=gnu++11"; // these flags seem to enable slightly different things on gcc, and both seem to be needed
  469. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  470. return flags + createGCCPreprocessorFlags (defines);
  471. }
  472. //==============================================================================
  473. XmlElement* createAntBuildXML() const
  474. {
  475. XmlElement* proj = new XmlElement ("project");
  476. proj->setAttribute ("name", projectName);
  477. proj->setAttribute ("default", "debug");
  478. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "local.properties");
  479. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "project.properties");
  480. {
  481. XmlElement* target = proj->createNewChildElement ("target");
  482. target->setAttribute ("name", "clean");
  483. target->setAttribute ("depends", "android_rules.clean");
  484. target->createNewChildElement ("delete")->setAttribute ("dir", "libs");
  485. target->createNewChildElement ("delete")->setAttribute ("dir", "obj");
  486. XmlElement* executable = target->createNewChildElement ("exec");
  487. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  488. executable->setAttribute ("dir", "${basedir}");
  489. executable->setAttribute ("failonerror", "true");
  490. executable->createNewChildElement ("arg")->setAttribute ("value", "clean");
  491. }
  492. {
  493. XmlElement* target = proj->createNewChildElement ("target");
  494. target->setAttribute ("name", "-pre-build");
  495. addDebugConditionClause (target, "makefileConfig", "Debug", "Release");
  496. addDebugConditionClause (target, "ndkDebugValue", "NDK_DEBUG=1", "NDK_DEBUG=0");
  497. String debugABIs, releaseABIs;
  498. for (ConstConfigIterator config (*this); config.next();)
  499. {
  500. const AndroidBuildConfiguration& androidConfig = dynamic_cast<const AndroidBuildConfiguration&> (*config);
  501. if (config->isDebug())
  502. debugABIs = androidConfig.getArchitectures();
  503. else
  504. releaseABIs = androidConfig.getArchitectures();
  505. }
  506. addDebugConditionClause (target, "app_abis", debugABIs, releaseABIs);
  507. XmlElement* executable = target->createNewChildElement ("exec");
  508. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  509. executable->setAttribute ("dir", "${basedir}");
  510. executable->setAttribute ("failonerror", "true");
  511. executable->createNewChildElement ("arg")->setAttribute ("value", "--jobs=2");
  512. executable->createNewChildElement ("arg")->setAttribute ("value", "CONFIG=${makefileConfig}");
  513. executable->createNewChildElement ("arg")->setAttribute ("value", "${ndkDebugValue}");
  514. executable->createNewChildElement ("arg")->setAttribute ("value", "APP_ABI=${app_abis}");
  515. target->createNewChildElement ("delete")->setAttribute ("file", "${out.final.file}");
  516. target->createNewChildElement ("delete")->setAttribute ("file", "${out.packaged.file}");
  517. }
  518. proj->createNewChildElement ("import")->setAttribute ("file", "${sdk.dir}/tools/ant/build.xml");
  519. return proj;
  520. }
  521. void addDebugConditionClause (XmlElement* target, const String& property,
  522. const String& debugValue, const String& releaseValue) const
  523. {
  524. XmlElement* condition = target->createNewChildElement ("condition");
  525. condition->setAttribute ("property", property);
  526. condition->setAttribute ("value", debugValue);
  527. condition->setAttribute ("else", releaseValue);
  528. XmlElement* equals = condition->createNewChildElement ("equals");
  529. equals->setAttribute ("arg1", "${ant.project.invoked-targets}");
  530. equals->setAttribute ("arg2", "debug");
  531. }
  532. String getAppPlatform() const
  533. {
  534. int ndkVersion = getMinimumSDKVersionString().getIntValue();
  535. if (ndkVersion == 9)
  536. ndkVersion = 10; // (doesn't seem to be a version '9')
  537. return "android-" + String (ndkVersion);
  538. }
  539. void writeProjectPropertiesFile (const File& file) const
  540. {
  541. MemoryOutputStream mo;
  542. mo << "# This file is used to override default values used by the Ant build system." << newLine
  543. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  544. << newLine
  545. << "target=" << getAppPlatform() << newLine
  546. << newLine;
  547. overwriteFileIfDifferentOrThrow (file, mo);
  548. }
  549. void writeLocalPropertiesFile (const File& file) const
  550. {
  551. MemoryOutputStream mo;
  552. mo << "# This file is used to override default values used by the Ant build system." << newLine
  553. << "# It is automatically generated by the Introjucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  554. << newLine
  555. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPathString())) << newLine
  556. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPathString())) << newLine
  557. << "key.store=" << getKeyStoreString() << newLine
  558. << "key.alias=" << getKeyAliasString() << newLine
  559. << "key.store.password=" << getKeyStorePassString() << newLine
  560. << "key.alias.password=" << getKeyAliasPassString() << newLine
  561. << newLine;
  562. overwriteFileIfDifferentOrThrow (file, mo);
  563. }
  564. void writeIcon (const File& file, const Image& im) const
  565. {
  566. if (im.isValid())
  567. {
  568. createDirectoryOrThrow (file.getParentDirectory());
  569. PNGImageFormat png;
  570. MemoryOutputStream mo;
  571. if (! png.writeImageToStream (im, mo))
  572. throw SaveError ("Can't generate Android icon file");
  573. overwriteFileIfDifferentOrThrow (file, mo);
  574. }
  575. }
  576. void writeStringsFile (const File& file) const
  577. {
  578. XmlElement strings ("resources");
  579. XmlElement* resourceName = strings.createNewChildElement ("string");
  580. resourceName->setAttribute ("name", "app_name");
  581. resourceName->addTextElement (projectName);
  582. writeXmlOrThrow (strings, file, "utf-8", 100);
  583. }
  584. //==============================================================================
  585. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter)
  586. };