Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Compiling Binaries & Installing Data with Meson
The next thing we need is a build system. The build system that we're going to be using is called Meson. We already installed the meson
program at the beginning of this book when we installed elementary-sdk
. What we're going to do in this step is create the files that tell Meson how to install your program. This includes all the rules for building your source code as well as correctly installing your icons, .desktop, and metainfo files and the binary app that results from the build process.
Create a new file in your project's root folder called "meson.build". We've included some comments along the way to explain what each section does. You don't have to copy those, but type the rest into that file:
Notice that in each of our install_data
methods, we rename our files using our project name. By using our project name—and its RDNN scheme—we will ensure that our files are installed under a unique name that won't cause conflicts with other apps.
And you're done! Your app now has a real build system. This is a major milestone in your app's development!
Don't forget to add these files to git
and push to GitHub.
Now that we have a build system, let's try it out. Configure the build directory using the Meson command in Terminal:
This command tells Meson to get ready to build our app using the prefix "/usr" and that we want to build our app in a clean directory called "build". The meson setup
command defaults to installing our app locally, but we want to install our app for all users on the computer.
Change into the build directory and use ninja
to build. Then, if the build is successful, install with ninja install
:
If all went well, you should now be able to open your app from the Applications Menu and pin it to the Dock. We'll revisit Meson again later to add some more complicated behavior, but for now this is all you need to know to give your app a proper build system. If you want to explore Meson a little more on your own, you can always check out Meson's documentation.
If you were about to add the "build" folder to your git repository and push it, stop! This binary was built for your computer and we don't want to redistribute it. In fact, we built your app in a separate folder like this so that we can easily delete or ignore the "build" folder and it won't mess up our app's source code.
To uninstall your application, change into the build directory and we will use ninja
once again to uninstall:
If all went well, you should see command output that shows files related to your application were removed. Again, more details can be found in Meson's documentation.
Let's review all we've learned to do:
Create a new Gtk app using Gtk.Window
, Gtk.Button
, and Gtk.Label
Keep our projects organized into branches
License our app under the GPL and declare our app's authors in a standardized manner
Create a .desktop file using RDNN that tells the computer how to display our app in the Applications Menu and the Dock
Set up a Meson build system that contains all the rules for building our app and installing it cleanly
Uninstall our application cleanly
That's a lot! You're well on your way to becoming a bona fide app developer for elementary OS. Give yourself a pat on the back, then take some time to play around with this example. Change the names of files and see if you can still build and install them properly. Ask another developer to clone your repo from GitHub and see if it builds and installs cleanly on their computer. If so, you've just distributed your first app! When you're ready, we'll move onto the next section: Translations.
If you're having trouble, you can view the full example code here on GitHub
The last thing we need for a minimum-viable-app is to provide app icons. Apps on elementary OS provide icons hinted in 6 sizes: 16px, 24px, 32px, 48px, 64px, and 128px. These icons will be shown in many places throughout the system, such as those listed below:
16px
✖️ No
✖️ No
✔️ Yes
✔️ Yes
✔️ Yes
✖️ No
24px
✖️ No
✖️ No
✖️ No
✖️ No
✔️ Yes
✔️ Yes
32px
✖️ No
✔️ Yes
✔️ Yes
✔️ Yes
✔️ Yes
✖️ No
48px
✔️ Yes
✖️ No
✔️ Yes
✖️ No
✔️ Yes
✔️ Yes
64px
✔️ Yes
✔️ Yes
✔️ Yes
✖️ No
✔️ Yes
✖️ No
128px
✔️ Yes
✖️ No
✖️ No
✖️ No
✖️ No
✖️ No
To help you provide the necessary sizes—and for this tutorial—Micah Ilbery maintains an icon template project here on GitHub
Place your icons in the data directory and name them after their pixel sizes, such as32.svg
, 64.svg
, etc. The file structure should look like this:
Now that you have icon files in the data directory, add the following lines to the end of meson.build
to install them .
You'll notice the section for installing app icons is a little more complicated than installing other files. In this example, we're providing SVG icons in all of the required sizes for AppCenter and, since we're using SVG, we're installing them for both LoDPI and HiDPI. If you're providing PNG icons instead, you'll need to tweak this part a bit to handle assets exported for use on HiDPI displays.
If you cannot see your new icon in the Applications Menu or the Dock once you've reinstalled your app, refresh your system's icon cache using the following command:
For more information about creating and hinting icons, check out the Human Interface Guidelines
Launch-able, Build-able, and Shareable
In the previous chapter, we created a "Hello World!" app to show off our Vala and Gtk skills. But what if we wanted to share our new app with a friend? They'd have to know which packages to include with the valac
command we used to build our app, and after they'd built it they'd have to run it from the build directory like we did. Clearly, we need to do some more stuff to make our app fit for people to use, to make it a real app.
To create our first real app, we're going to re-use what we learned in and build on that example.
Create a new project folder, including a new "src" folder and an Application.vala file
Create a Gtk.Application
class with a Gtk.ApplicationWindow
in the activate ()
function.
Add a new Gtk.Label
to the window with the text "Hello Again World!"
Finally, this time we're going to prefix our file with a small legal header. Make sure this header matches the license of your code and assigns copyright to you. More info about this later.
The results should look like this:
Initialize a git branch, add your files to the project, and write a commit message using what you learned in the last chapter. Lastly, make sure you've created a new repository for your project on GitHub push your first revision with git
:
Everything working as expected? Good. Now, let's get our app ready for other people to use.
Next, we need a way to create a listing in AppCenter and a way to tell the interface to show our app in the Applications menu and dock: let's write some Metadata.
Continuous integration testing (also called CI), is a way to automatically ensure that your app builds correctly and has the minimum necessary configuration to run when it's installed. Setting up CI for your app can save you time and give you reassurance that when you submit your app for publishing it will pass the automated testing phase in AppCenter Dashboard. Keep in mind however, that there is also a human review portion of the submission process, so an app that passes CI may still need fixing before being published in AppCenter. Now that you have an app with a build system, metadata files, and packaging, we can configure CI using GitHub Actions.
Navigate to your project's page on GitHub and select the "Actions" tab.
Select "set up a workflow yourself". You'll then be shown the main.yml
file with GitHub's default CI configuration. Replace the default workflow with the following:
Select "Start commit" in the top right corner of the page to commit this workflow to your repository.
That's it! The Flatpak CI workflow will now run on your repository for any new commits or pull requests. You'll be able to see if the build succeeded or failed, and any reasons it might have failed. Configuring this CI workflow will help guide you during the development process and ensure that everything is working as intended.
This workflow will also produce a Flatpak Bundle file using GitHub Artifacts that you can download and install with Sideload. Using this bundle file, you can test feature or bug fix branches with your team, contributors, or community before merging the proposed fix into your main git branch.
List your app in AppCenter and make it accessible from the Applications menu and Dock.
Every app comes with two metadata files that we can generate using an online tool. Open and fill out the form with your app's info:
When you get the section titled "Launchables", make sure to select "Generate a .desktop file for me".
Don't worry about generating Meson snippets, as we'll cover that in the next section. After you select "Generate", you should have two resulting files that you can copy.
First is a MetaInfo file. This file contains all the information needed to list your app in AppCenter. It should look something like this:
In your project's root, create a new folder called "data", and save your MetaInfo to a new file called "hello-again.metainfo.xml".
For the purposes of this tutorial, screenshots are optional, but they are required for publishing in AppCenter. Screenshots should only show your app on a transparent background and not contain any additional text or illustrations. You can use the caption
tag to provide translatable and accessible descriptions of your screenshots.
You can use the built-in Screenshot app on elementary OS with the "grab the current window" option or secondary-click on your app's title area and select "Take Screenshot" to get transparent window-only screenshots of your app
You can also specify a brand color for your app by adding the branding
tag inside the component
tag. Colors must be in hexadecimal, starting with #
. The background will automatically be given a slight gradient in your app's banner.
Remember that AppCenter is a pay-what-you-want store. A suggested price is not a price floor. Users will still be able to choose any price they like, including 0.
Your app must have a release
tag for every version you wish to publish in AppCenter, including the initial release.
This file contains all the information needed to display your app in the Applications Menu and in the Dock. The one generated from AppStream Metainfo Creator looks something like this:
Copy the contents of your Desktop Entry and save it to the data folder you created earlier. Name this new file "hello-again.desktop".
Each time we add a new file or make a significant change it's a good idea to commit a new revision and push to GitHub. Keep in mind that this acts as a backup system as well; when we push our work to GitHub, we know it's safe and we can always revert to a known good revision if we mess up later.
Add all of these files to git
and commit a revision:
Now that we've got all these swanky files laying around, we need a way to tell the computer what to do with them. Ready for the next chapter? Let's do this!
While having a build system is great, our app still isn't ready for regular users. We want to make sure our app can be built and installed without having to use Terminal. What we need to do is package our app. To do this, we use Flatpak on elementary OS. This section will teach you how to package your app as a Flatpak, which is required to publish apps in AppCenter. This will allow people to install your app and even get updates for it when you publish them.
If you want to get really good really fast, you're going to want to practice. Repetition is the best way to commit something to memory. So let's recreate our entire Hello World app again from scratch:
Create a new branch folder "hello-packaging"
Set up our directory structure including the "src" and "data" folders.
Add your Copying, .desktop, .metainfo.xml, icons, and source code.
Now set up the Meson build system and translations.
Test everything!
Did you commit and push to GitHub for each step? Keep up these good habits and let's get to packaging this app!
The Flatpak manifest file describes your app's build dependencies and required permissions. Create a io.github.yourusername.yourrepositoryname.yml
file in your project root with the following contents:
To run a test build and install your app, we can use flatpak-builder
with a few arguments:
This tells Flatpak Builder to build the manifest we just wrote into a clean build
folder the same as we did for Meson. Plus, we install the built Flatpak package locally for our user. If all goes well, congrats! You've just built and installed your app as a Flatpak.
Ninja and Flatpak both provide commands to uninstalling the application. It is recommended to use the provided method in both cases.
To remove our application with Flatpak, we will use Flatpak's remove
command:
Flatpak will prompt you to remove your application.
Note: You can append
-y
to the command to bypass the dialog confirmation prompt
Build "Application.vala" just to make sure it all works. If something goes wrong here, feel free to refer back to and remember to check your terminal output for any hints.
Since we're going to be putting our app out into the wild, we should include some information about who wrote it and the legal usage of its source code. For this we need a new file in our project's root folder: the LICENSE
file. This file contains a copy of the license that your code is released under. For elementary OS apps this is typically the (GPL). Remember the header we added to our source code? That header reminds people that your app is licensed and it belongs to you. GitHub has a built-in way to add several popular licenses to your repository. Read their and add a LICENSE
file to your repository.
If you'd like to better understand software licensing, the Linux Foundation offers a
GitHub Actions can be used to configure many more types of CI or other automation. For more information, check out the .
Below are some of the most important fields for publishing in AppCenter, but there are even more that you can read about
We use the standard to describe sensitive content that may be present in your app so that people using it can be informed and actively consent to seeing that content. OARS data is required and can be generated by taking :
If you want to monetize your app, you will need to add two keys inside a custom
tag inside the component
tag. Suggested prices should be in whole USD. You also must include your app's AppCenter Stripe key. This is a unique public key for each app and is not the same as your Stripe account's regular public key. You can connect your app to Stripe and receive a new key on the .
For more details on available features and advice on writing engaging release notes see .
For more info about crafting .desktop files, check out .
That wasn't too bad, right? We'll set up more complicated packaging in the future, but this is all that is required to submit your app to AppCenter Dashboard for it to be built, packaged, and distributed. If you'd like you can always read .
If you'd like you can always read .