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: ability to embed into platform host projects #1799

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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'

12 changes: 10 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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"
@@ -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"
2 changes: 0 additions & 2 deletions test-app/.gitignore
Original file line number Diff line number Diff line change
@@ -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
@@ -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" />
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
@@ -12,7 +12,7 @@
}
}

@JavaProxy("com.tns.NativeScriptActivity")
@JavaProxy("com.tns.TestNativeScriptActivity")
class MyActivity extends android.app.Activity
{
onCreate(bundle: android.os.Bundle)
@@ -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()
{
Original file line number Diff line number Diff line change
@@ -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 {
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>
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;
@@ -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;
@@ -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()) {
@@ -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);

@@ -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 {

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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
(function () {

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length;
var r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;

if (typeof global.Reflect === "object" && typeof global.Reflect.decorate === "function") {
r = global.Reflect.decorate(decorators, target, key, desc);
}
else {
for (var i = decorators.length - 1; i >= 0; i--) {
if (d = decorators[i]) {
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
}
}
}
return c > 3 && r && Object.defineProperty(target, key, r), r;
};

// For backward compatibility.
var __native = function (thiz) {
// we are setting the __container__ property to the base class when the super method is called
// if the constructor returns the __native(this) call we will use the old implementation
// copying all the properties to the result
// otherwise if we are using the result from the super() method call we won't need such logic
// as thiz already contains the parent properties
// this way we now support both implementations in typescript generated constructors:
// 1: super(); return __native(this);
// 2: return super() || this;
if (thiz.__container__) {
var result = thiz.__proto__;

for (var prop in thiz) {
if (thiz.hasOwnProperty(prop)) {
thiz.__proto__[prop] = thiz[prop];
delete thiz[prop];
}
}

thiz.constructor = undefined;
thiz.__proto__ = undefined;
Object.freeze(thiz);
Object.preventExtensions(thiz);
return result;
} else {
return thiz;
}
};

var __extends = function (Child, Parent) {
var extendNativeClass = !!Parent.extend && (Parent.extend.toString().indexOf("[native code]") > -1);
if (!extendNativeClass) {
__extends_ts(Child, Parent);
return;
}
if (Parent.__isPrototypeImplementationObject) {
throw new Error("Can not extend an already extended native object.");
}

function extend(thiz) {
var child = thiz.__proto__.__child;
if (!child.__extended) {
var parent = thiz.__proto__.__parent;
child.__extended = parent.extend(child.name, child.prototype, true);
// This will deal with "i instanceof child"
child[Symbol.hasInstance] = function (instance) {
return instance instanceof this.__extended;
}
}
return child.__extended;
};

Parent.__activityExtend = function (parent, name, implementationObject) {
__log("__activityExtend called");
return parent.extend(name, implementationObject);
};

Parent.call = function (thiz) {
var Extended = extend(thiz);
thiz.__container__ = true;
if (arguments.length > 1) {
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(Array.prototype.slice.call(arguments, 1))));
}
else {
thiz.__proto__ = new Extended()
}
return thiz.__proto__;
};

Parent.apply = function (thiz, args) {
var Extended = extend(thiz);
thiz.__container__ = true;
if (args && args.length > 0) {
thiz.__proto__ = new (Function.prototype.bind.apply(Extended, [null].concat(args)));
}
else {
thiz.__proto__ = new Extended();
}
return thiz.__proto__;
};
__extends_ns(Child, Parent);
Child.__isPrototypeImplementationObject = true;
Child.__proto__ = Parent;
Child.prototype.__parent = Parent;
Child.prototype.__child = Child;
}

var __extends_ts = function (child, parent) {
extendStaticFunctions(child, parent);
assignPrototypeFromParentToChild(parent, child);
};

var __extends_ns = function (child, parent) {
if (!parent.extend) {
assignPropertiesFromParentToChild(parent, child);
}

assignPrototypeFromParentToChild(parent, child);
};

var extendStaticFunctions =
Object.setPrototypeOf
|| (hasInternalProtoProperty() && function (child, parent) { child.__proto__ = parent; })
|| assignPropertiesFromParentToChild;

function hasInternalProtoProperty() {
return { __proto__: [] } instanceof Array;
}

function assignPropertiesFromParentToChild(parent, child) {
for (var property in parent) {
if (parent.hasOwnProperty(property)) {
child[property] = parent[property];
}
}
}

function assignPrototypeFromParentToChild(parent, child) {
function __() {
this.constructor = child;
}

if (parent === null) {
child.prototype = Object.create(null);
} else {
__.prototype = parent.prototype;
child.prototype = new __();
}
}


function JavaProxy(className) {
return function (target) {
var extended = target.extend(className, target.prototype)
extended.name = className;
return extended;
};
}

function Interfaces(interfacesArr) {
return function (target) {
if (interfacesArr instanceof Array) {
// attach interfaces: [] to the object
target.prototype.interfaces = interfacesArr;
}
}
}

Object.defineProperty(global, "__native", { value: __native });
Object.defineProperty(global, "__extends", { value: __extends });
Object.defineProperty(global, "__decorate", { value: __decorate });

global.JavaProxy = JavaProxy;
global.Interfaces = Interfaces;
})()
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ public void testParseNamedExtend() throws IOException {
Assert.assertEquals(row.getColumn(), "");
Assert.assertEquals(row.getNewClassName(), "");
Assert.assertEquals(row.getMethods()[0], "toString");
Assert.assertEquals(row.getFilename(), "com.tns.NativeScriptActivity");
Assert.assertEquals(row.getFilename(), "com.tns.TestNativeScriptActivity");
Assert.assertEquals(row.getJsFilename(), "MyActivity.js");
Assert.assertEquals(row.getInterfaces()[0], "");
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
java.lang.Object*****toString*com.tns.NativeScriptActivity*MyActivity.js*
java.lang.Object*****toString*com.tns.TestNativeScriptActivity*MyActivity.js*
4 changes: 2 additions & 2 deletions test-app/runtests.gradle
Original file line number Diff line number Diff line change
@@ -80,9 +80,9 @@ task startInstalledApk(type: Exec) {
println "Starting test application"

if (isWinOs) {
commandLine "cmd", "/c", "adb", runOnDeviceOrEmulator, "-e", "shell", "am", "start", "-n", "com.tns.testapplication/com.tns.NativeScriptActivity", "-a", "android.intent.action.MAIN", "-c", "android.intent.category.LAUNCHER"
commandLine "cmd", "/c", "adb", runOnDeviceOrEmulator, "-e", "shell", "am", "start", "-n", "com.tns.testapplication/com.tns.TestNativeScriptActivity", "-a", "android.intent.action.MAIN", "-c", "android.intent.category.LAUNCHER"
} else {
commandLine "adb", runOnDeviceOrEmulator, "-e", "shell", "am", "start", "-n", "com.tns.testapplication/com.tns.NativeScriptActivity", "-a", "android.intent.action.MAIN", "-c", "android.intent.category.LAUNCHER"
commandLine "adb", runOnDeviceOrEmulator, "-e", "shell", "am", "start", "-n", "com.tns.testapplication/com.tns.TestNativeScriptActivity", "-a", "android.intent.action.MAIN", "-c", "android.intent.category.LAUNCHER"
}
}
}
57 changes: 52 additions & 5 deletions test-app/runtime/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id 'maven-publish'
}

apply plugin: 'com.android.library'

def optimized = project.hasProperty("optimized")
@@ -73,8 +77,12 @@ base {
android {
namespace "com.tns.android_runtime"

namespace = 'org.nativescript.android.runtime'

namespace = 'org.nativescript.android.runtime'

compileSdk NS_DEFAULT_COMPILE_SDK_VERSION as int
buildToolsVersion = NS_DEFAULT_BUILD_TOOLS_VERSION as String
buildToolsVersion NS_DEFAULT_BUILD_TOOLS_VERSION as String

sourceSets {
main {
@@ -93,6 +101,10 @@ android {
}

defaultConfig {
aarMetadata {
minCompileSdk = NS_DEFAULT_MIN_SDK_VERSION as int
}

minSdkVersion NS_DEFAULT_MIN_SDK_VERSION as int
targetSdkVersion NS_DEFAULT_COMPILE_SDK_VERSION as int

@@ -141,24 +153,59 @@ android {
}
}

publishing {
singleVariant('release') {
withSourcesJar()
}
}
}

allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile).tap {
configureEach {
options.compilerArgs << "-Xlint:all" << "-Werror"
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:all" << "-Werror"
}
}
}

publishing {
publications {
release(MavenPublication) {
groupId = 'org.nativescript.android'
artifactId = 'runtime'
version = '1.0-SNAPSHOT'

afterEvaluate {
from components.release
}
}
}
repositories {
mavenLocal()
}
}

dependencies {
// println "\t ~ [DEBUG][runtime] build.gradle - ns_default_junit_version = ${ns_default_junit_version}..."
def androidXAppCompatVersion = "${ns_default_androidx_appcompat_version}"
if (project.hasProperty("androidXAppCompat")) {
androidXAppCompatVersion = androidXAppCompat
outLogger.withStyle(Style.SuccessHeader).println "\t + using android X library androidx.appcompat:appcompat:$androidXAppCompatVersion"
}

def androidXMaterialVersion = "${ns_default_androidx_material_version}"
if (project.hasProperty("androidXMaterial")) {
androidXMaterialVersion = androidXMaterial
outLogger.withStyle(Style.SuccessHeader).println "\t + using android X library com.google.android.material:material:$androidXMaterialVersion"
}

implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation "junit:junit:${ns_default_junit_version}"
testImplementation "org.mockito:mockito-core:${ns_default_mockito_core_version}"
implementation "com.google.android.material:material:$androidXMaterialVersion"
implementation "androidx.appcompat:appcompat:$androidXAppCompatVersion"
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
Comment on lines +204 to +207
Copy link
Collaborator

Choose a reason for hiding this comment

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

are these needed? It'd mean all apps now depend on material, androidx navigation and appcompat

implementation 'androidx.preference:preference:1.2.1'
}


Original file line number Diff line number Diff line change
@@ -32,9 +32,10 @@
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

@@ -59,7 +60,7 @@ class ErrorReport implements TabLayout.OnTabSelectedListener {
private static AppCompatActivity activity;

private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPager2 viewPager;
private Context context;

private static String exceptionMsg;
@@ -279,12 +280,12 @@ void buildUI() {
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
int pagerId = this.context.getResources().getIdentifier("pager", "id", this.context.getPackageName());

viewPager = (ViewPager) activity.findViewById(pagerId);
viewPager = (ViewPager2) activity.findViewById(pagerId);

Pager adapter = new Pager(activity.getSupportFragmentManager(), tabLayout.getTabCount());
Pager adapter = new Pager(activity, tabLayout.getTabCount());

viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

@@ -332,18 +333,18 @@ public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

private class Pager extends FragmentStatePagerAdapter {
private class Pager extends FragmentStateAdapter {

int tabCount;

@SuppressWarnings("deprecation")
public Pager(FragmentManager fm, int tabCount) {
public Pager(FragmentActivity fm, int tabCount) {
super(fm);
this.tabCount = tabCount;
}

@Override
public Fragment getItem(int position) {
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new ExceptionTab();
@@ -355,7 +356,7 @@ public Fragment getItem(int position) {
}

@Override
public int getCount() {
public int getItemCount() {
return tabCount;
}
}
@@ -410,14 +411,14 @@ public static void restartApp(Context context) {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int exceptionTabId = container.getContext().getResources().getIdentifier("exception_tab", "layout", container.getContext().getPackageName());
int exceptionTabId = getContext().getResources().getIdentifier("exception_tab", "layout", getContext().getPackageName());
View view = inflater.inflate(exceptionTabId, container, false);

int errorExceptionViewId = activity.getResources().getIdentifier("errorException", "id", activity.getPackageName());
TextView errorExceptionView = (TextView) activity.findViewById(errorExceptionViewId);
errorExceptionView.setMovementMethod(new ScrollingMovementMethod());

int errorStackTraceViewId = container.getContext().getResources().getIdentifier("errorStacktrace", "id", container.getContext().getPackageName());
int errorStackTraceViewId = getContext().getResources().getIdentifier("errorStacktrace", "id", getContext().getPackageName());
TextView errorStackTraceView = (TextView) view.findViewById(errorStackTraceViewId);

String[] exceptionParts = exceptionMsg.split("StackTrace:");
@@ -438,10 +439,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
errorStackTraceView.setMovementMethod(LinkMovementMethod.getInstance());
errorStackTraceView.setEnabled(true);

int btnCopyExceptionId = container.getContext().getResources().getIdentifier("btnCopyException", "id", container.getContext().getPackageName());
int btnCopyExceptionId = getContext().getResources().getIdentifier("btnCopyException", "id", getContext().getPackageName());
Button copyToClipboard = (Button) view.findViewById(btnCopyExceptionId);

int btnRestartAppId = container.getContext().getResources().getIdentifier("btnRestartApp", "id", container.getContext().getPackageName());
int btnRestartAppId = getContext().getResources().getIdentifier("btnRestartApp", "id", getContext().getPackageName());
Button restartApp = (Button) view.findViewById(btnRestartAppId);
restartApp.setOnClickListener(v -> {
restartApp(getContext().getApplicationContext());
@@ -459,18 +460,18 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
public static class LogcatTab extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int logcatTabId = container.getContext().getResources().getIdentifier("logcat_tab", "layout", container.getContext().getPackageName());
int logcatTabId = getContext().getResources().getIdentifier("logcat_tab", "layout", getContext().getPackageName());
View view = inflater.inflate(logcatTabId, container, false);

int textViewId = container.getContext().getResources().getIdentifier("logcatMsg", "id", container.getContext().getPackageName());
int textViewId = getContext().getResources().getIdentifier("logcatMsg", "id", getContext().getPackageName());
TextView txtlogcatMsg = (TextView) view.findViewById(textViewId);
txtlogcatMsg.setText(logcatMsg);

txtlogcatMsg.setMovementMethod(new ScrollingMovementMethod());

final String logName = "Log-" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());

int btnCopyLogcatId = container.getContext().getResources().getIdentifier("btnCopyLogcat", "id", container.getContext().getPackageName());
int btnCopyLogcatId = getContext().getResources().getIdentifier("btnCopyLogcat", "id", getContext().getPackageName());
Button copyToClipboard = (Button) view.findViewById(btnCopyLogcatId);
copyToClipboard.setOnClickListener(new View.OnClickListener() {
@Override
@@ -498,7 +499,7 @@ public void onClick(View v) {
} catch (Exception e) {
String err = "Could not write logcat report to sdcard. Make sure you have allowed access to external storage!";
Toast.makeText(activity, err, Toast.LENGTH_LONG).show();
if (Util.isDebuggableApp(container.getContext())) {
if (Util.isDebuggableApp(getContext())) {
e.printStackTrace();
}
}
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
9 changes: 9 additions & 0 deletions test-app/runtime/src/debug/res/drawable/button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#44403c" />
<corners android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:topRightRadius="8dp"
android:topLeftRadius="8dp"/>
</shape>
9 changes: 9 additions & 0 deletions test-app/runtime/src/debug/res/drawable/button_accented.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/nativescript_blue" />
<corners android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:topRightRadius="8dp"
android:topLeftRadius="8dp"/>
</shape>
Original file line number Diff line number Diff line change
@@ -35,14 +35,14 @@
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />

<androidx.viewpager.widget.ViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/toolbar"
android:scrollbarAlwaysDrawVerticalTrack="false">

</androidx.viewpager.widget.ViewPager>
</androidx.viewpager2.widget.ViewPager2>

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
Original file line number Diff line number Diff line change
@@ -45,24 +45,26 @@

<Button
android:id="@+id/btnRestartApp"
android:text="Restart APP"
android:textAlignment="center"
android:textColor="@android:color/white"
android:background="@drawable/button_accented"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_marginRight="10dp" />
android:background="@drawable/button_accented"
android:foreground="?android:attr/selectableItemBackground"
android:text="Restart APP"
android:textAlignment="center"
android:textColor="@android:color/white" />

<Button
android:id="@+id/btnCopyException"
android:textColor="@android:color/white"
android:text="Copy"
android:background="@drawable/button"
android:textAlignment="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:layout_weight="1"
android:background="@drawable/button"
android:foreground="?android:attr/selectableItemBackground"
android:text="Copy"
android:textAlignment="center"
android:textColor="@android:color/white" />


</LinearLayout>
Original file line number Diff line number Diff line change
@@ -21,18 +21,19 @@

<Button
android:id="@+id/btnCopyLogcat"
android:layout_height="wrap_content"
android:text="Copy to sdcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/button"
android:textColor="@android:color/white"
android:textAlignment="center"
android:foreground="?android:attr/selectableItemBackground"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Copy to sdcard"
android:textAlignment="center"
android:textColor="@android:color/white"
tools:layout_width="match_parent" />

</LinearLayout>
7 changes: 7 additions & 0 deletions test-app/runtime/src/debug/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="nativescript_blue">#65ADF1</color>
<color name="gray">#1c1917</color>
<color name="red">#dc2626</color>
<color name="white">#ffffff</color>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns;

public class NativeScriptActivity extends org.nativescript.NativeScriptActivity {

}
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.preference.PreferenceManager;
import androidx.preference.PreferenceManager;
import android.util.Log;

import java.io.File;
@@ -68,6 +68,8 @@ public static Runtime initRuntime(Context context) {
ManualInstrumentation.Frame loadLibraryFrame = ManualInstrumentation.start("loadLibrary NativeScript");
try {
System.loadLibrary("NativeScript");
} catch (Exception e) {
throw e;
} finally {
loadLibraryFrame.close();
}
Original file line number Diff line number Diff line change
@@ -2,18 +2,17 @@

import java.io.*;

import com.tns.internal.Plugin;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;

import androidx.core.content.pm.PackageInfoCompat;

import com.tns.internal.Plugin;

public final class Util {
private Util() {
}
@@ -57,7 +56,7 @@ static boolean runPlugin(Logger logger, Context context) {

try {
Class<?> liveSyncPluginClass = Class.forName(pluginClassName);
Plugin p = (Plugin) liveSyncPluginClass.newInstance();
Plugin p = (Plugin) liveSyncPluginClass.getDeclaredConstructor().newInstance();
success = p.execute(context);
} catch (Exception e) {
if (Util.isDebuggableApp(context) && logger.isEnabled()) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.tns.embedding;

import android.app.Application;

public class ApplicationHolder {

private static Application application;

private ApplicationHolder() {

}

public static void setInstance(Application application) {
ApplicationHolder.application = application;
}

public static Application getInstance() {
return application;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.tns.embedding;

public class CallbacksStore {

private static EmbeddableFragmentCallbacks fragmentCallbacks;
private static EmbeddableActivityCallbacks activityCallbacks;

public static void setFragmentCallbacks(EmbeddableFragmentCallbacks callbacks) {
fragmentCallbacks = callbacks;
}

public static void setActivityCallbacks(EmbeddableActivityCallbacks callbacks) {
activityCallbacks = callbacks;
}

public static EmbeddableFragmentCallbacks getFragmentCallbacks() {
if (fragmentCallbacks == null) {
throw new RuntimeException("{N} Core modules: Fragment callbacks are null");
}
return fragmentCallbacks;
}

public static EmbeddableActivityCallbacks getActivityCallbacks() {
if (activityCallbacks == null) {
throw new RuntimeException("{N} Core modules: Activity callbacks are null");
}
return activityCallbacks;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tns.embedding;

import androidx.appcompat.app.AppCompatActivity;

public abstract class EmbeddableActivityCallbacks {

private AppCompatActivity activity;

public void setActivity(AppCompatActivity activity) {
this.activity = activity;
}

public AppCompatActivity getActivity() {
return this.activity;
}

public abstract void onPostResume();

public abstract void onStart();

public abstract void onStop();

public abstract void onDestroy();

public abstract void onActivityResult(int param_0, int param_1, android.content.Intent param_2);

public abstract void onCreate(android.os.Bundle param_0);

public abstract void onRequestPermissionsResult(int param_0, java.lang.String[] param_1, int[] param_2);

public abstract void onSaveInstanceState(android.os.Bundle param_0);

public abstract void onBackPressed();

public abstract void onNewIntent(android.content.Intent param_0);

public abstract void onCreate(android.os.Bundle param_0, android.os.PersistableBundle param_1);

public abstract void onSaveInstanceState(android.os.Bundle param_0, android.os.PersistableBundle param_1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.tns.embedding;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public abstract class EmbeddableFragmentCallbacks {

private Fragment fragment;

public void setFragment(Fragment fragment) {
this.fragment = fragment;
}

public Fragment getFragment() {
return fragment;
}

public abstract View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
);

public abstract void onResume();

public abstract void onPause();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.tns.embedding;

import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;

public class JetpackNavigationUtils {
public static void navigate(View view, String route) {
NavController nav = Navigation.findNavController(view);
nav.navigate(route);
}
}
Original file line number Diff line number Diff line change
@@ -2,4 +2,4 @@

public interface Plugin {
boolean execute(android.content.Context context) throws Exception;
}
}
22 changes: 22 additions & 0 deletions test-app/runtime/src/main/java/org/nativescript/Bootstrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.nativescript;

import android.app.Application;

import com.tns.ManualInstrumentation;
import com.tns.RuntimeHelper;
import com.tns.embedding.ApplicationHolder;

public class Bootstrap {
public static void bootstrapNativeScriptRuntime(Application application) {
ManualInstrumentation.Frame frame = ManualInstrumentation.start("NativeScriptApplication.onCreate");
try {
ApplicationHolder.setInstance(application);
com.tns.Runtime runtime = RuntimeHelper.initRuntime(application);
if (runtime != null) {
runtime.run();
}
} finally {
frame.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.nativescript;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.tns.embedding.CallbacksStore;
import com.tns.embedding.EmbeddableActivityCallbacks;

import org.nativescript.android.runtime.R;

public class NativeScriptActivity extends AppCompatActivity {

private final EmbeddableActivityCallbacks callbacks = CallbacksStore.getActivityCallbacks();
static View nativeScriptView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callbacks.setActivity(this);
callbacks.onCreate(savedInstanceState);
// drawNativeScriptApp(savedInstanceState);
}

@Override
public void onCreate(android.os.Bundle savedInstanceState, android.os.PersistableBundle persistableBundle) {
super.onCreate(savedInstanceState, persistableBundle);
callbacks.setActivity(this);
callbacks.onCreate(savedInstanceState, persistableBundle);
// drawNativeScriptApp(savedInstanceState);
}

private void drawNativeScriptApp(Bundle savedInstanceState) {
setContentView(R.layout.main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new NativeScriptFragment())
.commitNow();
}
}

@Override
protected void onPostResume() {
super.onPostResume();
callbacks.onPostResume();
}

@Override
protected void onStart() {
super.onStart();
callbacks.onStart();
}

@Override
protected void onStop() {
super.onStop();
callbacks.onStop();
}

@Override
protected void onDestroy() {
try {
callbacks.onDestroy();
nativeScriptView = null;
} finally {
super.onDestroy();
}
}

@Override
protected void onActivityResult(int param_0, int param_1, android.content.Intent param_2) {
super.onActivityResult(param_0, param_1, param_2);
callbacks.onActivityResult(param_0, param_1, param_2);
}

@SuppressLint("MissingSuperCall")
@Override
public void onRequestPermissionsResult(int param_0, java.lang.String[] param_1, int[] param_2) {
callbacks.onRequestPermissionsResult(param_0, param_1, param_2);
}

@Override
protected void onSaveInstanceState(android.os.Bundle param_0) {
super.onSaveInstanceState(param_0);
callbacks.onSaveInstanceState(param_0);
}

@Override
public void onBackPressed() {
callbacks.onBackPressed();
}

@Override
protected void onNewIntent(android.content.Intent param_0) {
super.onNewIntent(param_0);
super.setIntent(param_0);
callbacks.onNewIntent(param_0);
}

@Override
public void onSaveInstanceState(android.os.Bundle param_0, android.os.PersistableBundle param_1) {
super.onSaveInstanceState(param_0, param_1);
}

public void superOnBackPressed() {
super.onBackPressed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.nativescript;

import static org.nativescript.Bootstrap.bootstrapNativeScriptRuntime;

import android.app.Application;

public class NativeScriptApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
bootstrapNativeScriptRuntime(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.nativescript;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.tns.embedding.CallbacksStore;
import com.tns.embedding.EmbeddableFragmentCallbacks;

public class NativeScriptFragment extends Fragment {

private final EmbeddableFragmentCallbacks callbacks = CallbacksStore.getFragmentCallbacks();

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callbacks.setFragment(this);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
if (NativeScriptActivity.nativeScriptView == null) {
NativeScriptActivity.nativeScriptView = callbacks.onCreateView(inflater, container, savedInstanceState);
}

FrameLayout wrapper = new FrameLayout(requireContext());
wrapper.addView(NativeScriptActivity.nativeScriptView);
return wrapper;
}

@Override
public void onResume() {
super.onResume();
callbacks.onResume();
}

@Override
public void onPause() {
super.onPause();
callbacks.onPause();
}

@Override
public void onDestroyView() {
super.onDestroyView();
if (NativeScriptActivity.nativeScriptView != null && NativeScriptActivity.nativeScriptView.getParent() != null) {
((ViewGroup) NativeScriptActivity.nativeScriptView.getParent()).removeView(NativeScriptActivity.nativeScriptView);
}
}
}
7 changes: 7 additions & 0 deletions test-app/runtime/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</androidx.constraintlayout.widget.ConstraintLayout>
7 changes: 7 additions & 0 deletions test-app/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
pluginManagement {
repositories {
gradlePluginPortal()
mavenLocal()
}
}

include ':app',
':runtime',
':runtime-binding-generator',