Search code examples
angularnativescript

NativeScript does not recognize "android.app.Application"


I'm starting with nativescript 8 but the documentation for version 8 is horrible, version 7 has much better documentation but they are not compatible in most cases.

The official documentation shows an example to call custom android classes, but it does not show its imports:

OFICIAL EXAMPLE:

@NativeClass()
@JavaProxy('org.myApp.Application')
class Application extends android.app.Application {
  public onCreate(): void {
    super.onCreate()
    // At this point modules have already been initialized
    // Enter custom initialization code here
  }

  public attachBaseContext(baseContext: android.content.Context) {
    super.attachBaseContext(baseContext)
    // This code enables MultiDex support for the application (if needed)
    // androidx.multidex.MultiDex.install(this);
  }
}

I couldn't get the "android.app.Application" class to be recognized, even after importing the following:

import { android } from '@nativescript/core/application';

Looking at the content of "@nativescript/core/application", I see that those properties do not exist and that for example the property "android.app.Application" was changed to "nativeApp".

I don't know if I'm importing the correct class or maybe I'm missing a type import or something like that, because in the NativeScript source codes they refer to "android.app.Application" without any import that refers to it.

I know I'm missing something, but seriously the official documentation for version 8 is very bad.


Solution

  • @nativescript/core/application is a javascript module that abstracts platform (android, ios) specific implementations to use in a javascript (or typescript, vue, etc.) file. The official example you referenced shows how to extend an Android application with custom native application classes which is rarely needed when developing an application on Nativescript. Because there is a default Application and Activities to start with.

    If you need a custom Application class as the main entry point of your app; then you can extend the native class without importing it into the javascript file. The compiler should handle it and import required classes accordingly. See metadata generation

    If you just need to call native classes in your app;

    • You can call Android and iOS API methods without importing them. See Native API access

    • In the case of custom native packages published to a central repository, you should first implement them in your app.gradle file. For example;

      dependencies {
        implementation 'com.github.yalantis:ucrop:2.2.6'
      }
      
    • If you have a native java or kotlin package locally;

      • Then you can create an AAR file via Android Studio and place it in your app's App_Resources/Android/libs directory before referencing it in your app.
      • Or you can create your custom Nativescript plugin. Place your java or kotlin file in your plugin's platforms/Android/java directory. And the Nativescript CLI will create AAR files inside the platforms/android folder for you. See how to develop a custom plugin here, and here. There is also a really well-presented tutorial here