Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Added a kotlin tutorial for android (#2525)
Browse files Browse the repository at this point in the history
* Added a kotlin tutorial for android

* Corrected errors in kotlin tutorial

Co-authored-by: v-jizhang <66389669+buck-bot@users.noreply.github.com>
  • Loading branch information
Jinlin Zhang and buck-bot authored Aug 31, 2020
1 parent 6ce4daa commit 2c78fcb
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 6 deletions.
179 changes: 174 additions & 5 deletions docs/learning/tutorial.soy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/***/
{template .soyweb}
{call buck.page}
{param title: 'Tutorial' /}
{param title: 'Tutorials' /}
{param navid: 'learning_tutorial' /}
{param prettify: true /}
{param description}
Expand All @@ -41,6 +41,32 @@
</blockquote>
</p>

<div class="toggler">
<table>
<tr>
<td><span><strong>Platform:</strong></span></td>
<td>
<a href="javascript:void(0);" class="button-android" onclick="display('platform', 'android')">Android</a>
</td>
</tr>
<tr>
<td><span><strong>Development OS:</strong></span></td>
<td>
<a href="javascript:void(0);" class="button-mac" onclick="display('os', 'mac')">macOS</a>
<a href="javascript:void(0);" class="button-linux" onclick="display('os', 'linux')">Linux</a>
</td>
</tr>
<tr>
<td><span><strong>Language:</strong></span></td>
<td>
<a href="javascript:void(0);" class="button-java" onclick="display('language', 'java')">Java</a>
<a href="javascript:void(0);" class="button-kotlin" onclick="display('language', 'kotlin')">Kotlin</a>
</td>
</tr>
</table>
</div>

<span><block class="mac linux android java kotlin" /></span>
<h2>Path Setup</h2>

<p>
Expand All @@ -54,7 +80,6 @@ sudo ln -s ${PWD}/bin/buckd /usr/bin/buckd
</pre>
{/literal}


<h2>Create Project</h2>

<p>
Expand All @@ -75,13 +100,14 @@ cd ~/my-first-buck-project/
<h2>Compile Your Code</h2>

<p>
Android applications are typically written in Java, so the first thing we will do is configure Buck to compile Java code against the Android API. To do so, Buck needs to know where your Android SDK is. Assuming that your Android SDK is installed in <code>~/android-sdk</code>, run the following command to set a <code>ANDROID_SDK</code> environment variable that tells Buck where to find your Android SDK:
Android applications are typically written in Javai and kotlin, so the first thing we will do is to configure Buck to compile code against the Android API. To do so, Buck needs to know where your Android SDK is. Assuming that your Android SDK is installed in <code>~/android-sdk</code>, run the following command to set a <code>ANDROID_SDK</code> environment variable that tells Buck where to find your Android SDK:
</p>

{literal}<pre>
export ANDROID_SDK=$HOME/android-sdk
</pre>{/literal}

<span><block class="mac linux android java" /></span>
<p>
Now that Buck can locate your Android SDK, it is time to compile some Java code. First, we create a simple <code>Activity</code> at <code>java/com/example/activity/MyFirstActivity.java</code>:
</p>
Expand Down Expand Up @@ -126,6 +152,52 @@ echo "android_library(
</blockquote>
</p>

<span><block class="mac linux android kotlin" /></span>
<p>
Now that Buck can locate your Android SDK, it is time to compile some kotlin code. First, we create a simple <code>Activity</code> at <code>kotlin/com/example/activity/MyFirstActivity.kt</code>:
</p>

{literal}<pre>
mkdir -p kotlin/com/example/activity/
echo "package com.example.activity

import android.app.Activity
import android.os.Bundle

class MyFirstActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}" > kotlin/com/example/activity/MyFirstActivity.kt
</pre>{/literal}

<p>
Now we need a build file that defines a build rule to compile this kotlin code, so we create an <code><a href="rule/android_library.html">android_library()</a></code> rule in <code>kotlin/com/example/activity/BUCK</code>:
</p>

{literal}<pre>
echo "android_library(
name = 'activity',
srcs = glob(['*.kt']),
language = "KOTLIN",
visibility = [ 'PUBLIC' ],
)" > kotlin/com/example/activity/BUCK
</pre>{/literal}

<p>
Now we can compile our Java code using Buck:
</p>

<pre>buck build{sp}//kotlin/com/example/activity:activity</pre>

<p>
<blockquote>
Buck generates its output in the <code>buck-out</code> directory, so this is a good time to specify <code>buck-out</code> as something that should be ignored by your version control system.
</blockquote>
</p>

<span><block class="mac linux android java kotlin" /></span>
<h2>Package Resources</h2>

