google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.46k stars 2.02k forks source link

Bazel seems to not able to find generated Hilt classes #3787

Closed kenny-liou-ai closed 1 year ago

kenny-liou-ai commented 1 year ago

Hi there,

I'm trying to do a simple dagger with Android activity using Bazel. I've setup everything as the example directory has shown and bazel seems to have a hard time finding the generated Hilt_MainActivity()

@AndroidEntryPoint class expected to extend Hilt_MainActivity. Found: Object [Hilt] Processing did not complete. See error above for details.

Here are my WORKSPACE and BUILD files

WORKSPACE:

workspace(
    name = "example",
)

...

DAGGER_TAG = "2.45"

DAGGER_SHA = "8f55f8b081a72835fed96be66dbfccb95ee2ceff092872397e5e639cccec2114"

http_archive(
    name = "dagger",
    sha256 = DAGGER_SHA,
    strip_prefix = "dagger-dagger-{}".format(DAGGER_TAG),
    urls = ["https://github.com/google/dagger/archive/dagger-{}.zip".format(DAGGER_TAG)],
)

load("@rules_jvm_external//:defs.bzl", "maven_install")
load("@dagger//:workspace_defs.bzl", "DAGGER_ARTIFACTS", "DAGGER_REPOSITORIES", "HILT_ANDROID_ARTIFACTS", "HILT_ANDROID_REPOSITORIES")

maven_install(
    artifacts = DAGGER_ARTIFACTS + HILT_ANDROID_ARTIFACTS + [
        ...
    ],
    fetch_sources = True,
    repositories = DAGGER_REPOSITORIES + HILT_ANDROID_REPOSITORIES + [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
    version_conflict_policy = "pinned",
)

android_sdk_repository(
    name = "androidsdk",
    api_level = 32,
    path = "/opt/android/sdk",
)

BUILD

load("@dagger//:workspace_defs.bzl", "hilt_android_rules")
hilt_android_rules()

kt_android_library(
    name = "app",
    srcs = [
        "MainActivity.kt",
        "MyApplication.kt",
    ],
    manifest = "AndroidManifest.xml",
    resource_files = glob(["res/**"]),
    deps = [
        ":hilt-android",
        "@maven//:androidx_activity_activity_ktx",
        "@maven//:androidx_core_core",
        "@maven//:androidx_core_core_ktx",
        "@maven//:androidx_appcompat_appcompat",
        "@maven//:androidx_compose_material3_material3",
        "@maven//:androidx_constraintlayout_constraintlayout",
        "@maven//:androidx_coordinatorlayout_coordinatorlayout",
        "@maven//:androidx_multidex_multidex",
        "@maven//:javax_inject_javax_inject",
        "@maven//:org_jetbrains_kotlin_kotlin_stdlib",
    ],
)

android_binary(
    name = "example",
    manifest = "AndroidManifest.xml",
    manifest_values = {
        "versionCode": "1",
        "versionName": "1.0",
        "minSdkVersion": "30",
        "targetSdkVersion": "33",
    },
    multidex = "native",
    deps = [
        ":app",
    ],
)

And my MainActivity is as simple as this:

package com.example

import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint(AppCompatActivity::class)
class MainActivity : Hilt_MainActivity() {
}

MyApplication:

package com.example

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp(Application::class)
class MyApplication: Hilt_MyApplication()

Any suggestions?

kenny-liou-ai commented 1 year ago

After reviewing the old issues, I think I'm experiencing https://github.com/bazelbuild/rules_kotlin/issues/514. Will close this.