NativeScript / android

NativeScript for Android using v8
https://docs.nativescript.org/guide/android-marshalling
Apache License 2.0
524 stars 134 forks source link

@Interfaces decorator gives an error "Error executing Static Binding Generator: Class not found" #1638

Open harsh2304 opened 3 years ago

harsh2304 commented 3 years ago

Environment: Nativescript: 6.7.8 tns-core-modules: 6.0.1 tns-andoird: 6.5.3

Issue: I am implementing Java Interface in my Nativescript-angular application for android. I have added Java android plugin dependency in the app.gradle file. I get a build (tns build android) error for @Interfaces decorator.

Build error during "tns build android" "Error executing Static Binding Generator: Class not found global.com.microsoft.identity.client.IPublicClientApplication.ISingleAccountApplicationCreatedListener"

If I execute below steps then I dont see any issue.

  1. "tns run android --no-hmr"
  2. Application built and started successfully
  3. Perform necessary @Interfaces related changes in the TS file
  4. Automatically starts the incremental build
  5. I dont see any error and my code works fine.

app.gradle:
dependencies { implementation "com.microsoft.identity.client:msal:1.2.+" }

TS file: declare var global: any;

@Interfaces([global.com.microsoft.identity.client.IPublicClientApplication.ISingleAccountApplicationCreatedListener]) export class SingleAccount extends java.lang.Object { mSingleAccountApp; constructor() { super(); // necessary when extending TypeScript constructors return global.__native(this); } onCreated(application): void { //some code } onError(exception): void { ////some code } }

vmutafov commented 3 years ago

Hi @harsh2304 The error is probably caused because of the global at the beginning of global.com.microsoft.identity.client.IPublicClientApplication.ISingleAccountApplicationCreatedListener

The Static Binding Generator parses the JS code from the transpilation of the TS code and looks up the arguments of the @Interfaces decorator. In this case, it tries to find a Java/Kotlin interface with a package name global.com.microsoft.identity.client and interface IPublicClientApplication.ISingleAccountApplicationCreatedListener but fails due to the global word in the package.

Please, try removing global. You would however need to declare a com variable or namespaces for the TS compiler. Please check the following docs: https://docs.nativescript.org/core-concepts/android-runtime/binding-generator/extend-class-interface#interfaces https://docs.nativescript.org/core-concepts/android-runtime/metadata/generating-typescript-declarations

harsh2304 commented 3 years ago

Hi @vmutafov, Thank you so much for your response.

Surprisingly, If I call any function of Java package using global, I don't get any issue/error at build time and run time. My code works as per expectation.

//Sample code global.com.microsoft.identity.client.PublicClientApplication.createSingleAccountPublicClientApplication (this.context,this.res, this.singleAccount)

It only gives issue with the @Interface decorator and that too when I perform clean build. It works fine if it is an incremental build as I specified in the main issue. This is bit strange.

Though, using global/android is not a good practice as mentioned in the link posted by you. https://docs.nativescript.org/core-concepts/android-runtime/metadata/generating-typescript-declarations

harsh2304 commented 3 years ago

Hi @vmutafov , I tried following below link. https://docs.nativescript.org/core-concepts/android-runtime/metadata/generating-typescript-declarations

I followed below steps.

  1. Open dts-generator/build.gradle file and locate dependencies part
  2. Add as a testCompileOnly dependency the one that you want to generate typings for

build.gradle file

dependencies { testCompileOnly 'com.microsoft.identity.client:msal:1.2.+' }

  1. Open the dts-generator folder in your terminal

  2. Run the following command: ./gradlew extractAllJars

Output of the above command:

Task :extractAllJars FAILED

FAILURE: Build failed with an exception.

BUILD FAILED in 5s 1 actionable task: 1 executed

Below is the 73 line number of build.gradle file which is part of task "extractAllJars"

73: def outputDir = java.nio.file.Paths.get(extractedDependenciesDir, nextDependency.toString()).normalize().toString().replace(':', '_')

vmutafov commented 3 years ago

Do you have a sample app you can share in Github? I may be able to debug your issue but have in mind I no longer work on NativeScript and probably @NathanaelA could help quicker than me.

As for why accessing, for example global.java.util.ArrayList, using the global keyword, that's because native packages/classes/interfaces are attached to the global object. So, using global or not - it's the same. But for the @Interfaces decorator, things happen at build time and the SBG doesn't know about the global object.

harsh2304 commented 3 years ago

Thank you @vmutafov for your reply. Sorry, I can't put the app code on GitHub. But all the relevant part of the code for the issue, has been put here. Let me know in case you find some more information on the issue.

Hi @NathanaelA, can you please help here?

NathanaelA commented 3 years ago

I just tested this; I added testCompileOnly 'com.microsoft.identity.client:msal:2.0.1' image

And then found out it had a dependency, which I googled and found the issue discussing it and how to get gradle to find it, and then added this.. image

When I was done I had a folder full of jar-files: image Including the msal.aar

I suspect you must have done a typo on your build.gradle file on line 73.

harsh2304 commented 3 years ago

Hi @NathanaelA, Thank you so much for your reply :)

I have not done any changes to the build.gradle file except adding testCompileOnly line and adding dependencies.

build.gradle image

build.gradle image

Error on line number 76: image

Error on line number 76 when I execute "gradlew extractAllJars"

image

Any idea what could be wrong here?

NathanaelA commented 3 years ago

Can you try deleting your line: testCompileOnly 'com.microsoft.identity.client:msal:2.0.1' and then copying and pasting mine into its place.

Maybe the ":" you have in yours is a unicode replacement character of some sort, rather than just a normal colon. So it is confusing it. Based on the error it looks like it doesn't know what the ":" is, however all ":" should be replaced with "_" during extraction, so if it is still seeing it that means it didn't replace it, and the only reason I can think is that this is not a colon character but some other unicode character that looks like a colon...

harsh2304 commented 3 years ago

@NathanaelA : Thank you for your reply and Sorry, for the delayed response. I tried deleting the line and copied the line mentioned by you but getting the same error. It is strange.