robolectric icon indicating copy to clipboard operation
robolectric copied to clipboard

java.lang.NoClassDefFoundError: android/bluetooth/le/BluetoothLeAdvertiser

Open luoqii opened this issue 2 years ago • 2 comments

when i write android hilt demo (https://developer.android.com/training/dependency-injection/hilt-testing#testing-dependencies), in app/build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'

    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.kilt_kotlin"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }

    lintOptions{
        ignore("SecDev_Quality_03_2")
    }
}

dependencies {
    implementation "com.google.dagger:hilt-android:2.38.1"
    kapt "com.google.dagger:hilt-compiler:2.38.1"

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'

    //https://developer.android.com/training/dependency-injection/hilt-testing#kotlin
    // For Robolectric tests.
    testImplementation("com.google.dagger:hilt-android-testing:2.44")
    // ...with Kotlin.
    kaptTest("com.google.dagger:hilt-android-compiler:2.44")

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'

    testImplementation "org.robolectric:robolectric:4.11.1"
    testImplementation 'androidx.test.ext:junit:1.1.3'

    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

// Allow references to generated code
kapt {
    correctErrorTypes true
}

in build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
//https://developer.android.com/training/dependency-injection/hilt-android
buildscript {

    dependencies {

        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
    }
}

plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'com.android.library' version '7.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

when run unittest , it says:

java.lang.NoClassDefFoundError: android/bluetooth/le/BluetoothLeAdvertiser
	at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166)
	at java.base/java.lang.Class.privateGetPublicMethods(Class.java:3191)
	at java.base/java.lang.Class.getMethods(Class.java:1904)
	at org.robolectric.util.reflector.ReflectorClassWriter.write(ReflectorClassWriter.java:125)
	at org.robolectric.util.reflector.Reflector.getBytecode(Reflector.java:165)
	at org.robolectric.util.reflector.Reflector.createReflectorClass(Reflector.java:119)
	at org.robolectric.util.reflector.Reflector.lambda$reflector$0(Reflector.java:74)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
	at org.robolectric.util.reflector.Reflector.reflector(Reflector.java:72)
	at org.robolectric.util.reflector.Reflector.reflector(Reflector.java:50)
	at org.robolectric.shadows.ShadowBluetoothAdapter.reset(ShadowBluetoothAdapter.java:114)
	at org.robolectric.Shadows.reset(Shadows.java:2736)
	at org.robolectric.android.internal.AndroidTestEnvironment.resetState(AndroidTestEnvironment.java:667)
	at org.robolectric.RobolectricTestRunner.lambda$finallyAfterTest$0(RobolectricTestRunner.java:370)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:86)
	at org.robolectric.RobolectricTestRunner.finallyAfterTest(RobolectricTestRunner.java:368)
	at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$2(SandboxTestRunner.java:298)
	at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:101)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassNotFoundException: couldn't load android.bluetooth.le.BluetoothLeAdvertiser
	at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:164)
	at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$loadClass$0(SandboxClassLoader.java:136)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
	at org.robolectric.internal.bytecode.SandboxClassLoader.loadClass(SandboxClassLoader.java:136)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 23 more
Caused by: java.lang.RuntimeException: failed to instrument android.bluetooth.le.BluetoothLeAdvertiser
	at org.robolectric.internal.bytecode.ClassInstrumentor.instrument(ClassInstrumentor.java:188)
	at org.robolectric.internal.bytecode.ClassInstrumentor.instrumentToBytes(ClassInstrumentor.java:113)
	at org.robolectric.internal.bytecode.ClassInstrumentor.lambda$instrument$1(ClassInstrumentor.java:140)
	at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
	at org.robolectric.internal.bytecode.ClassInstrumentor.instrument(ClassInstrumentor.java:140)
	at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:156)
	... 27 more
Caused by: java.lang.RuntimeException: huh? <init>()V
	at org.robolectric.internal.bytecode.ClassInstrumentor.extractCallToSuperConstructor(ClassInstrumentor.java:490)
	at org.robolectric.internal.bytecode.ClassInstrumentor.instrumentConstructor(ClassInstrumentor.java:387)
	at org.robolectric.internal.bytecode.ClassInstrumentor.instrumentMethods(ClassInstrumentor.java:285)
	at org.robolectric.internal.bytecode.ClassInstrumentor.instrument(ClassInstrumentor.java:161)
	... 32 more


but actually, i do NOT use any bt-related class .

Robolectric & Android Version

robolectric:4.11.1

luoqii avatar Dec 14 '23 06:12 luoqii

This is a very strange issue. I am not sure what is going on here. Can you please upload the entire sample app to GitHub? If it is reproducible, it is the only way to debug it.

You may want to do rm -rf $HOME/.m2/repository/org/robolectric/android-all rm -rf $HOME/.m2/repository/org/robolectric/android-all-instrumented

in case the android-all jars got corrupt.

hoisie avatar Dec 15 '23 17:12 hoisie

Robolectric 4.13 includes some changes related to BluetoothLeAdvertiser . Can you try with that version?

MGaetan89 avatar Jul 08 '24 06:07 MGaetan89