Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add android embedding and merge main #1799

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ thumbs.db
android-runtime.iml
test-app/build-tools/*.log
test-app/analytics/build-statistics.json
package-lock.json
package-lock.json
test-app/build-tools/jsparser/tests/cases/*/internal/livesync.js
test-app/build-tools/*.jar
7 changes: 7 additions & 0 deletions build-artifacts/project-template-gradle/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
pluginManagement {
repositories {
gradlePluginPortal()
mavenLocal()
}
}

rootProject.name = "__PROJECT_NAME__"
include ':app'//, ':runtime', ':runtime-binding-generator'

Expand Down
12 changes: 10 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,18 @@ task copyFilesToProjectTemeplate {
copy {
from "$TEST_APP_PATH/app/src/main/java/com/tns/"
include "*.java"
exclude "NativeScriptApplication.java"
exclude "NativeScriptActivity.java"
exclude "TestNativeScriptApplication.java"
exclude "TestNativeScriptActivity.java"
into "$DIST_FRAMEWORK_PATH/app/src/main/java/com/tns"
}
copy {
from "$TEST_APP_PATH/app/src/main/java/com/tns/internal"
into "$DIST_FRAMEWORK_PATH/app/src/main/java/com/tns/internal"
}
copy {
from "$TEST_APP_PATH/app/src/main/java/com/tns/embedding"
into "$DIST_FRAMEWORK_PATH/app/src/main/java/com/tns/embedding"
}
copy {
from "$BUILD_TOOLS_PATH/static-binding-generator/build/libs/static-binding-generator.jar"
into "$DIST_FRAMEWORK_PATH/build-tools"
Expand Down Expand Up @@ -295,6 +299,10 @@ task copyFilesToProjectTemeplate {
from "$TEST_APP_PATH/app/build.gradle"
into "$DIST_FRAMEWORK_PATH/app"
}
copy {
from "$TEST_APP_PATH/app/nativescript.gradle"
into "$DIST_FRAMEWORK_PATH/app"
}
copy {
from "$TEST_APP_PATH/build.gradle"
into "$DIST_FRAMEWORK_PATH"
Expand Down
2 changes: 0 additions & 2 deletions test-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ app/app.iml
treeNodeStream.dat
treeStringsStream.dat
treeValueStream.dat
NativeScriptActivity.java
NativeScriptApplication.java
**/com/tns/gen
4 changes: 2 additions & 2 deletions test-app/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

<application
android:requestLegacyExternalStorage="true"
android:name="com.tns.NativeScriptApplication"
android:name="com.tns.TestNativeScriptApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.tns.NativeScriptActivity" android:exported="true">
<activity android:name="com.tns.TestNativeScriptActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
4 changes: 2 additions & 2 deletions test-app/app/src/main/assets/app/MyActivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
}

