问题
I'm not able to use Kafka producer in Android app. I'm getting the below exception.,
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.management.DynamicMBean" on path: DexPathList[[dex file "/data/data/com.a.kafkaproducer/files/instant-run/dex/slice-zookeeper-3.4.6_8a38f89467d40a18afbd8baad7b29088157e6083-classes.dex", dex file "/data/data/com.a.kafkaproducer/files/instant-run/dex/slice-zkclient-0.8_e811ffc59b2b86f143b12056ea3479df3f628d5a-classes.dex", dex file "/data/data/com.a.kafkaproducer/files/instant-run/dex/slice-support-annotations-25.1.0_c6c37b5425fad5a782cbe7db2279266099bc29a9-classes.dex", dex file "/data/data/com.a.kafkaproducer/files/instant-run/dex/slice-snappy-java-1.1.2.6_848c76c3e0ca3a9ed4d0c95a6eeba1e11cd43625-classes.dex", dex file "/data/data/com.a.kafkaproducer/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.a.kafkaproducer/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.a.kafkaproducer/files/instant-run/dex/slice-slice_7-classes.dex", dex file
`at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
I/art: at java.lang.Class
com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(java.lang.String) (IncrementalClassLoader.java:90)
I/art: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
I/art: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
I/art: at java.lang.Class dalvik.system.DexFile.defineClassNative(java.lang.String, java.lang.ClassLoader, java.lang.Object, dalvik.system.DexFile) (DexFile.java:-2)
I/art: at java.lang.Class dalvik.system.DexFile.defineClass(java.lang.String, java.lang.ClassLoader, java.lang.Object, dalvik.system.DexFile, java.util.List) (DexFile.java:296)
I/art: at java.lang.Class dalvik.system.DexFile.loadClassBinaryName(java.lang.String, java.lang.ClassLoader, java.util.List) (DexFile.java:289)
I/art: at java.lang.Class dalvik.system.DexPathList.findClass(java.lang.String, java.util.List) (DexPathList.java:418)
I/art: at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:54)
I/art: at java.lang.Class com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(java.lang.String) (IncrementalClassLoader.java:90)
I/art: at java.lang.Class com.android.tools.fd.runtime.IncrementalClassLoader.findClass(java.lang.String) (IncrementalClassLoader.java:62)
I/art: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
I/art: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:367)
I/art: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:367)
I/art: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
I/art: at void org.apache.kafka.clients.producer.KafkaProducer.<init>(org.apache.kafka.clients.producer.ProducerConfig, org.apache.kafka.common.serialization.Serializer, org.apache.kafka.common.serialization.Serializer) (KafkaProducer.java:223)
I/art: at void org.apache.kafka.clients.producer.KafkaProducer.<init>(java.util.Properties) (KafkaProducer.java:188)
I/art: at void com.nokia.kafkaproducer.Producer.<init>(java.lang.String, java.lang.Boolean) (Producer.java:29)
I/art: at void com.nokia.kafkaproducer.MainActivity$1.onClick(android.view.View) (MainActivity.java:35)
I/art: at boolean android.view.View.performClick() (View.java:5637)
I/art: at void android.view.View$PerformClick.run() (View.java:22429)
I/art: at void android.os.Handler.handleCallback(android.os.Message) (Handler.java:751)
I/art: at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:95)
I/art: at void android.os.Looper.loop() (Looper.java:154)
I/art: at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6119)
I/art: at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
I/art: at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:886)
I/art: at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:776)`
Using following Gradle dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
// https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients
compile group: 'org.apache.kafka', name: 'kafka-clients', version: '0.10.1.1' testCompile 'junit:junit:3.8.1' }
I'm using Producer example from this Github repo
Any help is appreciated Thanks
回答1:
The kafka clients library is developed against Java Standard Edition, which javax.management
package is a part of. However, this package is not part of the Dalvik runtime that Android uses and which only provides a subset of the Java SE APIs. You will need to find a JAR file which contains the classes used by Kafka and put it in the libs
file of your project.
回答2:
Enter it in your build gradle
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 14 //lower than 14 doesn't support multidex
targetSdkVersion 22
// Enabling multidex support.
multiDexEnabled true
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
hope it helps :)
来源:https://stackoverflow.com/questions/41437047/kafka-android-issue