在本文中,我们将带你了解ReactNative是否可以将JavaScript编译为Android的Java?在这篇文章中,我们将为您详细介绍ReactNative是否可以将JavaScript编译为A
在本文中,我们将带你了解React Native是否可以将JavaScript编译为Android的Java?在这篇文章中,我们将为您详细介绍React Native是否可以将JavaScript编译为Android的Java?的方方面面,并解答react native 编译常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的android – React Native Maps错误编译debugJavaWithJavac、android – 如何理解React Native中“向Javascript发送事件”中的“ReactContext”、android – 对Nativescript插件中的Java对象无效’this’、android – 有没有在react-native中实现JavaScriptModule的指南?。
本文目录一览:- React Native是否可以将JavaScript编译为Android的Java?(react native 编译)
- android – React Native Maps错误编译debugJavaWithJavac
- android – 如何理解React Native中“向Javascript发送事件”中的“ReactContext”
- android – 对Nativescript插件中的Java对象无效’this’
- android – 有没有在react-native中实现JavaScriptModule的指南?
React Native是否可以将JavaScript编译为Android的Java?(react native 编译)
当我使用React Native开发混合应用程序时。从React Native代码创建Android-
App时,我编写的JavaScript代码会转换为Dalvik / ART
Runtime的Java代码还是Java字节代码?还是只是将UI组件编译为本机UI组件?还是像Fetch
API这样的库将JavaScript代码编译为Java代码或Java字节码?
答案1
小编典典基本上,您编写Javascript。Javascript与本机组件(Android上的Java,iOS上的Objective
C,Windows上的C#)进行通信。
通信通过所谓的“桥”进行。如果您在任何时候觉得这种通信使速度降低太多,可以选择分别用Java,Objective
C或C#实现Javascript功能,以便纯本机运行。在这种情况下,您是直接用本机代码编写的,因此本机编译没有Javascript。
这将牺牲性能的兼容性。通常,这不是必需的。
- 进一步阅读
了解React Native桥概念
android – React Native Maps错误编译debugJavaWithJavac
我是React-native的新手.我尝试使用npm在我的应用程序上添加谷歌地图i react-native-maps –save
我推荐这个https://github.com/react-community/react-native-maps/blob/master/docs/installation.md并按照所有步骤进行操作.我获得了Google Api并将其放入AndroidManifest.我的Google Play服务版本为14.5.74.我还添加了android / app / build.gradle.这里是
ext {
buildToolsversion = “27.0.3”
minSdkVersion = 16
compileSdkVersion = 27
targetSdkVersion = 26
supportLibVersion = “27.1.1”
googlePlayServicesversion = “14.5.74”
androidMapsUtilsversion = “0.5+”
}
我曾经
“dependencies”: {
“@babel/runtime”: “^7.1.5”,
“react”: “16.5.0”,
“react-native”: “0.57.0”,
“react-native-maps”: “^0.22.1” },
但是我收到了一个错误
ask :react-native-maps:compileDebugJavaWithJavac Failed
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolyline.java:8:
error: cannot find symbol
import
com.google.android.gms.maps.model.Cap;
^ symbol: class Cap location: package com.google.android.gms.maps.model
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolyline.java:12:
error: cannot find symbol import
com.google.android.gms.maps.model.RoundCap;
^ symbol: class RoundCap location: package com.google.android.gms.maps.model
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolyline.java:27:
error: cannot find symbol private Cap lineCap = new RoundCap();
^ symbol: class Cap location: class AirMappolyline D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolyline.java:73:
error: cannot find symbol public void setLineCap(Cap cap) {
^ symbol: class Cap location: class AirMappolyline
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolylineManager.java:15:
error: cannot find symbol import
com.google.android.gms.maps.model.ButtCap;
^ symbol: class ButtCap location: package com.google.android.gms.maps.model
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolylineManager.java:16:
error: cannot find symbol import
com.google.android.gms.maps.model.Cap;
^ symbol: class Cap location: package com.google.android.gms.maps.model
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolylineManager.java:17:
error: cannot find symbol import
com.google.android.gms.maps.model.RoundCap;
^ symbol: class RoundCap location: package com.google.android.gms.maps.model
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolylineManager.java:18:
error: cannot find symbol import
com.google.android.gms.maps.model.SquareCap;
^ symbol: class SquareCap location: package com.google.android.gms.maps.model
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolyline.java:27:
error: cannot find symbol private Cap lineCap = new RoundCap();
^ symbol: class RoundCap location: class AirMappolyline
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolylineManager.java:77:
error: cannot find symbol
Cap cap = null;
^ symbol: class Cap location: class AirMappolylineManager D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolylineManager.java:80:
error: cannot find symbol
cap = new ButtCap();
^ symbol: class ButtCap location: class AirMappolylineManager
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolylineManager.java:83:
error: cannot find symbol
cap = new RoundCap();
^ symbol: class RoundCap location: class AirMappolylineManager
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolylineManager.java:86:
error: cannot find symbol
cap = new SquareCap();
^ symbol: class SquareCap location: class AirMappolylineManager
D:_reactNative\MSB\LocationMap\node_modules\react-native-maps\lib\android\src\main\java\com\airbnb\android\react\maps\AirMappolylineManager.java:89:
error: cannot find symbol
cap = new RoundCap();
^ symbol: class RoundCap location: class AirMappolylineManager Note: Some input files use or override a
deprecated API. Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations. Note:
Recompile with -Xlint:unchecked for details. 14 errors
- What went wrong: Execution Failed for task ‘:react-native-maps:compileDebugJavaWithJavac’.
Compilation Failed; see the compiler error output for details.
请告诉我怎么解决这个问题?
解决方法:
react-native-maps依赖不适用于react-native版本0.57.0.请将其更新为0.57.4或最新版本.
android – 如何理解React Native中“向Javascript发送事件”中的“ReactContext”
private void sendEvent(ReactContext reactContext,String eventName,@Nullable WritableMap params) {reactContext .getJSModule(DeviceEventManagerModule.RCTdeviceeventemitter.class) .emit(eventName,params);}
解决方法
public final class MainActivity extends ReactActivity { private static ReactInstanceManager sReactInstanceManager = null; /* [...] */ @Override protected ReactInstanceManager createReactInstanceManager() { ReactInstanceManager manager = super.createReactInstanceManager(); sReactInstanceManager = manager; return manager; } public static ReactContext getContext() { if (sReactInstanceManager == null){ // This doesn't seem to happen ... throw new IllegalStateException("Instance manager not available"); } final ReactContext context = sReactInstanceManager.getCurrentReactContext(); if (context == null){ // This really shouldn't happen ... throw new IllegalStateException("React context not available"); } return context; } /* [...] */ }
createReactInstanceManager()由ReactActivity的onCreate()方法调用,所以我认为可以安全地假设sReactInstanceManager将指向正确的实例,但不要相信我的话.
android – 对Nativescript插件中的Java对象无效’this’
所以我在我的插件中添加了以下功能:
MobileServiceUser.newUser = function (userId,client) { var userObject = com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId); // causing the error client.setCurrentUser(userObject); };
UserId是一个字符串.
然而,顶线抛出了error:
JS: Error: Trying to link invalid ‘this’ to a Java object
我有一个完整的回购,显示了在Github创建此问题的最小启示.
我真的很感激任何建议如何解决这个问题.
解决方法
错误有点简单,但如果你看一下代码源,你会发现它实际上告诉你,你不能将一个变量链接到Object / Class本身作为参考:
MobileServiceUser.newUser = function (userId,client) { var userObject = com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId); };
如果要实例化类,则需要使用关键字new来表示此操作.如果在类构造函数调用之前没有新列出,程序只会看到您在userObject和类本身之间建立了直接链接.这应该可以解决您的问题:
var userObject = new com.microsoft.windowsazure.mobileservices.authentication.MobileServiceUser(userId);
干杯〜Px的
android – 有没有在react-native中实现JavaScriptModule的指南?
请帮助一些在本机中创建和注册本机到javascript模块的示例.
已经发现官方native modules android文档中的代码有一个名为createJSModules的方法,它返回一个JavaScriptModule类列表.
class AnExampleReactPackage implements ReactPackage { @Override public List<Class<? extends JavaScriptModule>> createJSModules() { return Collections.emptyList(); } ... }
com.facebook.react.bridge.JavaScriptModule的Javadoc说:
/** * Interface denoting that a class is the interface to a module with the same name in JS. Calling * functions on this interface will result in corresponding methods in JS being called. * ... */ @DoNotStrip public interface JavaScriptModule { }
我能够创建接口并将其传递给createJSModules类,但不知道如何从js代码注册适当的javascript模块.
解决方法
>扩展JavaScriptModule并将其传递给自定义反应包的createJSModules方法(例如ActionsModule.java).
>在您内部注册包反应活动getPackages方法或直接认为ReactInstanceManager
>在js代码中创建具有相同名称的js文件
>按照下面列出的说明将其注册到BatchedBridge.
>在响应上下文初始化之后,您可以通过以下方式获取并调用:
ActionsModule jsModule = context.getJSModule(ActionsModule.class);
jsModule.callAction(“greet”,“hello”);
这将异步调用您注册的js模块方法.
// AppPackage.java public class AppPackage implements ReactPackage { @Override public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { return Collections.emptyList(); } @Override public List<Class<? extends JavaScriptModule>> createJSModules() { return Arrays.<Class<? extends JavaScriptModule>>asList( ActionsModule.class ); } @Override public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { return Collections.emptyList(); } } // ActionsModule.java public interface ActionsModule extends JavaScriptModule { void callAction(String type,String value); } // MyReactActivity.java public class MyReactActivity extends ReactActivity implements ReactInstanceManager.ReactInstanceEventListener { ... @Override public void onReactContextinitialized(ReactContext context) { ActionsModule jsModule = context.getJSModule(ActionsModule.class); jsModule.callAction("greet","hello"); } ... }
// ActionsModule.js var ActionsModule = { callAction(type,value) { console.log("callAction => " + type + ":" + value); },} module.exports = ActionsModule; // index.android.js import { AppRegistry } from 'react-native'; import App from './components/App'; // js module registration var BatchedBridge = require('BatchedBridge'); var ActionsModule = require('./ActionsModule'); BatchedBridge.registerCallableModule( 'ActionsModule',ActionsModule ); //~ js module registration AppRegistry.registerComponent('MyApp',() => App);
UPD>将一个演示项目推向了github,并提供了android和ios的解决方案:https://github.com/dmba/rct-native2js
关于React Native是否可以将JavaScript编译为Android的Java?和react native 编译的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于android – React Native Maps错误编译debugJavaWithJavac、android – 如何理解React Native中“向Javascript发送事件”中的“ReactContext”、android – 对Nativescript插件中的Java对象无效’this’、android – 有没有在react-native中实现JavaScriptModule的指南?的相关信息,请在本站寻找。
本文标签: