Skip to content

Commit

Permalink
Fix examples/android code to actually run successfully
Browse files Browse the repository at this point in the history
- Add a manifest with a main activity to launch
- Use ``${lib_name}`` manifest variable in order to reuse the same manifest from multiple libs and inherit the right `MainActivity` from the right lib
- Define `resource_files` attr to actually include the resource dir. Modify the code to actually use the resources.
- Fixed the code to extend from `AppCompatActivity` in order to actually use `androidx.appcompat` and not crash on launch
- Renamed `lib` target to `my_kt` and `lib2` to `my_android` in order to make it easier to distinguish between the generated output files during debugging.
- Added `my_java` target to help with debugging when comparing plugin generation for java vs native android vs kotlin
- Added `.bazelproject` file for IDE
  • Loading branch information
nkorostelev-sc authored and nkoroste committed Jan 21, 2021
1 parent 35cb119 commit 2ec60f9
Show file tree
Hide file tree
Showing 25 changed files with 135 additions and 25 deletions.
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",
},
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

0 comments on commit 2ec60f9

Please sign in to comment.