Nativescript-cli: is it possible to use .java files in nativescript ?

Created on 8 Mar 2016  路  25Comments  路  Source: NativeScript/nativescript-cli

Hello,

I am wondering if is it possible to design application UI using Angular JS and business logic using android with nativescript ? I mean can I dump all java files as it is like my native android and just build UI using nativescript/javascript ?
If it is possible which IDE I should use ? Is it possible using android studio?
On the other hand can a simple nativescript app be built using Android studio/Eclipse ?
Please reply.

Most helpful comment

I've came up with a solution for Android.

  1. run npm install --save fs-extra
  2. create a new file hooks/before-prepare/copy-native-code-files.js with following content
var fs = require('fs-extra');
var path = require('path');

module.exports = function (logger, platformsData, projectData, hookArgs) {
    var platform = hookArgs.platform.toLowerCase();
    var appResourcesPath = projectData.appResourcesDirectoryPath;

    var paths = {
        android: {
            resources: path.join(appResourcesPath, 'Android'),
            platform: platformsData.platformsData.android.projectRoot
        },

        ios: {
            resources: path.join(appResourcesPath, 'iOS'),
            platform: platformsData.platformsData.ios.projectRoot
        }
    };

    return new Promise(function (resolve, reject) {
        if (platform == 'android') {
            var source = path.join(paths.android.resources, "native-code");
            var target = path.join(paths.android.platform, "src", "main", "java");

            try
            {
                fs.ensureDir(path.join(paths.android.resources, "native-code"));
                fs.copySync(source, target, { preserveTimestamps: true });
            }
            catch (error)
            {
                reject();
            }
        }

        resolve();
    });
};

Now remove the android platform, add it back again and prepare. You'll see the directory App_Resources/Android/native-code has been created. Now place any native code there like you would under src/main/java. (e. g. native-code/com/example/project/Test.java)

Please be aware of that:

  1. you need to prepare again if you change Java files
  2. iOS is missing
  3. deletion of java files will require you to remove the platform and add it back again

If you can provide me the target directory for iOS I'll add that as well and maybe create a installable nativescript plugin.

All 25 comments

Hi, @DeepakDonde
is it possible to use .java files in nativescript ?
Yes, it's possible to use .java files in {N}. With the new 1.7.0 release you will be able to put the .java files in platforms/android/src/main/java folder and metadata will be generated for these files and they will be included in the .apk package.

  • I am wondering if is it possible to design application UI using Angular JS and business logic using android with nativescript ?
  • I mean can I dump all java files as it is like my native android and just build UI using nativescript/javascript ?

ping: @hdeshev

Is it possible using android studio?
Yes, you can use Android Studio and open platforms/android folder which is an Android Studio project.

On the other hand can a simple nativescript app be built using Android studio/Eclipse ?
The nativescript app is build by an Android Studio project.

Hello @hdeshev ,
Can you please help me regarding this?

Can I dump all java files as it is like my native android and just build UI using nativescript/javascript ?
If is it possible to design application UI using Angular JS and business logic using android with nativescript ?

It will be great if you would able to provide some sample app and code for same.

@Plamen5kov ,
Thanks for the reply.
I have opened platforms/android folder of sample-groceries sample app. I am getting following error-

Error:(154, 0) Gradle DSL method not found: 'compile()'
Possible causes:

Please help.
Thanks.

Hi @DeepakDonde
If you opened the folder in Android Studio you probably need to update the gradle tool chain version from 1.3.0 to 1.5.0.

@Plamen5kov ,
I suppose gradle tool chain version is nothing but Android studio version.
If not then how to check my gradle tool chain version ?
My Android Studio version is 1.5.1.

@DeepakDonde
In the build gradle file there should be:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:1.5.0"
    }
}

This is the tool chain I've mentioned.

@Plamen5kov
I have exactly same as you have mentioned.
It is 1.5.0

@DeepakDonde, can you provide a repo with the project you're trying to run on Android Studio, because I can't seem to reproduce your problem.

@Plamen5kov
I am running this project.
I have downloaded this from command prompt and following the commands mentioned.
I have opened this project in Android studio and started getting the errors.
Find attached screen shot for the details.
screenshot

@DeepakDonde
Here is what you should do:

  • git clone https://github.com/NativeScript/sample-Groceries.git
  • cd sample-Groceries
  • tns install
  • Open Androd studio
  • File/New/Import Project (navigate to sample-Groceries/platforms/android)
  • you'll be prompted with something like (do you want android studio to generate gradle wrapper for you) .. you say OK
  • go in build.gradle and change classpath "com.android.tools.build:gradle:1.3.0" to 1.5.0
  • build and run

The project should have the following structure in Project view:

          |--manifests/
          |--jniLibs/
          |--res/
          |--assets/
Gradle Scripts
          |--build.gradle
          |--settings.gradle
          |--local.properties

I've tested it and it runs ok.

@Plamen5kov,
I performed the steps you mentioned above.

  • git clone https://github.com/NativeScript/sample-Groceries.git
  • cd sample-Groceries
  • tns install
  • File/New/Import Project (navigate to sample-Groceries/platforms/android)

After this step I am facing errors -
Please find screenshot of errors -

screenshot2

Can you please end me your AS configurations ?

Thanks.

@DeepakDonde
Well based on the error:
No resource found that matches the given name (at "icon" with value "@drawable/icon").
You don't have an icon in sample/drawable/ folder, but as I see in the sample groceries repo all icons are present, so i would suggest checking if everything is OK in the res/drawable folder.
I would also suggest to update your Local Maven repository for Support Libraries from the SDK manager.