<p>
Expand Down Expand Up @@ -229,8 +301,8 @@ echo "android_binary(
manifest_entries = {
'version_code': 1,
'version_name': '1.0',
'min_sdk_version': 16,
'target_sdk_version': 23
'min_sdk_version': 26,
'target_sdk_version': 29
},
keystore = ':debug_keystore',
deps = [
Expand Down Expand Up @@ -279,6 +351,7 @@ echo "[alias]

<pre>buck install app</pre>

<span><block class="mac linux android java" /></span>
<h2>Create an IntelliJ Project</h2>

<p>
Expand Down Expand Up @@ -324,4 +397,100 @@ echo "/.buckd

{/param}
{/call}

{literal}
<script>
// Convert <div>...<span><block /></span>...</div>
// Into <div>...<block />...</div>
var blocks = document.getElementsByTagName('block');
for (var i = 0; i < blocks.length; ++i) {
var block = blocks[i];
var span = blocks[i].parentNode;
var container = span.parentNode;
container.insertBefore(block, span);
container.removeChild(span);
}
// Convert <div>...<block />content<block />...</div>
// Into <div>...<block>content</block><block />...</div>
blocks = document.getElementsByTagName('block');
for (var i = 0; i < blocks.length; ++i) {
var block = blocks[i];
while (block.nextSibling && block.nextSibling.tagName !== 'BLOCK') {
block.appendChild(block.nextSibling);
}
}
function display(type, value) {
var container = document.getElementsByTagName('block')[0].parentNode;
container.className = 'disp-' + type + '-' + value + ' ' +
container.className.replace(RegExp('disp-' + type + '-[a-z]+ ?'), '');
}

// If we are coming to the page with a hash in it (i.e. from a search, for example), try to get
// us as close as possible to the correct platform and dev os using the hashtag and block walk up.
var foundHash = false;
if (window.location.hash !== '' && window.location.hash !== 'content') { // content is default
var hashLinks = document.querySelectorAll('a.hash-link');
for (var i = 0; i < hashLinks.length && !foundHash; ++i) {
if (hashLinks[i].hash === window.location.hash) {
var parent = hashLinks[i].parentElement;
while (parent) {
if (parent.tagName === 'BLOCK') {
var devOS = null;
var targetPlatform = null;
var languageToUse = null;
// Could be more than one target os and dev platform, but just choose some sort of order
// of priority here.

// Dev OS
if (parent.className.indexOf('mac') > -1) {
devOS = 'mac';
} else if (parent.className.indexOf('linux') > -1) {
devOS = 'linux';
} else if (parent.className.indexOf('windows') > -1) {
devOS = 'windows';
} else {
break; // assume we don't have anything.
}

// Target Platform
if (parent.className.indexOf('ios') > -1) {
targetPlatform = 'ios';
} else if (parent.className.indexOf('android') > -1) {
targetPlatform = 'android';
} else {
break; // assume we don't have anything.
}

// Language
if (parent.className.indexOf('java') > -1) {
languageToUse = 'java';
} else if (parent.className.indexOf('kotlin') > -1) {
languageToUse = 'kotlin';
} else {
break;
}

// We would have broken out if both targetPlatform and devOS hadn't been filled.
display('os', devOS);
display('platform', targetPlatform);
display('language', languageToUse);
foundHash = true;
break;
}
parent = parent.parentElement;
}
}
}
}
// Do the default if there is no matching hash
if (!foundHash) {
var isMac = navigator.platform.toLowerCase().indexOf('mac') !== -1;
var isLinux = navigator.platform.toLowerCase().indexOf('linux') !== -1;
display('os', isMac ? 'mac' : (isLinux ? 'linux' : 'windows'));
display('platform', 'android');
display('language', 'java');
}
</script>
{/literal}

{/template}
13 changes: 12 additions & 1 deletion docs/static/buck.css
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,14 @@ footer {
.display-platform-ios .toggler .button-ios,
.display-platform-android .toggler .button-android,
.display-platform-other .toggler .button-other,
.display-platform-java .toggler .button-java {
.display-platform-java .toggler .button-java,
.disp-os-mac .toggler .button-mac,
.disp-os-linux .toggler .button-linux,
.disp-os-windows .toggler .button-windows,
.disp-platform-ios .toggler .button-ios,
.disp-platform-android .toggler .button-android,
.disp-language-java .toggler .button-java,
.disp-language-kotlin .toggler .button-kotlin {
background-color: #05A5D1;
color: white;
}
Expand All @@ -561,6 +568,10 @@ block {
display: none;
}

.disp-platform-android.disp-os-mac.disp-language-java .android.mac.java,
.disp-platform-android.disp-os-linux.disp-language-java .android.linux.java,
.disp-platform-android.disp-os-mac.disp-language-kotlin .android.mac.kotlin,
.disp-platform-android.disp-os-linux.disp-language-kotlin .android.linux.kotlin,
.display-platform-ios.display-os-mac .ios.mac,
.display-platform-ios.display-os-linux .ios.linux,
.display-platform-ios.display-os-windows .ios.windows,
Expand Down

0 comments on commit 2c78fcb

Please sign in to comment.