Skip to content

Commit 6776833

Browse files
committed
feat: support android embedding
1 parent 643958b commit 6776833

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+632
-23
lines changed

build-artifacts/project-template-gradle/settings.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
pluginManagement {
2+
repositories {
3+
gradlePluginPortal()
4+
mavenLocal()
5+
}
6+
}
7+
18
rootProject.name = "__PROJECT_NAME__"
29
include ':app'//, ':runtime', ':runtime-binding-generator'
310

build.gradle

+8
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ task copyFilesToProjectTemeplate {
260260
from "$TEST_APP_PATH/app/src/main/java/com/tns/internal"
261261
into "$DIST_FRAMEWORK_PATH/app/src/main/java/com/tns/internal"
262262
}
263+
copy {
264+
from "$TEST_APP_PATH/app/src/main/java/com/tns/embedding"
265+
into "$DIST_FRAMEWORK_PATH/app/src/main/java/com/tns/embedding"
266+
}
263267
copy {
264268
from "$BUILD_TOOLS_PATH/static-binding-generator/build/libs/static-binding-generator.jar"
265269
into "$DIST_FRAMEWORK_PATH/build-tools"
@@ -295,6 +299,10 @@ task copyFilesToProjectTemeplate {
295299
from "$TEST_APP_PATH/app/build.gradle"
296300
into "$DIST_FRAMEWORK_PATH/app"
297301
}
302+
copy {
303+
from "$TEST_APP_PATH/app/nativescript.gradle"
304+
into "$DIST_FRAMEWORK_PATH/app"
305+
}
298306
copy {
299307
from "$TEST_APP_PATH/build.gradle"
300308
into "$DIST_FRAMEWORK_PATH"

test-app/app/src/main/AndroidManifest.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
<application
1111
android:requestLegacyExternalStorage="true"
12-
android:name="com.tns.NativeScriptApplication"
12+
android:name="com.tns.TestNativeScriptApplication"
1313
android:allowBackup="true"
1414
android:icon="@mipmap/ic_launcher"
1515
android:label="@string/app_name"
1616
android:roundIcon="@mipmap/ic_launcher_round"
1717
android:supportsRtl="true"
1818
android:theme="@style/AppTheme">
19-
<activity android:name="com.tns.NativeScriptActivity" android:exported="true">
19+
<activity android:name="com.tns.TestNativeScriptActivity" android:exported="true">
2020
<intent-filter>
2121
<action android:name="android.intent.action.MAIN" />
2222
<category android:name="android.intent.category.LAUNCHER" />

test-app/app/src/main/assets/app/MyActivity.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var MyActivity = (function (_super) {
6262

6363
};
6464
MyActivity = __decorate([
65-
JavaProxy("com.tns.NativeScriptActivity")
65+
JavaProxy("com.tns.TestNativeScriptActivity")
6666
], MyActivity);
6767
return MyActivity;
6868
})(android.app.Activity);

test-app/app/src/main/assets/app/MyApp.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// demonstrates how to extend class in JavaScript with prebuilt Java proxy
2-
var MyApp = android.app.Application.extend("com.tns.NativeScriptApplication",
2+
var MyApp = android.app.Application.extend("com.tns.TestNativeScriptApplication",
33
{
44
onCreate: function()
55
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* AUTO-GENERATED FILE. DO NOT MODIFY.
2+
* This class was automatically generated by the
3+
* static binding generator from the resources it found.
4+
* Please do not modify by hand.
5+
*/
6+
package com.tns;
7+
8+
@com.tns.JavaScriptImplementation(javaScriptFile = "./MyActivity.js")
9+
public class TestNativeScriptActivity extends android.app.Activity
10+
implements com.tns.NativeScriptHashCodeProvider {
11+
public TestNativeScriptActivity() {
12+
super();
13+
com.tns.Runtime.initInstance(this);
14+
}
15+
16+
protected void onCreate(android.os.Bundle param_0) {
17+
java.lang.Object[] args = new java.lang.Object[1];
18+
args[0] = param_0;
19+
com.tns.Runtime.callJSMethod(this, "onCreate", void.class, args);
20+
}
21+
22+
public void onCreate(android.os.Bundle param_0, android.os.PersistableBundle param_1) {
23+
java.lang.Object[] args = new java.lang.Object[2];
24+
args[0] = param_0;
25+
args[1] = param_1;
26+
com.tns.Runtime.callJSMethod(this, "onCreate", void.class, args);
27+
}
28+
29+
public int hashCode__super() {
30+
return super.hashCode();
31+
}
32+
33+
public boolean equals__super(java.lang.Object other) {
34+
return super.equals(other);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* AUTO-GENERATED FILE. DO NOT MODIFY.
2+
* This class was automatically generated by the
3+
* static binding generator from the resources it found.
4+
* Please do not modify by hand.
5+
*/
6+
package com.tns;
7+
8+
@com.tns.JavaScriptImplementation(javaScriptFile = "./MyApp.js")
9+
public class TestNativeScriptApplication extends android.app.Application
10+
implements com.tns.NativeScriptHashCodeProvider {
11+
private static android.app.Application thiz;
12+
13+
public TestNativeScriptApplication() {
14+
super();
15+
thiz = this;
16+
}
17+
18+
public void onCreate() {
19+
com.tns.Runtime runtime = com.tns.RuntimeHelper.initRuntime(this);
20+
if (!com.tns.Runtime.isInitialized()) {
21+
try {
22+
super.onCreate();
23+
return;
24+
} catch (Throwable throwable) {
25+
throw new RuntimeException(throwable);
26+
}
27+
}
28+
java.lang.Object[] args = new java.lang.Object[0];
29+
com.tns.Runtime.callJSMethod(this, "onCreate", void.class, args);
30+
if (runtime != null) {
31+
runtime.run();
32+
}
33+
}
34+
35+
public static android.app.Application getInstance() {
36+
return thiz;
37+
}
38+
39+
public int hashCode__super() {
40+
return super.hashCode();
41+
}
42+
43+
public boolean equals__super(java.lang.Object other) {
44+
return super.equals(other);
45+
}
46+
}

test-app/app/src/main/java/com/tns/tests/StringConversionTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void callback(String str) {
7171
private String readString() throws Exception {
7272
String str = null;
7373

74-
Context context = com.tns.NativeScriptApplication.getInstance();
74+
Context context = com.tns.TestNativeScriptApplication.getInstance();
7575

7676
InputStream inputStream = null;
7777
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
</androidx.constraintlayout.widget.ConstraintLayout>

test-app/build-tools/android-metadata-generator/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apply plugin: 'java'
22
apply plugin: 'kotlin'
33

4-
sourceCompatibility = JavaVersion.VERSION_1_8
5-
targetCompatibility = JavaVersion.VERSION_1_8
4+
sourceCompatibility = JavaVersion.VERSION_11
5+
targetCompatibility = JavaVersion.VERSION_11
66

77
// todo: check if still needed
88
// if(!project.hasProperty("loadedProjectDeps")){

test-app/build-tools/static-binding-generator/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'java-library'
22

3-
sourceCompatibility = JavaVersion.VERSION_1_8
4-
targetCompatibility = JavaVersion.VERSION_1_8
3+
sourceCompatibility = JavaVersion.VERSION_11
4+
targetCompatibility = JavaVersion.VERSION_11
55

66
// todo: check if still needed
77
// if(!project.hasProperty("loadedProjectDeps")){

test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Main.java

+44-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.nativescript.staticbindinggenerator;
22

33
import org.apache.commons.io.FileUtils;
4+
import org.apache.commons.io.IOUtils;
45
import org.json.JSONArray;
56
import org.json.JSONException;
67
import org.json.JSONObject;
@@ -11,8 +12,10 @@
1112

1213
import java.io.File;
1314
import java.io.IOException;
15+
import java.io.InputStream;
1416
import java.io.PrintWriter;
1517
import java.nio.charset.Charset;
18+
import java.nio.file.Path;
1619
import java.nio.file.Paths;
1720
import java.util.ArrayList;
1821
import java.util.List;
@@ -112,6 +115,10 @@ private static void validateInput() throws IOException {
112115

113116
List<DataRow> inputFile = Generator.getRows(SBG_INPUT_FILE);
114117
inputDir = new File(inputFile.get(0).getRow());
118+
Path assetsInternalDirPath = inputDir.getParentFile().toPath().resolve("internal");
119+
extractResource(assetsInternalDirPath.resolve("ts_helpers.js"), "ts_helpers.js");
120+
extractResource(assetsInternalDirPath.resolve("livesync.js"), "livesync.js");
121+
115122
webpackWorkersExcludePath = Paths.get(inputDir.getAbsolutePath(), "__worker-chunks.json").toString();
116123

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

@@ -141,6 +150,40 @@ private static void runJsParser() {
141150
}
142151
}
143152

153+
private static void extractResource(Path savePath, String resourceName) {
154+
File jsParserFile = savePath.toFile();
155+
if (!jsParserFile.exists()) {
156+
try {
157+
jsParserFile.getParentFile().mkdirs();
158+
jsParserFile.createNewFile();
159+
InputStream source = Main.class.getResourceAsStream("/" + resourceName);
160+
if (source == null) {
161+
throw new RuntimeException(resourceName + " not found in resources");
162+
}
163+
FileUtils.copyInputStreamToFile(source, jsParserFile);
164+
} catch (IOException e) {
165+
throw new RuntimeException(e);
166+
}
167+
}
168+
}
169+
170+
private static void maybeExtractJsParserSource(Path jsParserPath) {
171+
File jsParserFile = jsParserPath.toFile();
172+
if (!jsParserFile.exists()) {
173+
try {
174+
jsParserFile.getParentFile().mkdirs();
175+
jsParserFile.createNewFile();
176+
InputStream source = Main.class.getResourceAsStream("/js_parser.js");
177+
if (source == null) {
178+
throw new RuntimeException("js_parser.js not found in resources");
179+
}
180+
FileUtils.copyInputStreamToFile(source, jsParserFile);
181+
} catch (IOException e) {
182+
throw new RuntimeException(e);
183+
}
184+
}
185+
}
186+
144187
private static Boolean rootTraversed = false;
145188

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

test-app/build-tools/static-binding-generator/src/main/resources/js_parser.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if (global.__onLiveSync) {
2+
global.__onLiveSync();
3+
}

0 commit comments

Comments
 (0)