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);
}
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 created a Firebase project and add google-services.json to app module.
I have setup Firebase remote config as follow :
private fun initFirebaseRemoteConfig() {
val remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = if (BuildConfig.DEBUG) 0 else 3600
}
//remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
remoteConfig.setConfigSettingsAsync(configSettings)
remoteConfig.fetchAndActivate().addOnCompleteListener { task ->
val updated = task.result
if (task.isSuccessful) {
Timber.d("Config params updated: $updated")
} else {
Timber.d("Unsuccessful")
}
}
}
I receive following exception on this line task.result as follow :
com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.remoteconfig.FirebaseRemoteConfigServerException: Fetch failed: The user is not authorized to access the project. Please make sure you are using the API key that corresponds to your Firebase project.
at com.google.android.gms.tasks.zzu.getResult(com.google.android.gms:play-services-tasks##17.0.2:15)
at com.sample.android.tmdb.TmdbApp.initFirebaseRemoteConfig$lambda-0(TmdbApp.kt:34)
at com.sample.android.tmdb.TmdbApp.lambda$P4-jkzwf9-IJZ17_EtnpghkBXis(Unknown Source:0)
at com.sample.android.tmdb.-$$Lambda$TmdbApp$P4-jkzwf9-IJZ17_EtnpghkBXis.onComplete(Unknown Source:0)
at com.google.android.gms.tasks.zzi.run(com.google.android.gms:play-services-tasks##17.0.2:4)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at com.google.android.gms.internal.tasks.zzb.dispatchMessage(com.google.android.gms:play-services-tasks##17.0.2:6)
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: com.google.firebase.remoteconfig.FirebaseRemoteConfigServerException: Fetch failed: The user is not authorized to access the project. Please make sure you are using the API key that corresponds to your Firebase project.
at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.createExceptionWithGenericMessage(ConfigFetchHandler.java:380)
at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchFromBackend(ConfigFetchHandler.java:333)
at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchFromBackendAndCacheResponse(ConfigFetchHandler.java:278)
at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.lambda$fetchIfCacheExpiredAndNotThrottled$1$ConfigFetchHandler(ConfigFetchHandler.java:215)
at com.google.firebase.remoteconfig.internal.-$$Lambda$ConfigFetchHandler$PAoFtfQiOtbXSTeK4H2aFJ-JNJI.then(Unknown Source:8)
at com.google.android.gms.tasks.zzg.run(com.google.android.gms:play-services-tasks##17.0.2:2)
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)
Caused by: com.google.firebase.remoteconfig.FirebaseRemoteConfigServerException: Forbidden
at com.google.firebase.remoteconfig.internal.ConfigFetchHttpClient.fetch(ConfigFetchHttpClient.java:195)
at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchFromBackend(ConfigFetchHandler.java:307)
at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.fetchFromBackendAndCacheResponse(ConfigFetchHandler.java:278)
at com.google.firebase.remoteconfig.internal.ConfigFetchHandler.lambda$fetchIfCacheExpiredAndNotThrottled$1$ConfigFetchHandler(ConfigFetchHandler.java:215)
at com.google.firebase.remoteconfig.internal.-$$Lambda$ConfigFetchHandler$PAoFtfQiOtbXSTeK4H2aFJ-JNJI.then(Unknown Source:8)
at com.google.android.gms.tasks.zzg.run(com.google.android.gms:play-services-tasks##17.0.2:2)
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)
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 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'