How to call a kotlin function from gradle in android project? - android

In my android project, I have a kotlin object AssetValidator which does some validation over the asset files.
object AssetValidator {
fun validate(assetsDir: File) {
...
}
...
}
Until now, I was calling this function at the startup of my application. But now, I am thinking of calling it from build script instead. But, I am unable to import this package into my gradle file. Android studio is showing Unresolved reference in the import statement. Someone please tell me how to do this.

Related

Cannot resolve symbol 'EInvoice' in using EInvoice API in Android Studio

I'm new to Android Studio and is trying to create a app for recording invoice relative information.
Currently I'm trying to use "EInvoice" API shared in Github but stuck in object declaration.
Refer to author's advice, I succeeded to add dependency to Android Studio, but always failed in object declaration:
val invSdk = EInvoice("API_KEY")
It seems that Android Studio can't recognize "val" and "EInvoice".
Then I try to declare it with common method:
private EInvoice Invoice=new EInvoice();
Unfortunately Android Studio always failed to compile with error "cannot resolve symbol 'EInvoice' even if I try to add below import code:
import com.github.kevinchung0921:einvoice_sdk:v1.3
Can you please be kind to suggest correct method to declare the external-imported-API object and use functions such as:
val detail = invSdk.getCarrierDetail(header, USER_BARCODE, USER_PASS)
Thanks a lot for your reading and further help.
cannot resolve Error

How to use react-native-batch-push on android? RNBatch is undefined when trying to access RNBatch.NOTIFICATION_TYPES

I'm trying to use this package in my react native project.
So far I've followed all the steps in their installation guide and I made it work on iOS.
However, on Android, every time I try to import Batch or BatchPush in my code like this:
import { BatchPush, Batch } from "#bam.tech/react-native-batch";
I get an error on Android only:
null is not an object (evaluating 'RNBatch.NOTIFICATION_TYPES')
So when I go to node_modules/#bam.tech/react-native-batch/dist/BatchPush.js I see this
const RNBatch = react_native_1.NativeModules.RNBatch;
exports.AndroidNotificationTypes = RNBatch.NOTIFICATION_TYPES;
So somehow the native module is not being imported correctly. Do I need to follow extra steps for this to work?
#bam.tech/react-native-batch is being used with version 5.2.1
npm version 6.14.7
react-native version 0.60.5
Update: it turns out that the package was not linked correctly and that I had to manually add the package in MainApplication.java (I don't know why react-native link did not add this automatically)
Add the import:
import tech.bam.RNBatchPush.RNBatchPackage;
And then add
new RNBatchPackage(), in the getPackages() method.

Ionic capacitor plugin not working for android

I am written a very simple plugin which get all the messages from android device and pack it to response. When I am using npm install ionic-capacitor-sms-access and trying to access messages it just call web method but not the android method. So its not working can someone help me here and tell me what went wrong?
Here is my plugin https://www.npmjs.com/package/ionic-capacitor-sms-access
(Note: only the android folder)
It works, there are two ways to achieve this.
Method 1 (from the official tutorial)
What you need to do is just to keep following that tutorial to the section of 'Custom JavaScript', where shows demonstrates how to use your plugin within your ionic/typescripts codes.
such as:
import { Plugins } from '#capacitor/core';
const { SuperGreatPlugin } = Plugins;
export class CustomSuperPlugin {
constructor() {
}
customAwesomeness() {
SuperGreatPlugin.awesome();
}
}
Notes:
the name of the plugin class, i.e. 'SuperGreatPlugin' must be same with your java class for the plugin.
Method 2 (javascript way)
You can also import your plugin from the npm package you published. But be aware of the first line of the generated definitions.ts, declare module "#capacitor/core". This means you have to find your plugin from the specific module, here it is 'Plugins' as other plugins usage.
The following is the method 2:
import { SuperGreatPlugin } from 'YOUR_PLUGIN_NPM_PACKAGE';
async pluginEcho() {
await Plugins.SuperGreatPlugin.echo({value: 'bla bla bla'})
}
call the function pluginEcho() in your ionic page.
When I started I head really trouble distinguishing the web and native plugin but finally I understand that they are completely separated.
I recommend to rename the exported web plugin object in your web.ts file and add Web in the name. For me this was also necessary in order to not have compile errors.
TS2308: Module './definitions' has already exported a member named 'YourPlugin'. Consider explicitly re-exporting to resolve the ambiguity.
If you want to use your web plugin you import and use it like:
import { YourWebPlugin } from 'YOUR_PLUGIN_NPM_PACKAGE';
YourWebPlugin.callSomething();
If you want to use the native plugin you import and use it like:
import { Plugins } from '#capacitor/core';
const { YourPlugin } = Plugins;
YourPlugin.callSomething();
Don't forget to expose your native plugin to your android app project where you use your custom plugin
https://capacitor.ionicframework.com/docs/plugins/android#export-to-capacitor

Kotlin: unresolved reference softbankrobotics

I'm trying to integrate a chatbot on my Pepper robot using Kotlin on Android Studio. I just follow this tutorial step by step.
Testing your Dialogflow agent in standalone
But when I execute this in Kotlin REPL(in module data):
import com.softbankrobotics.chatwithdialogflow.data.DialogflowDataSource
import java.io.File
val stream = File("C:/Users/yuhliu/ChatWithDialogflow/app/src/main/res/raw/credentials.json").inputStream()
val dataSource = DialogflowDataSource(stream)
dataSource.detectIntentTexts("sois intelligent", "my-test-session", "fr")
I got this error
error: unresolved reference: softbankrobotics
import com.softbankrobotics.chatwithdialogflow.data.DialogflowDataSource
My project directory is something like this:
I'm new to Kotlin. Do you have any idea what's going wrong here?
On first opening the Kotlin REPL, you might get:
You’re running the REPL with outdated classes: Build module 'data' and restart
... clicking on that will reload the REPL and the import should work.
(I tried the same as you and had the same error, and the import worked after reload)

Why an unresolved reference on attempting to access constant values?

The app defines constants in a Kotlin singleton object:
#file:JvmName("APIConstants")
package com.myapp.api
object APIConstants {
const val HTTP_RESPONSE_CODE_NOT_AUTHORIZED = 401
etc....
}
They are then used in another class:
import com.myapp.api.APIConstants.HTTP_RESPONSE_CODE_NOT_AUTHORIZED
etc ...
class API {
private fun returnBadResponse(response: Response<*>, callback: ApiAuthListener<*>) {
if (response.code() == HTTP_RESPONSE_CODE_NOT_AUTHORIZED) {
callback.onBadAuthToken()
} else {
callback.onFailure(response.message(), getServerError(response))
}
}
In this class Android Studio (3.0 beta) provided a hint to add the import for the constant, and it does not give any indication of a problem (no red underlines etc, and the constant reference in the method is shown in purple italic text indicating it has been resolved) but when I build the project I get this:
Error: Unresolved reference: HTTP_RESPONSE_CODE_NOT_AUTHORIZED
I've tried clearing the IDE cache and restarting it, and doing a clean build, which make no difference. I've tried removing the #JvmName annotation and even placing the const values in the root of the file with no containing object but neither allows a build.
Why is the class failing to reference the constant, especially when the IDE strongly suggests it can resolve it?
And the solution is.... to make very sure all Kotlin source files have a .kt file extension! In this case the APIConstants file was called "APIConstants" and not "APIConstants.kt" which appears to mean the IDE was able to resolve references based on the content of the file, but the build tools could not. Confusingly Android Studio showed a Kotlin K icon on the filename despite the lack of a .kt extension.

Categories

Resources