Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
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 the last chapter 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:
Build "Application.vala" just to make sure it all works. If something goes wrong here, feel free to refer back to the last chapter and remember to check your terminal output for any hints.
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.
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 GNU General Public License (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 documentation for adding software licenses and add a LICENSE
file to your repository.
If you'd like to better understand software licensing, the Linux Foundation offers a free online course on open source licensing
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.
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
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.
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 more about 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
If you'd like you can always read more about Flatpak remove.
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.
GitHub Actions can be used to configure many more types of CI or other automation. For more information, check out the GitHub Actions website.
Now that you've learned about Meson, the next step is to make your app able to be translated to different languages. The first thing you need to know is how to mark strings in your code as translatable. Here's an example:
See the difference? We marked the string as translatable by adding _()
around it. Go back to your project and make all your strings translatable by adding _()
.
Now we have to make some changes to our Meson build system and add a couple new files to describe which files we want to translate and which languages we want to translate into.
Open up your "meson.build" file and add these lines below your project declaration:
Remove the lines that install your .desktop and metainfo files and replace them with the following: