Skip to content

Commit 496fcbb

Browse files
vmutafovedusperoni
authored andcommitted
feat: support android embedding
1 parent d4b7164 commit 496fcbb

Some content is hidden

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

41 files changed

+552
-18
lines changed

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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
{

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

Lines changed: 1 addition & 1 deletion
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 {
Lines changed: 6 additions & 0 deletions
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/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Main.java

Lines changed: 44 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
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)