Skip to content

Instantly share code, notes, and snippets.

@izzyleung
Created May 23, 2018 19:54
Show Gist options
  • Save izzyleung/a1a1fab771e15d423a7d605a74620bda to your computer and use it in GitHub Desktop.
Save izzyleung/a1a1fab771e15d423a7d605a74620bda to your computer and use it in GitHub Desktop.
Example of how to use AutoValue in a Bazel Android Project

Example project on how to integrate AutoValue in a Bazel Android Project

  • Building the projcet:
    • bazel build :app
  • Launch the App in your mobile device or Emulator
    • bazel mobile-install :app
  • Verify the output in LogCat
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="1"
android:targetSdkVersion="27" />
<application
android:label="AutoValue Bazel Android Example">
<activity
android:name=".MainActivity"
android:label="Main Activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
java_plugin(
name = "autovalue_plugin",
processor_class = "com.google.auto.value.processor.AutoValueProcessor",
deps = [
"@autovalue//jar",
],
)
java_library(
name = "autovalue",
exported_plugins = [
":autovalue_plugin",
],
neverlink = 1,
exports = [
"@autovalue//jar",
],
)
android_library(
name = "lib",
srcs = [
"Model.java",
],
deps = [
":autovalue",
],
)
android_binary(
name = "app",
custom_package = "org.example",
srcs = [
"MainActivity.java",
],
manifest = "AndroidManifest.xml",
deps = [
":autovalue",
":lib",
],
)
package org.example;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Model model = Model.create(42);
Log.v("ANSWER", "The Answer is " + model.field());
}
}
package org.example;
import com.google.auto.value.AutoValue;
@AutoValue
public abstract class Model {
abstract int field();
public static Model create(int field) {
return new AutoValue_Model(field);
}
}
workspace(name = "AutoValue_Bazel_Android_Example")
API_LEVEL = 27
BUILD_TOOLS_VERSION = "27.0.2"
android_sdk_repository(
name = "androidsdk",
api_level = API_LEVEL,
build_tools_version = BUILD_TOOLS_VERSION,
)
maven_jar(
name = "autovalue",
artifact = "com.google.auto.value:auto-value:1.5.3",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment