grpc-java icon indicating copy to clipboard operation
grpc-java copied to clipboard

Android Hello World app fails to build even after adding `android.useAndroidX=true`

Open haluzpav opened this issue 1 year ago • 5 comments

What version of gRPC-Java are you using?

1.69.0

What is your environment?

MacOS Sequoia 15.0.1 javac 23.0.1 Android Studio Ladybug Feature Drop | 2024.2.2

What did you expect to see?

Working hello world project on Android

What did you see instead?

Building client app fails with

> Task :app:dexBuilderDebug FAILED
ERROR:/Users/haluzpav/git/grpc-java/examples/android/helloworld/app/build/intermediates/javac/debug/classes/io/grpc/helloworldexample/HelloworldActivity$1.class: D8: java.lang.NullPointerException: Cannot invoke "String.length()" because "<parameter1>" is null
ERROR:/Users/haluzpav/git/grpc-java/examples/android/helloworld/app/build/intermediates/javac/debug/classes/io/grpc/examples/helloworld/GreeterGrpc$GreeterStub.class: D8: java.lang.NullPointerException: Cannot invoke "String.length()" because "<parameter1>" is null
ERROR:/Users/haluzpav/git/grpc-java/examples/android/helloworld/app/build/intermediates/javac/debug/classes/io/grpc/helloworldexample/HelloworldActivity$GrpcTask.class: D8: java.lang.NullPointerException: Cannot invoke "String.length()" because "<parameter1>" is null

Full log: error.log

Steps to reproduce the bug

  1. https://grpc.io/docs/platforms/android/java/quickstart/#run-the-example
  2. Which fails, as described in #11835
  3. Add android.useAndroidX=true into .../helloworld/gradle.properties, as described in the error of the linked issue
  4. Get another error, see above

haluzpav avatar Jan 16 '25 11:01 haluzpav

Potential issue could be build tool incompatibility arising from Android Studio (Ladybug Feature Drop 2024.2.2). Did you try this with lower versions of grpc-java? If not, I'd suggest you to do so and narrow down the root cause.

shivaspeaks avatar Jan 17 '25 10:01 shivaspeaks

I'm no longer interested. I successfully integrated gRPC into my Android project, with help of various other sources.

I suggest maintainers of this repo just recreate this hello word sample app from scratch, using the latest Android app template in Android Studio, as this current one is outdated basically in all aspects. Easier than trying to debug this IMHO.

haluzpav avatar Jan 17 '25 12:01 haluzpav

I also encountered this problem, feel that this problem occurs frequently and is a must-have problem.

zbjlu avatar Apr 03 '25 03:04 zbjlu

Hi @haluzpav, I’m also facing this same issue with the Android Hello World example. You mentioned you got it working using other sources, can you please share how you fixed it? That would really help me.

NaveenPrasannaV avatar Apr 23 '25 00:04 NaveenPrasannaV

@NaveenPrasannaV I can't tell anymore, but here's what I added to my project setup when I was integrating gRPC. It's possibly outdated, or dependent on other things I already had in the project. Hope it helps.

Root build.gradle.kts:

plugins {
    // ...
    id("com.google.protobuf") version "0.9.4" apply false
}

app/build.gradle.kts:

val protobufVersion = "4.29.3"
val grpcVersion = "1.69.0"
val grpcKotlinVersion = "1.4.1"

plugins {
    // ...
    id("com.google.protobuf")
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:$protobufVersion"
    }
    plugins {
        create("grpc") {
            artifact = "io.grpc:protoc-gen-grpc-java:$grpcVersion"
        }
        create("grpckt") {
            artifact = "io.grpc:protoc-gen-grpc-kotlin:$grpcKotlinVersion:jdk8@jar"
        }
    }
    generateProtoTasks {
        all().forEach { task ->
            task.builtins {
                create("java") {
                    option("lite")
                }
                create("kotlin") {
                    option("lite")
                }
            }
            task.plugins {
                create("grpc") {
                    option("lite")
                }
                create("grpckt") {
                    option("lite")
                }
            }
        }
    }
}

dependencies {
    implementation("io.grpc:grpc-okhttp:$grpcVersion")
    implementation("io.grpc:grpc-protobuf-lite:$grpcVersion")
    implementation("com.google.protobuf:protobuf-kotlin-lite:$protobufVersion")
    implementation("io.grpc:grpc-stub:$grpcVersion")
    implementation("io.grpc:grpc-kotlin-stub:$grpcKotlinVersion")
    compileOnly("org.apache.tomcat:annotations-api:6.0.53") // needed for some code generated by Protobuf
}

haluzpav avatar Apr 23 '25 08:04 haluzpav

hi @haluzpav @shivaspeaks I encountered the same DexWorkAction / D8 error (Execution failed for task ':app:dexBuilderDebug') when building with Java 23. switching to Java 17 resolved this because it generates bytecode version v61, which is supported by D8. The 'dexBuilderDebug' failure occurs when encounter unsupported bytecode versions(v65/v67 from java 21/23). AGP 7.x’s D8 supports bytecode up to v61, so Java 21+ (v65+) fails. AGP 8.1.x+, D8 supports v65 (Java 21) but not v67(java 23) as of all current versions. To enable compatibility, always ensure all three elements are aligned: ensuring compatibility between your AGP version, Java version, and build settings. this can resolve the "DexWorkAction / D8" error and successfully build the project.

Sangamesh1997 avatar Jul 09 '25 03:07 Sangamesh1997