@JavaProxy("com.tns.NativeScriptActivity")
@JavaProxy("com.tns.TestNativeScriptActivity")
class MyActivity extends android.app.Activity
{
onCreate(bundle: android.os.Bundle)
Expand Down Expand Up @@ -62,7 +62,7 @@ var MyActivity = (function (_super) {

};
MyActivity = __decorate([
JavaProxy("com.tns.NativeScriptActivity")
JavaProxy("com.tns.TestNativeScriptActivity")
], MyActivity);
return MyActivity;
})(android.app.Activity);
2 changes: 1 addition & 1 deletion test-app/app/src/main/assets/app/MyApp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// demonstrates how to extend class in JavaScript with prebuilt Java proxy
var MyApp = android.app.Application.extend("com.tns.NativeScriptApplication",
var MyApp = android.app.Application.extend("com.tns.TestNativeScriptApplication",
{
onCreate: function()
{
Expand Down
36 changes: 36 additions & 0 deletions test-app/app/src/main/java/com/tns/TestNativeScriptActivity.java
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file might need to be gitignored (NativeScriptActivity.java isn't currently committed into main)

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* AUTO-GENERATED FILE. DO NOT MODIFY.
* This class was automatically generated by the
* static binding generator from the resources it found.
* Please do not modify by hand.
*/
package com.tns;

@com.tns.JavaScriptImplementation(javaScriptFile = "./MyActivity.js")
public class TestNativeScriptActivity extends android.app.Activity
implements com.tns.NativeScriptHashCodeProvider {
public TestNativeScriptActivity() {
super();
com.tns.Runtime.initInstance(this);
}

protected void onCreate(android.os.Bundle param_0) {
java.lang.Object[] args = new java.lang.Object[1];
args[0] = param_0;
com.tns.Runtime.callJSMethod(this, "onCreate", void.class, args);
}

public void onCreate(android.os.Bundle param_0, android.os.PersistableBundle param_1) {
java.lang.Object[] args = new java.lang.Object[2];
args[0] = param_0;
args[1] = param_1;
com.tns.Runtime.callJSMethod(this, "onCreate", void.class, args);
}

public int hashCode__super() {
return super.hashCode();
}

public boolean equals__super(java.lang.Object other) {
return super.equals(other);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file might need to be gitignored (NativeScriptApplication.java isn't currently committed into main)

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* AUTO-GENERATED FILE. DO NOT MODIFY.
* This class was automatically generated by the
* static binding generator from the resources it found.
* Please do not modify by hand.
*/
package com.tns;

@com.tns.JavaScriptImplementation(javaScriptFile = "./MyApp.js")
public class TestNativeScriptApplication extends android.app.Application
implements com.tns.NativeScriptHashCodeProvider {
private static android.app.Application thiz;

public TestNativeScriptApplication() {
super();
thiz = this;
}

public void onCreate() {
com.tns.Runtime runtime = com.tns.RuntimeHelper.initRuntime(this);
if (!com.tns.Runtime.isInitialized()) {
try {
super.onCreate();
return;
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
java.lang.Object[] args = new java.lang.Object[0];
com.tns.Runtime.callJSMethod(this, "onCreate", void.class, args);
if (runtime != null) {
runtime.run();
}
}

public static android.app.Application getInstance() {
return thiz;
}

public int hashCode__super() {
return super.hashCode();
}

public boolean equals__super(java.lang.Object other) {
return super.equals(other);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void callback(String str) {
private String readString() throws Exception {
String str = null;

Context context = com.tns.NativeScriptApplication.getInstance();
Context context = com.tns.TestNativeScriptApplication.getInstance();

InputStream inputStream = null;
try {
Expand Down
6 changes: 6 additions & 0 deletions test-app/app/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 2 additions & 2 deletions test-app/build-tools/android-metadata-generator/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

// todo: check if still needed
// if(!project.hasProperty("loadedProjectDeps")){
Expand Down
4 changes: 2 additions & 2 deletions test-app/build-tools/static-binding-generator/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'java-library'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

// todo: check if still needed
// if(!project.hasProperty("loadedProjectDeps")){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.nativescript.staticbindinggenerator;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -11,8 +12,10 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -112,6 +115,10 @@ private static void validateInput() throws IOException {

List<DataRow> inputFile = Generator.getRows(SBG_INPUT_FILE);
inputDir = new File(inputFile.get(0).getRow());
Path assetsInternalDirPath = inputDir.getParentFile().toPath().resolve("internal");
extractResource(assetsInternalDirPath.resolve("ts_helpers.js"), "ts_helpers.js");
extractResource(assetsInternalDirPath.resolve("livesync.js"), "livesync.js");

webpackWorkersExcludePath = Paths.get(inputDir.getAbsolutePath(), "__worker-chunks.json").toString();

if (!inputDir.exists() || !inputDir.isDirectory()) {
Expand All @@ -132,7 +139,9 @@ private static void validateInput() throws IOException {
* This output file should contain all the information needed to generate java counterparts to the traversed js classes.
* */
private static void runJsParser() {
String parserPath = Paths.get(System.getProperty("user.dir"), "jsparser", "js_parser.js").toString();
Path jsParserPath = Paths.get(System.getProperty("user.dir"), "jsparser", "js_parser.js");
extractResource(jsParserPath, "js_parser.js");
String parserPath = jsParserPath.toString();
NodeJSProcess nodeJSProcess = new NodeJSProcessImpl(new ProcessExecutorImpl(), new EnvironmentVariablesReaderImpl());
int exitCode = nodeJSProcess.runScript(parserPath);

Expand All @@ -141,6 +150,40 @@ private static void runJsParser() {
}
}

private static void extractResource(Path savePath, String resourceName) {
File jsParserFile = savePath.toFile();
if (!jsParserFile.exists()) {
try {
jsParserFile.getParentFile().mkdirs();
jsParserFile.createNewFile();
InputStream source = Main.class.getResourceAsStream("/" + resourceName);
if (source == null) {
throw new RuntimeException(resourceName + " not found in resources");
}
FileUtils.copyInputStreamToFile(source, jsParserFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

private static void maybeExtractJsParserSource(Path jsParserPath) {
File jsParserFile = jsParserPath.toFile();
if (!jsParserFile.exists()) {
try {
jsParserFile.getParentFile().mkdirs();
jsParserFile.createNewFile();
InputStream source = Main.class.getResourceAsStream("/js_parser.js");
if (source == null) {
throw new RuntimeException("js_parser.js not found in resources");
}
FileUtils.copyInputStreamToFile(source, jsParserFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

private static Boolean rootTraversed = false;

private static void traverseDirectory(File currentDir, boolean traverseExplicitly) throws IOException, JSONException {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (global.__onLiveSync) {
global.__onLiveSync();
}