{"id":1154,"date":"2023-12-14T18:00:47","date_gmt":"2023-12-14T17:00:47","guid":{"rendered":"http:\/\/blogs.igalia.com\/jaragunde\/?p=1154"},"modified":"2023-12-14T16:47:55","modified_gmt":"2023-12-14T15:47:55","slug":"setting-up-a-minimal-command-line-android-emulator-on-linux","status":"publish","type":"post","link":"https:\/\/blogs.igalia.com\/jaragunde\/2023\/12\/setting-up-a-minimal-command-line-android-emulator-on-linux\/","title":{"rendered":"Setting up a minimal, command-line Android emulator on Linux"},"content":{"rendered":"<p>Android has all kinds of nice development tools, but sometimes you just want to run an apk and don&#8217;t need all the surrounding tooling. In my case, I have already have my Chromium setup, which can produce binaries for several platforms <a href=\"https:\/\/source.chromium.org\/chromium\/chromium\/src\/+\/main:docs\/android_build_instructions.md\">including Android<\/a>.<\/p>\n<p>I usually test on a physical device, a smartphone, but I would like to try a device with a tablet form factor and I don&#8217;t have one at hand.<\/p>\n<div id=\"attachment_1162\" style=\"width: 594px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blogs.igalia.com\/jaragunde\/files\/2023\/12\/chromium_android_screenshots.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1162\" src=\"http:\/\/blogs.igalia.com\/jaragunde\/files\/2023\/12\/chromium_android_screenshots-1024x535.jpg\" alt=\"Chromium smartphone and tablet screenshots\" width=\"584\" height=\"305\" class=\"size-large wp-image-1162\" srcset=\"https:\/\/blogs.igalia.com\/jaragunde\/files\/2023\/12\/chromium_android_screenshots-1024x535.jpg 1024w, https:\/\/blogs.igalia.com\/jaragunde\/files\/2023\/12\/chromium_android_screenshots-300x157.jpg 300w, https:\/\/blogs.igalia.com\/jaragunde\/files\/2023\/12\/chromium_android_screenshots-768x401.jpg 768w, https:\/\/blogs.igalia.com\/jaragunde\/files\/2023\/12\/chromium_android_screenshots-500x261.jpg 500w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><p id=\"caption-attachment-1162\" class=\"wp-caption-text\">Chromium provides different user experiences for smartphones and tablets.<\/p><\/div>\n<p>I&#8217;ve set up the most stripped-down environment possible to run an Android emulator using the same tools provided by the platform. Notice these are generic instructions, not tied to Chromium tooling, despite I&#8217;m doing this mainly to run Chromium.<\/p>\n<p>The first step is to download the latest version of the <strong>command line tools<\/strong>, instead of Android Studio, from the <a href=\"https:\/\/developer.android.com\/studio#command-tools\">Android developer website<\/a>. We will extract the tools to the location <code>~\/Android\/Sdk<\/code>, in the path where they like to find themselves:<\/p>\n<pre><code class=\"shell\">mkdir -p ~\/Android\/Sdk\/cmdline-tools\nunzip -d ~\/Android\/Sdk\/cmdline-tools ~\/Downloads\/commandlinetools-linux-10406996_latest.zip \nmv ~\/Android\/Sdk\/cmdline-tools\/cmdline-tools\/ ~\/Android\/Sdk\/cmdline-tools\/latest\n<\/code><\/pre>\n<p>Now we have the tools installed in <code>~\/Android\/Sdk\/cmdline-tools\/latest<\/code>, we need to <strong>add their binaries to the path<\/strong>. We will also add other paths for tools we are about to install, with the command:<\/p>\n<pre><code class=\"shell\">export PATH=~\/Android\/Sdk\/cmdline-tools\/latest\/bin:~\/Android\/Sdk\/emulator:~\/Android\/Sdk\/platform-tools:$PATH\n<\/code><\/pre>\n<p>Run the command above for a one-time use, or add it to <code>~\/.bashrc<\/code> or your preferred shell configuration for future use.<\/p>\n<p>You will also need to setup another envvar:<\/p>\n<pre><code class=\"shell\">export ANDROID_HOME=~\/Android\/Sdk\n<\/code><\/pre>\n<p>Again, run it once for a one-time use or add it to your shell configuration in <code>~\/.bashrc<\/code> or equivalent.<\/p>\n<p>Now we use the tool <code>sdkmanager<\/code>, which came with Android&#8217;s command-line tools, to <strong>install the rest of the software<\/strong> we need. All of this will be installed inside your <code>$ANDROID_HOME<\/code>, so in <code>~\/Android\/Sdk<\/code>.<\/p>\n<pre><code class=\"shell\">sdkmanager \"emulator\" \"platform-tools\" \"platforms;android-29\" \"system-images;android-29;google_apis;x86_64\"\n<\/code><\/pre>\n<p>In the command above I have installed the emulator, platform tools (<code>adb<\/code> and friends), and system libraries and a system image for Android 10 (API level 29). You may check what other things are available with <code>sdkmanager --list<\/code>, you will find multiple variants of the Android platform and system images.<\/p>\n<p>Finally, we have to <strong>setup a virtual machine<\/strong>, called AVD (&#8220;Android Virtual Device&#8221;) in Android jargon. We do that with:<\/p>\n<pre><code class=\"shell\">avdmanager -v create avd -n Android_API_29_Google -k \"system-images;android-29;google_apis;x86_64\" -d pixel_c\n<\/code><\/pre>\n<p>Here I have created a virtual device called &#8220;Android_API_29_Google&#8221; with the system image I had downloaded, and the form factor and screen size of a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pixel_C\">Pixel C<\/a>, which is a tablet. You may get a list of all the devices that can be simulated with <code>avdmanager list device<\/code>.<\/p>\n<p>Now we can already <strong>start an emulator<\/strong> running the AVD with the name we have just chosen, with:<\/p>\n<pre><code class=\"shell\">emulator -avd Android_API_29_Google\n<\/code><\/pre>\n<p>The files and configuration for this AVD are stored in <code>~\/.android\/avd\/Android_API_29_Google.avd<\/code>, or a sibling directory if you used a different name. <strong>Configuration<\/strong> is in the <code>config.ini<\/code> file, you can set here many options you would normally configure from Android Studio, and <a href=\"https:\/\/learn.microsoft.com\/en-us\/xamarin\/android\/get-started\/installation\/android-emulator\/device-properties\">even more<\/a>. I recommend to change at least this value, for your convenience:<\/p>\n<pre><code>hw.keyboard = yes\n<\/code><\/pre>\n<p>Otherwise you will find yourself typing URLs with a mouse on a virtual keyboard&#8230; Not fun.<\/p>\n<p>Finally, your software will be the up-to-date after a fresh install but, when new versions are released, you will be able to install them with:<\/p>\n<pre><code class=\"shell\">sdkmanager --update\n<\/code><\/pre>\n<p>Make sure to run this every now and then!<\/p>\n<p>At this point, your setup should be ready, and all the tools you need are in the PATH. Feel free to reuse the commands above to create any number of virtual devices with different Android system images, different form factors&#8230; Happy hacking!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android has all kinds of nice development tools, but sometimes you just want to run an apk and don&#8217;t need all the surrounding tooling. In my case, I have already have my Chromium setup, which can produce binaries for several &hellip; <a href=\"https:\/\/blogs.igalia.com\/jaragunde\/2023\/12\/setting-up-a-minimal-command-line-android-emulator-on-linux\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,23,24,3],"tags":[],"class_list":["post-1154","post","type-post","status-publish","format-standard","hentry","category-android","category-browsers","category-chromium","category-igalia"],"_links":{"self":[{"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/posts\/1154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/comments?post=1154"}],"version-history":[{"count":13,"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/posts\/1154\/revisions"}],"predecessor-version":[{"id":1170,"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/posts\/1154\/revisions\/1170"}],"wp:attachment":[{"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/media?parent=1154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/categories?post=1154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.igalia.com\/jaragunde\/wp-json\/wp\/v2\/tags?post=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}