📜 ⬆️ ⬇️

Scala Application Development

In this small post I will try to tell you where to start developing Scala applications for Android using SBT and IntelliJ Idea, as well as the pros and cons of this approach. Everything will be quite simple, you just have to be patient. The whole process is described for unix like systems, it was not checked on Windows, but if you really want to, you can try to do the same through cygwin. So…

Existing Solutions


With a cursory googling, you can stumble upon these three ( one , two , three ) wonderful articles that gradually describe a long and thorny path to enlightenment, namely how to raise the SBT project for Android in Idea. This method is outdated, because the article describes the process for SBT 0.7.4, as well as many unnecessary steps that only repel further study of the material. Despite this, I recommend reading them to get a basic idea of ​​what you are dealing with.

Many people know that under Eclipse there is also a plugin for development on Scala, but it is in perpetual beta and completely useless due to the constant brakes and scarcity of functionality.
')

Instruments






SBT installation


You must first download the latest version of sbt-launch.jar and put it in the ~ / bin folder, then create an empty file named sbt with the following contents

java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@" 

and also put it in the ~ / bin folder. Next, you need to make the sbt file executable and add the ~ / bin folder to the $ PATH variable in order to access its executable files without explicitly specifying the path:

 $ chmod u+x ~/bin/sbt $ echo 'PATH=$PATH:~/bin' >> ~/.bashrc 

After that, restart the terminal so that it rereads the ~ / .bashrc file.

Principle of operation


To create a project using giter8 (g8). The principle of its operation is very simple - it downloads a previously prepared project template from github and substitutes the properties specified in the default.properties file instead of the specified variables. This is true for both the contents of the files and their names. For example, in this code

 class $main_activity$ extends Activity { ... } 

$ main_activity $ will be replaced with the corresponding specified property. All property values ​​are set interactively via g8. Based on this, a template was created that includes standard for Idea project files and modules, in which at the stage of project generation, specific properties are substituted for it.

Project generation


First install giter8. The simplest solution is to run my little script that will do it by itself:

 curl https://raw.github.com/mbektimirov/android-app-idea.g8/master/install.sh | sh 

If everything goes well, then g8 will be installed in the ~ / bin folder. For possible problems, see the end of the article.
The next step is actually creating a project from a template. There are no difficulties, during the installation process g8 will request properties. If the default values ​​suit you (they are indicated in square brackets), then they can be omitted.

 mbektimirov@mbektimirov-pc:~$ g8 mbektimirov/android-app-idea Template for Android apps in Scala package [my.android.project]: ru.my.first.android.scala.project name [My Android Project]: my super project main_activity [MainActivity]: MyActivity api_level [10]: 8 project_scala_version [2.9.1]: Applied mbektimirov/android-app-idea.g8 in my-super-project 

After that, you can see the generated project in the folder my-super-project, which can already be deployed to the device. This can be done using the sbt start-device command.

IDE Setup


After you have downloaded and installed Idea, you need to add some plugins to it, namely to support Scala and SBT, so to speak, without departing from the cash register, so that you do not have to switch to the console each time and build / deploy the project from there. Plugins are available from the Idea itself in the plugin manager:



After installing the plugins, restart the IDE, then in the settings go to the SBT section and specify the path to sbt-launch.jar:



After these actions, you can upload our generated project to the IDE through “File -> Open project ...”.
Almost everything is ready, it remains to configure the configuration to run the project using SBT. To do this, select the item “Edit configurations” in the “Run” menu. In the window that appears, you need to create a new configuration, in our case it will be an Android Application, and call it StartDevice. In the settings that appear, select the “android-module” module, uncheck the “make” checkbox and check “Run SBT Action” opposite. In the SBT Action, in turn, add the “start-device” task. The screenshot below explains all the steps above:



Now the project can be launched by a green button or hot key. All logs will be written to the SBT console, which is part of the SBT plugin for Idea:





Known Issues




What to read


Source: https://habr.com/ru/post/128511/


All Articles