Edit: you can also try to run tns prepare android before opening the project in Android Studio, because there is a chance the icons aren't copied in the native project.

I have updated Local Maven repository for Support Librariesto version 28 and Android Support Library to version 23.2.1 today itself.
Also, As per your suggestion, I have performed this step -

  • File/New/Import Project (navigate to sample-Groceries/platforms/android ) in AS.

At \sample-Groceries\platforms\android\src\main\res folder I do not have drawable folder.
rather it is at \sample-Groceries\app\App_Resources\Android
Have I done something wrong while importing project ?
OR Do I need to manually copy drawable folder in res folder ?

@DeepakDonde run tns prepare android in the root {N} project folder and the CLI will copy everything necessary.
Then open AS and try building again.

Now drawable\icon error is seems to be resolved after tns prepare android.

Still no luck.
Here is the new error generated.

Error:Execution failed for task ':buildMetadata'.
Process 'command 'C:\Program Files\Java\jdk1.7.0_71\bin\java.exe'' finished with non-zero exit value 1

@DeepakDonde try to update java to 1.8.

@Plamen5kov I am using JDK 8 only.
jdk version is 1.8.0_74.
Weird error though.
Can you please send your build.gradle for my reference ?

@DeepakDonde you probably have an environment variable called JAVA_HOME (correct me if i'm wrong) but the gradle build uses C:\Program Files\Java\jdk1.7.0_71\bin\java.exe 1.7 java. That's commonly caused because when java is installed it adds a path to some java shortcuts, that are NOT the JAVA_HOME you specified. So if you edit the PATH environment variable, in the beginning there should be a path to C:\Program Files\Java\jdk1.7.0_71\bin\ and you should delete it, so that the build system can't find if from the wrong place.

@Plamen5kov ,

Following are my System Configurations -

  • Android SDK : API 23 (Android 6.0)
  • Android SDK Build-tools : 23.0.2
  • Android Support Library : 23.2.1
  • Local Maven repository for Support Libraries : 28
  • JDK : jdk1.8.0_74. (JDK 8)
  • node.js : version 4.3.1

Following are my Environmental Variables -

  • JAVA_HOME : C:\Program Files\Java\jdk1.8.0_74
  • ANDROID_HOME : E:\ANDROID\sdk
  • path : %PATH%;C:\Program Files\Java\jdk1.8.0_74\bin;C:\Program Files\nodejs\;E:\ANDROID\sdk\tools;E:\ANDROID\sdk\platform-tools;

@Plamen5kov
Can you please look for properties and dependencies in your project ?
I think I am getting it wrong there.
Compiling SDK version is showing 'unrecognised' in my case. refer screenshots below .
Also I am not getting this line in dependencies - def suppotVer = "22.2.0" and I am not able to change this anymore to 23.0.0.

dependencies
properties
Also Now I am getting this type of error :
error1
When I googled , I got some threads which asked to make dependencies version to current support lib version .

@Plamen5kov ,
Finally resolved with all issues and able to run the project successfully.
Thanks for all your time and help ..!!
I really appreciate .. :)

is it possible to use swift file in nativescript

It is possible to consume Swift code exposed to Objective-C in NativeScript.
According to Apple's documentation

A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C. If your Swift class is a descendant of an Objective-C class, the compiler automatically adds the @objc attribute for you.

All others would not be accessible in NativeScript.

It would be amazing if we could put Java Code to App_Resources/Android/native-code/* and Objective-C / Swift code under App_Resources/iOS/native-code/ and have hooks put the files at the right place automatically. Working under platforms/ directly is dangerous as this is generated code. Unfortunately there doesn't seem to be any documentation thould would help me enough to build such a hook myself. Did I miss anything? Could someone come up with this? It would be so amazing 馃憤

I've came up with a solution for Android.

  1. run npm install --save fs-extra
  2. create a new file hooks/before-prepare/copy-native-code-files.js with following content
var fs = require('fs-extra');
var path = require('path');

module.exports = function (logger, platformsData, projectData, hookArgs) {
    var platform = hookArgs.platform.toLowerCase();
    var appResourcesPath = projectData.appResourcesDirectoryPath;

    var paths = {
        android: {
            resources: path.join(appResourcesPath, 'Android'),
            platform: platformsData.platformsData.android.projectRoot
        },

        ios: {
            resources: path.join(appResourcesPath, 'iOS'),
            platform: platformsData.platformsData.ios.projectRoot
        }
    };

    return new Promise(function (resolve, reject) {
        if (platform == 'android') {
            var source = path.join(paths.android.resources, "native-code");
            var target = path.join(paths.android.platform, "src", "main", "java");

            try
            {
                fs.ensureDir(path.join(paths.android.resources, "native-code"));
                fs.copySync(source, target, { preserveTimestamps: true });
            }
            catch (error)
            {
                reject();
            }
        }

        resolve();
    });
};

Now remove the android platform, add it back again and prepare. You'll see the directory App_Resources/Android/native-code has been created. Now place any native code there like you would under src/main/java. (e. g. native-code/com/example/project/Test.java)

Please be aware of that:

  1. you need to prepare again if you change Java files
  2. iOS is missing
  3. deletion of java files will require you to remove the platform and add it back again

If you can provide me the target directory for iOS I'll add that as well and maybe create a installable nativescript plugin.

I'm using nativescript angular.
It seems that platforms is now git ignored, and the place to put native code is in App_resources.
Is that correct?
And where is that documented?
(I want to write a long running service which will check that files are downloaded / download them.)

Was this page helpful?
0 / 5 - 0 ratings