I have to make a library(aar) in Kotlin and have to use that in Android application made using Java. Kotlin library has a class with companion functions to use statically in Java application. When I use that static method in my Android application in Java, I do get "ClassNotFoundException". Can anyone help me how to use companion methods in aar from Android Java application. Please find my code snippets and exception Im getting while running my application.
And I do get this error only when using the Kotlin AAR in Android Java Project but not with Android Kotlin Project. Is there something Im missing in the Kotlin library or Android Java project.
Kotlin Library snippet:
class LogUtil {
companion object {
private const val LOG_TAG = "KOTLIN_LIBRARY"
#JvmStatic
fun log(message: String) {
Log.i(LOG_TAG, message)
}
}
}
Android Activity snippet:
public class MainActivity extends AppCompatActivity {
private HaloSDK haloSDK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testKotlinSDK();
}
private void testKotlinSDK() {
LogUtil.log("Hello");
}
public void showKotlinWebView(View view) {
Intent intent = HaloWebActivity.Companion.createIntent(this, "www.abc.com");
startActivity(intent);
}
}
Exception:
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
at com.abcd.kotlinlibrary.utils.LogUtil$Companion.log(Unknown Source:2)
at com.abcd.kotlinlibrary.utils.LogUtil.log(Unknown Source:2)
at com.abcd.thirdpartyapp.MainActivity.testKotlinSDK(MainActivity.java:25)
at com.abcd.thirdpartyapp.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6940)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.jvm.internal.Intrinsics" on path: DexPathList[[zip file "/data/app/com.abcd.thirdpartyapp-k2I71L3n7Qa6TEETe1GejQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.abcd.thirdpartyapp-k2I71L3n7Qa6TEETe1GejQ==/lib/arm64, /system/lib64, /system/vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.abcd.kotlinlibrary.utils.LogUtil$Companion.log(Unknown Source:2)
at com.abcd.kotlinlibrary.utils.LogUtil.log(Unknown Source:2)
at com.abcd.thirdpartyapp.MainActivity.testKotlinSDK(MainActivity.java:25)
at com.abcd.thirdpartyapp.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6940)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
The missing class is part of the Kotlin Standard Library. You have to depend on it manually if you're not using Kotlin in your project:
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'
Also you can make sure to export the dependency from your library as:
api 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'
Related
I'm struggling to access postman gRPC mock server by Android/kotlin app for several days.
I created the mock server after reading official document. I got the URL for mock server and I could get a response by grpcurl and a Node.js gRPC client. However, I got a error when I used the URL by Android gRPC client.
Stacktrace is below.
java.lang.RuntimeException: Unable to start activity ComponentInfo{io.github.aniokrait.grpcsample/io.github.aniokrait.grpcsample.MainActivity}: io.grpc.StatusRuntimeException: UNAVAILABLE
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: io.grpc.StatusRuntimeException: UNAVAILABLE
at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:271)
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:252)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:165)
at com.book.BookServiceGrpc$BookServiceBlockingStub.getBook(BookServiceGrpc.java:312)
at io.github.aniokrait.grpcsample.MainActivity$Companion.startGrpc(MainActivity.kt:71)
at io.github.aniokrait.grpcsample.MainActivity$Companion.access$startGrpc(MainActivity.kt:34)
at io.github.aniokrait.grpcsample.MainActivity.onCreate(MainActivity.kt:30)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.RuntimeException: TLS ALPN negotiation failed with protocols: [h2]
at io.grpc.okhttp.OkHttpProtocolNegotiator.negotiate(OkHttpProtocolNegotiator.java:104)
at io.grpc.okhttp.OkHttpProtocolNegotiator$AndroidNegotiator.negotiate(OkHttpProtocolNegotiator.java:218)
at io.grpc.okhttp.OkHttpTlsUpgrader.upgrade(OkHttpTlsUpgrader.java:63)
at io.grpc.okhttp.OkHttpClientTransport$3.run(OkHttpClientTransport.java:551)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
The messages says "TLS ALPN negotiation failed with protocols: [h2]", so I have an idea that the problem is around SSL/TLS, but I don't know how to solve it.
I do not think it is likely to be a certificate issue, as I am able to access the URL from Node.js and grpcurl but the issue of my Android app.
I've read tls-on-android and Update your security provider to protect against SSL exploits.
I added dependecies of play-services and installing code as below but still it doesn't work.
Here's my code.
MainActivity.kt
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.book.BookServiceGrpc
import com.book.Index.GetBookRequest
import com.google.android.gms.security.ProviderInstaller
import io.grpc.okhttp.OkHttpChannelBuilder
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ProviderInstaller.installIfNeeded(this#MainActivity)
startGrpc()
}
companion object {
private fun startGrpc() {
try {
val channel = OkHttpChannelBuilder
.forAddress("g-95d43o92ollxvwmm7nx895731r4g9y.srv.pstmn.io",443) //URL is slightly altered to prevent tampering
.useTransportSecurity()
.build()
val stub = BookServiceGrpc.newBlockingStub(channel)
val request = GetBookRequest.newBuilder().setIsbn(123).build()
val result = stub.getBook(request)
} catch (e: Exception) {
e.printStackTrace()
throw e
}
}
}
}
my proto file.
syntax = "proto3";
package com.book;
message Book {
int64 isbn = 1;
string title = 2;
string author = 3;
}
message GetBookRequest {
int64 isbn = 1;
}
service BookService {
rpc GetBook (GetBookRequest) returns (Book) {}
}
and dependecies relevant to gRPC
dependencies {
implementation 'io.grpc:grpc-okhttp:1.51.1'
implementation 'io.grpc:grpc-protobuf-lite:1.51.1'
implementation 'io.grpc:grpc-stub:1.51.1'
implementation 'org.apache.tomcat:annotations-api:6.0.53'
implementation 'com.google.android.gms:play-services-basement:18.1.0'
}
Update:
I found these messages in Debug logs.
W/DynamiteModule: Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule: Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller: Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
I/ProviderInstaller: Installed default security provider GmsCore_OpenSSL
Perhaps The Dynamic Security Provider isn't installed properly, but still I don't know why.
I have an instrumented test that starts an application with a content provider.
The test is straightforward:
#HiltAndroidTest
#UninstallModules({...})
public class MyTest {
#Rule
public HiltAndroidRule hiltRule = new HiltAndroidRule(this);
#Test
...
}
However, the application under test includes a ContentProvider, and this provider is using Hilt to inject data:
public class MyProvider extends ContentProvider {
#EntryPoint
#InstallIn(ApplicationComponent.class)
interface MyProviderEntryPoint {
#SqliteDatabaseName String databaseName();
}
#Override
synchronized public boolean onCreate() {
Context appContext = getContext().getApplicationContext();
MyProviderEntryPoint entryPoint =
EntryPointAccessors.fromApplication(appContext, MyProviderEntryPoint.class);
mOpenHelper = new SqliteStoreOpener(getContext(), entryPoint.databaseName());
return true;
}
This causes the test to crash on startup:
java.lang.RuntimeException: Unable to get provider com.test.MyProvider: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
at android.app.ActivityThread.installProvider(ActivityThread.java:6242)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalStateException: The component was not created. Check that you have added the HiltAndroidRule.
at dagger.hilt.internal.Preconditions.checkState(Preconditions.java:83)
at dagger.hilt.android.internal.testing.TestApplicationComponentManager.generatedComponent(TestApplicationComponentManager.java:79)
at dagger.hilt.android.testing.HiltTestApplication.generatedComponent(HiltTestApplication.java:49)
at dagger.hilt.EntryPoints.get(EntryPoints.java:46)
at dagger.hilt.android.EntryPointAccessors.fromApplication(EntryPointAccessors.java:36)
at com.test.MyProvider.onCreate(EboBirthdayProvider.java:114)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1917)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1892)
at android.app.ActivityThread.installProvider(ActivityThread.java:6239)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I suppose the problem is that the framework is trying to install the provider before Hilt even had a chance to start up and create the components. What's the proper setup to allow Hilt to run first and then construct the providers? For reference, I have a simple test runner:
public class MyRunner extends AndroidJUnitRunner {
#Override
public Application newApplication(ClassLoader cl, String className, Context context)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(cl, HiltTestApplication.class.getName(), context);
}
This is very anti-climactic, but it ended up being a problem in the build.gradle file.
The annotation processor was added as
annotationProcessor 'com.google.dagger:hilt-android-compiler:2.30.1-alpha'
instead of
androidTestAnnotationProcessor 'com.google.dagger:hilt-android-compiler:2.30.1-alpha'
Please if you want to devote question write reason to improve.
The problem is I dont know how to implement the eventbus in presenter with Kotlin. It works fine if I implement and subscribe in activity class but when I am trying the same code in the presenter I got an exception. Might be something in Kotlin way of implementation. There must be nothing wrong with eventbus setting up because implementation in view works fine
My activity => constructor of presenter
private val mMainPresenter = MainPresenter(this, MainInteractor(), this)
My presenter
class MainPresenter(var mMainView: MainInterface?, val mMainInteractor: MainInteractor, val mContext : Context){
/**
* Checking if user have language otherwise show list of games
*/
fun checkingLanguage() {
if(mMainInteractor.getUserLanguage() == "") mMainView?.callLanguageDialog()
else EventBus.getDefault().post(LanguageEvent (mMainInteractor.getUserLanguage()))
}
fun registerSubscribers() {
if (!EventBus.getDefault().isRegistered(mContext)) {
EventBus.getDefault().register(mContext)
}
}
#Subscribe
fun inflateGamesList(event: LanguageEvent){
mMainView?.showGamesList(event.language)
} }
Exception code
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a3802256.zzzz, PID: 13508
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a3802256.zzzz/com.example.a3802256.zzzz.view.main.MainActivity}: org.greenrobot.eventbus.EventBusException: Subscriber class com.example.a3802256.zzzz.view.main.MainActivity and its super classes have no public methods with the #Subscribe annotation
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.example.a3802256.zzzz.view.main.MainActivity and its super classes have no public methods with the #Subscribe annotation
at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67)
at org.greenrobot.eventbus.EventBus.register(EventBus.java:140)
at com.example.a3802256.zzzz.view.main.MainPresenter.registerSubscribers(MainPresenter.kt:20)
at com.example.a3802256.zzzz.view.main.MainActivity.onCreate(MainActivity.kt:32)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
If you want to listen to events in presenter, you should register presenter, and not activity (context). So,
fun registerSubscribers() {
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this)
}
}
I am trying to download objects from Firebase and store them in Realm. I am receiving a crash when combining these two excellent libraries.
I've created a very simple demo app which illustrates my problem. It seems Firebase gets upset when creating an object that extends RealmObject.
I can make this work by removing the RealmObject extension but then I have to maintain two identical objects. Over time this becomes complicated and frustrating which is far from ideal.
Firebase:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("players");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: " + dataSnapshot.getChildrenCount());
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
//***CRASH here*****
Player player = snapshot.getValue(Player.class);
Log.d(TAG, "onDataChange: " + player.getFirstname());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled: ", databaseError.toException());
}
});
Player:
public class Player extends RealmObject{
private int age;
private String firstname;
public Player() {
}
//getters and setters
}
Gradle:
compile 'com.google.firebase:firebase-database:11.8.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'
Realm:
classpath 'io.realm:realm-gradle-plugin:4.3.1'
Error:
FATAL EXCEPTION: main
Process: com.test.testrealm, PID: 7616
java.lang.NoClassDefFoundError: Failed resolution of: Lio/reactivex/Observable;
at java.lang.reflect.Executable.getMethodReturnTypeInternal(Native Method)
at java.lang.reflect.Method.getReturnType(Method.java:141)
at java.lang.Class.getDeclaredMethods(Class.java:1880)
at com.google.android.gms.internal.zzelx.<init>(Unknown Source:310)
at com.google.android.gms.internal.zzelw.zzf(Unknown Source:12)
at com.google.android.gms.internal.zzelw.zzb(Unknown Source:182)
at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.test.testrealm.MainActivity$2.onDataChange(MainActivity.java:41)
at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "io.reactivex.Observable" on path: DexPathList[[zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/base.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_dependencies_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_resources_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_0_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_1_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_2_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_3_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_4_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_5_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_6_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_7_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_8_apk.apk", zip file "/data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.test.testrealm-Mj0Zh1FV_CGS7fFGuEYfWQ==/lib/arm64, /data/app/com.test.testrealm-Mj0Zh1FV_CGS7fFGuEYfWQ==/base.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_dependencies_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_resources_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_0_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_1_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_2_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_3_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_4_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_5_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_6_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_7_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_8_apk.apk!/lib/arm64-v8a, /data/app/com.test.testrealm-Mj0Zh1FN_CGS6fFGuEYfWQ==/split_lib_slice_9_apk.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
01-10 10:24:06.881 7616-7616/com.test.testrealm E/AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 19 more
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.test.testrealm-Mj0Zh1FV_CGS7fFGuEYfWQ==/split_lib_resources_apk.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:353)
at dalvik.system.DexFile.<init>(DexFile.java:100)
at dalvik.system.DexFile.<init>(DexFile.java:74)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
at dalvik.system.DexPathList.<init>(DexPathList.java:157)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:69)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:35)
at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:693)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:727)
at android.app.LoadedApk.getResources(LoadedApk.java:954)
at android.app.ContextImpl.createAppContext(ContextImpl.java:2270)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5639)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
These kind of class loader issues can be solved in two ways:
1.) either add a dummy class for whatever missing, see this answer for details
2.) add compile "io.reactivex.rxjava2:rxjava:2.1.8" to dependencies
implementation "io.reactivex.rxjava2:rxjava:2.1.8
add to dependencies
realm docs says:
An alternative to extending the RealmObject base class is implementing
the RealmModel interface and adding the #RealmClass annotation.
All methods available on RealmObject are then available through static
methods.
If you add the #RealmClass annotation and implement the interface RealmModel, then you can deseralize your objects using Firebase like fireDoc.getObject(Model::class.jaava)
I moved Firebase Cloud Messaging to a library, so that I can import this into any of my apps and provide push messages. So therefore I created a library and moved everything from this example to my library.
Then I compile this library in my build.gradle and use it as desired.
This works so far when I instantiate Firebase from app level:
FirebaseMessaging.getInstance().subscribeToTopic(topic)
Now, I thought of moving this down into my library:
fun initFirebaseMessaging(topic : String) : String
{
FirebaseMessaging.getInstance().subscribeToTopic(topic)
Timber.d("Push subscribeToTopic $topic")
val token = FirebaseInstanceId.getInstance().token!!
Timber.d("Push FirebaseinstanceId token $token")
sendRegistrationToServer(token)
return token
}
Since I do not hand over the app level context, I get:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.demo, PID: 8047
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo/com.example.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.demo. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.demo. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(Unknown Source:58)
at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source:0)
at com.google.firebase.messaging.FirebaseMessaging.getInstance(Unknown Source:9)
at com.example.network.fcm.exampleFirebaseInstanceIdService$Companion.initFirebaseMessaging(exampleFirebaseInstanceIdService.kt:14)
at com.example.MainActivity.onCreate(MainActivity.kt:168)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
ADDENDUM: I added that line to my init
fun initFirebaseMessaging(topic : String, context : context) : String
{
FirebaseApp.initializeApp(context)
FirebaseMessaging.getInstance().subscribeToTopic(topic)
Timber.d("Push subscribeToTopic $topic")
val token = FirebaseInstanceId.getInstance().token!!
Timber.d("Push FirebaseinstanceId token $token")
sendRegistrationToServer(token)
return token
}
leads to very same error message.
Your error
Make sure to call FirebaseApp.initializeApp(Context) first.
In your library u need create function like
public static void Init(Context context) {
FirebaseApp.initializeApp(context);
}
and call it before using firebase. Best way its add to your application
example :
... extends Application /*** code **/
onCreate () {
YourLibrary.Init(this);
}