Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix examples/android code to actually run successfully #443

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/android/.bazelproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
directories:
# Add the directories you want added as source here
# By default, we've added your entire workspace ('.')
.

# Automatically includes all relevant targets under the 'directories' above
derive_targets_from_directories: true

targets:
# If source code isn't resolving, add additional targets that compile it here
//...:all
additional_languages:
kotlin


# Please uncomment an android-SDK platform. Available SDKs are:
android_sdk_platform: android-29
21 changes: 15 additions & 6 deletions examples/android/app/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,41 @@ android_binary(
incremental_dexing = 0,
manifest = "src/main/AndroidManifest.xml",
multidex = "native",
manifest_values = {
"lib_name": "lib",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these depend on the binary name rather than a dependency? This feels closer to how it would actually be used - the libraries have their own manifests. Right now you also still have two binaries with the same package name (app and app3) as they both define this as lib.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well no, this variable is used to inherit the right entry main activity which is currently inherited from the library package name since this binary target doesn't actually have any sources. For both app and app3 the same lib is used so it inherits the same MainActivity which is fine.

I guess the more proper fix which will resemble reality better is to have a unique AndroidManifest.xml per android_binary target which I'm happy to do if you think it's worth it?

},
deps = [
"//lib",
"//libKtAndroid:my_kt",
],
)

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

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

Expand All @@ -44,4 +53,4 @@ build_test(
":app2.apk",
":app3.apk",
],
)
)
20 changes: 18 additions & 2 deletions examples/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="examples.android.app">
<uses-sdk android:minSdkVersion="23"
<uses-sdk
android:minSdkVersion="23"
android:targetSdkVersion="23"
android:maxSdkVersion="29" />
</manifest>

<application
android:label="Rules Kotlin App"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">

<activity
android:name="examples.android.${lib_name}.MainActivity"
android:label="Bazel Tutorial App" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
2 changes: 0 additions & 2 deletions examples/android/lib/src/main/AndroidManifest.xml

This file was deleted.

2 changes: 0 additions & 2 deletions examples/android/lib2/src/main/AndroidManifest.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ 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",
name = "my_android",
srcs = glob(["src/main/java/**/*.java"]),
custom_package = "examples.android.lib2",
manifest = "src/main/AndroidManifest.xml",
visibility = ["//visibility:public"],
resource_files = glob(["res/**"]),
deps = [
":util",
"@maven//:androidx_appcompat_appcompat",
Expand Down
8 changes: 8 additions & 0 deletions examples/android/libAndroid/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 package="examples.android.lib2"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="23"
android:targetSdkVersion="23"
android:maxSdkVersion="29" />
</manifest>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends Activity {
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout parent = new LinearLayout(this);
Expand All @@ -19,7 +19,7 @@ public class MainActivity extends Activity {

new AlertDialog.Builder(this)
.setTitle("Blah")
.setMessage("Blah blah blah?")
.setMessage(R.string.little_bat)
.show();
}
}
22 changes: 22 additions & 0 deletions examples/android/libJava/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@rules_java//java:defs.bzl", "java_plugin")

java_plugin(
name = "autovalue",
generates_api = 1,
processor_class = "com.google.auto.value.processor.AutoValueProcessor",
deps = ["@maven//:com_google_auto_value_auto_value"],
)

java_library(
name = "my_java",
srcs = [
"src/main/java/examples/java/lib/JavaAutoValueProvider.java",
"src/main/java/examples/java/lib/TestJavaValue.java",
],
plugins = [
":autovalue",
],
deps = [
"@maven//:com_google_auto_value_auto_value_annotations",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package examples.java.lib;

public class JavaAutoValueProvider {

public TestJavaValue getAutoValue() {
return new AutoValue_TestJavaValue.Builder().setName("Auto Value Test").build();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package examples.java.lib;

import com.google.auto.value.AutoValue;

@AutoValue
public abstract class TestJavaValue {
abstract String name();

Builder builder() {
return new AutoValue_TestJavaValue.Builder();
}

@AutoValue.Builder
abstract static class Builder {
abstract Builder setName(String name);

abstract TestJavaValue build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ java_plugin(
)

kt_android_library(
name = "lib",
srcs = glob(["src/main/kotlin/**/*.kt"]),
name = "my_kt",
srcs = glob(["src/main/java/**/*.kt"]),
custom_package = "examples.android.lib",
manifest = "src/main/AndroidManifest.xml",
plugins = [
Expand All @@ -29,9 +29,10 @@ kt_android_library(
],
tags = ["trace"],
visibility = ["//visibility:public"],
resource_files = glob(["res/**"]),
deps = [
"@maven//:androidx_appcompat_appcompat",
"@maven//:com_google_auto_value_auto_value_annotations",
"@maven//:org_jetbrains_kotlinx_kotlinx_serialization_runtime",
],
)
)
3 changes: 3 additions & 0 deletions examples/android/libKtAndroid/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="where_you_at">Where you at?</string>
</resources>
8 changes: 8 additions & 0 deletions examples/android/libKtAndroid/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 package="examples.android.lib"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="23"
android:targetSdkVersion="23"
android:maxSdkVersion="29" />
</manifest>
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package examples.android.lib

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

class MainActivity : AppCompatActivity() {

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()
.setTitle(this.getString(R.string.where_you_at))
.setMessage("Blah blah blah?")
.show()
// Ensure Serialization plugin has run and generated code correctly.
Data.serializer()

Expand Down