Skip to content

Commit

Permalink
Stop passing source jars in kt_jvm_import() to the DefaultInfo(files=…
Browse files Browse the repository at this point in the history
…) attribute. (#221)

* Don't include the source jar in the default info's files= attribute, which gets used in places to imply the compile deps, not all the files produced by the rule (such as source jars). This gets fed into the incremental dexing inputs in the android rules, resuling in #208 (which this commit should fix).

* Add in an android application example with a `build_test()` and a CI pipeline to build/test it.  Also normalize examples naming in CI.

* Use the preferred form for kt_jvm_import in the core bits
  • Loading branch information
cgruber authored Oct 28, 2019
1 parent a954e2b commit 1a974a7
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 9 deletions.
12 changes: 9 additions & 3 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
validate_config: 1
bazel: 1.0.0
bazel: 1.1.0
tasks:
ubuntu1604:
test_targets:
Expand All @@ -23,8 +23,14 @@ tasks:
macos:
test_targets:
- "//:all_tests"
examples:
name: Example - Trivial
example-android:
name: "Example - Android"
platform: ubuntu1804
working_directory: examples/android
test_targets:
- //...
examples-trivial:
name: "Example - Trivial"
platform: ubuntu1804
working_directory: examples/trivial
include_json_profile:
Expand Down
4 changes: 3 additions & 1 deletion .bazelignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Some examples have their own WORKSPACE. Ignore as part of this root WORKSPACE so
# we don't break trying to build separate workspaces using wildcards like //...
examples/trivial
# examples/dagger doesn't have its own workspace, so don't do all of examples.
examples/android
examples/node
examples/trivial
68 changes: 68 additions & 0 deletions examples/android/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JVM_EXTERNAL_TAG = "2.8"

RULES_JVM_EXTERNAL_SHA = "79c9850690d7614ecdb72d68394f994fef7534b292c4867ce5e7dec0aa7bdfad"

http_archive(
name = "rules_jvm_external",
sha256 = RULES_JVM_EXTERNAL_SHA,
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"androidx.appcompat:appcompat:1.0.0",
"junit:junit:4.12",
"androidx.test.espresso:espresso-core:3.1.1",
"org.hamcrest:hamcrest-library:1.3",
],
repositories = [
"https://jcenter.bintray.com/",
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_android",
sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
strip_prefix = "rules_android-0.1.1",
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
)

load(
"@build_bazel_rules_android//android:rules.bzl",
"android_ndk_repository",
"android_sdk_repository",
)

android_sdk_repository(name = "androidsdk")

android_ndk_repository(name = "androidndk") # Required. Name *must* be "androidndk".

# Directly load the kotlin rules from the parent repo.
local_repository(
name = "io_bazel_rules_kotlin",
path = "../..",
)

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")

kotlin_repositories()

kt_register_toolchains()

# Skylib, for build_test, so don't bother initializing the unit test infrastructure.
http_archive(
name = "bazel_skylib",
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
urls = [
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
],
)
47 changes: 47 additions & 0 deletions examples/android/app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")
load("@bazel_skylib//rules:build_test.bzl", "build_test")

# An app that consumes android-kt deps
android_binary(
name = "app",
custom_package = "examples.android.app",
incremental_dexing = 0,
manifest = "src/main/AndroidManifest.xml",
multidex = "native",
deps = [
"//lib",
],
)

# An app that consumes jvm-kt libs
android_binary(
name = "app2",
custom_package = "examples.android.app",
incremental_dexing = 0,
manifest = "src/main/AndroidManifest.xml",
multidex = "native",
deps = [
"//lib2",
],
)

# An app that consumes android-kt deps, and does incremental dexing.
android_binary(
name = "app3",
custom_package = "examples.android.app",
incremental_dexing = 1,
manifest = "src/main/AndroidManifest.xml",
multidex = "native",
deps = [
"//lib",
],
)

build_test(
name = "force_build_apks_test",
targets = [
":app.apk",
":app2.apk",
":app3.apk",
],
)
8 changes: 8 additions & 0 deletions examples/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="examples.android.app">
<uses-sdk android:minSdkVersion="23"
android:targetSdkVersion="23"
android:maxSdkVersion="29" />
</manifest>
12 changes: 12 additions & 0 deletions examples/android/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library")

kt_android_library(
name = "lib",
srcs = glob(["src/main/kotlin/**/*.kt"]),
custom_package = "examples.android.lib",
manifest = "src/main/AndroidManifest.xml",
visibility = ["//visibility:public"],
deps = [
"@maven//:androidx_appcompat_appcompat",
],
)
2 changes: 2 additions & 0 deletions examples/android/lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="examples.android.lib"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package examples.android.lib

import android.app.Activity
import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import android.widget.LinearLayout.LayoutParams
import androidx.appcompat.app.AlertDialog

class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val parent = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
}.also { it.addView(Button(this).apply { text = "Foo!" }) }
setContentView(parent, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
AlertDialog.Builder(this)
.setTitle("Blah")
.setMessage("Blah blah blah?")
.show()
}
}
19 changes: 19 additions & 0 deletions examples/android/lib2/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
load("@build_bazel_rules_android//android:rules.bzl", "android_library")

android_library(
name = "lib2",
srcs = glob(["src/main/java/**/*.java"]),
custom_package = "examples.android.lib2",
manifest = "src/main/AndroidManifest.xml",
visibility = ["//visibility:public"],
deps = [
":util",
"@maven//:androidx_appcompat_appcompat",
],
)

kt_jvm_library(
name = "util",
srcs = glob(["src/main/kotlin/**/*.kt"]),
)
2 changes: 2 additions & 0 deletions examples/android/lib2/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="examples.android.lib2"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package examples.android.lib2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import androidx.appcompat.app.AlertDialog;

public class MainActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout parent = new LinearLayout(this);
parent.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
button.setText("Foo!");
parent.addView(button);
setContentView(parent, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

new AlertDialog.Builder(this)
.setTitle("Blah")
.setMessage("Blah blah blah?")
.show();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package examples.android.lib2

class Blargh

fun doStuff() {
println("yay!")
Blargh()
}
3 changes: 1 addition & 2 deletions kotlin/internal/jvm/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,10 @@ def kt_jvm_import_impl(ctx):
jars = [artifact],
),
)
all_files = [artifact.class_jar] + ([artifact.source_jar] if bool(artifact.source_jar) else [])
return struct(
kt = kt_info,
providers = [
DefaultInfo(files = depset(all_files)),
DefaultInfo(files = depset(direct = [artifact.class_jar])),
JavaInfo(
output_jar = artifact.class_jar,
compile_jar = artifact.class_jar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ filegroup(

kt_jvm_import(
name = "annotations",
jars = ["lib/annotations-13.0.jar"],
jar = "lib/annotations-13.0.jar",
neverlink = 1,
)

# Kotlin dependencies that are internal to this repo and are meant to be loaded manually into a classloader.
[
kt_jvm_import(
name = "kotlin-%s" % art,
jars = ["lib/kotlin-%s.jar" % art],
jar = "lib/kotlin-%s.jar" % art,
neverlink = 1,
)
for art in [
Expand All @@ -57,7 +57,7 @@ kt_jvm_import(
[
kt_jvm_import(
name = "kotlin-%s" % art,
jars = ["lib/kotlin-%s.jar" % art],
jar = "lib/kotlin-%s.jar" % art,
srcjar = "lib/kotlin-%s-sources.jar" % art,
visibility = ["//visibility:public"],
)
Expand Down

0 comments on commit 1a974a7

Please sign in to comment.