Skip to content

sdk access

zhangxin-it edited this page Jan 6, 2020 · 11 revisions

1. Android access

1.1 Configure Gradle

Open build.gradle and add the following code.

android {  
    defaultConfig {
        ndk {
            abiFilters "armeabi-v7a"
        }
    }
}  
dependencies {  
    implementation 'androidx.appcompat:appcompat:1.1.0+'  
    implementation "androidx.recyclerview:recyclerview:1.1.0+"  
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    // core library
    implementation "com.immomo.mlncore:core:1.0.0"
    // view、tool bridge library
    implementation "com.immomo.mls:mln:1.0.0"
    // code generation library
    annotationProcessor "com.immomo.mls:processor:1.0.0"
    // hotreload
    debugImplementation "com.immomo.luanative:hotreload:1.0.0"
    releaseImplementation "com.immomo.luanative:hotreload_empty:1.0.0"
    // use with hot reload
    debugImplementation "com.google.protobuf:protobuf-lite:3.0.1"
}

1.2 Initialize Lua Engine

Create a new global class to inherit Application and initialize Lua Engine in onCreate as follows:

MLSEngine.init(application, BuildConfig.DEBUG)
                .setLVConfig(new LVConfigBuilder(application)
                        .setRootDir(rootDir)    //set lua root directory
                        .setImageDir(imageDir)  //set lua picture root directory
                        .setCacheDir(cacheDir)  //set lua cache directory
                        .setGlobalResourceDir(gResDir)    //set the resource file directory
                        .build())
                .setImageProvider(new GlideImageProvider())//lua loading image tool, if it is not implemented, the image cannot be displayed
                .build(true);

Note:

  • The directory addresses in LVConfig need to be configured correctly.
  • ImageProvider needs to define its own image loading class.(see the implementation in MLN-Android)
  • Remember to register the Application in AndroidManifest.xml.

3. Show LuaView in Activity, Fragment or any view

FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
instance = new MLSInstance(this, true, true);
instance.setContainer(frameLayout);
InitData initData = new InitData("file://android_asset/demo.lua"); //MLSBundleUtils.parseFromBundle(bundle);MLSBundleUtils.createBundle(url)
instance.setData(initData);
if (!instance.isValid()) {
   //非法url
}
// instance三个生命周期记得调用:
instance.onResume();
instance.onPause();
instance.onDestroy();

Note:

  • Remember to call instance for three life cycles;
  • The lua path is passed in the InitData initialization, and the project can run to display the corresponding lua interface.
  • There will be an additional lua logo on the screen, at this time we can use hot reload for more convenient development.

4. If you need to support hot reload and be able to connect via wifi, add the following code.

// 1. 在MLSEngine初始化中设置QrCaptureAdapter,设置后可看到lua悬浮窗多出一个扫码图标
MLSEngine.init(mApplication, BuildConfig.DEBUG)
         .setQrCaptureAdapter(new MLSQrCaptureAdapter() {
              @Override
              public void startQrCapture(Context context) {
              //跳转扫码页面

              }
          })

// 2. 在二维码扫描结果中,设置热重载方式为wifi连接
if (HotReloadHelper.isIPPortString(result)) {
       HotReloadHelper.setUseWifi(result);
}

5. Obfuscation settings

# 注解和被注解类不混淆
-keepattributes *Annotation*
-keepattributes Exceptions
-keep class com.immomo.mls.annotation.* { *; }

-keep @com.immomo.mls.annotation.LuaClass class * {
    @com.immomo.mls.annotation.LuaBridge <methods>;
}
-keep @com.immomo.mls.wrapper.ConstantClass class * {
    @com.immomo.mls.wrapper.Constant <fields>;
}
-keep,allowobfuscation @interface org.luaj.vm2.utils.LuaApiUsed
-keep @com.immomo.mls.annotation.CreatedByApt class * { *; }
-keep @org.luaj.vm2.utils.LuaApiUsed class *
-keep @org.luaj.vm2.utils.LuaApiUsed class * {
    native <methods>;
    @org.luaj.vm2.utils.LuaApiUsed <methods>;
    @org.luaj.vm2.utils.LuaApiUsed <fields>;
}

iOS access

Access SDK

  1. pod configuration:
pod 'MLN','1.0.0.2'
  1. Environment processing configuration
    // AppDelegate中设置如下
  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 可根据需要开启预加载 [MLNKitEnvironment instancePreload]; // 设置默认的网络Handler [MLNKitEnvironment setDefaultHttpHandler:...]; // 设置默认的ScrollRefreshHandler [MLNKitEnvironment setDefaultScrollRefreshHandler:...]; // 设置默认的图片加载器 [MLNKitEnvironment setDefaultImageLoader:...]; // 设置默认的导航跳转工具 [MLNKitEnvironment setDefaultNavigatorHandler:...]; }
3. Create a MLN Lua page  

//在工程里新建一个lua页面,命名为demo.lua,然后在启动页中加载demo.lua NSString *entryfile = @"demo.lua"; MLNKitViewController *kvc = [[MLNKitViewController alloc] initWithEntryFilePath:entryfile];

4. push page  

[self presentViewController:kvc animated:YES completion:nil];

5. After running, the device will display the lua file we loaded as above.  

#### Access hot reload  
1. pod configuration:  

pod 'MLNDevTool' , '0.1.1'

2. Create a MLN Lua page that supports HotReload  

// 创建一个Lua页面 MLNHotReloadViewController *vc = [[MLNHotReloadViewController alloc] initWithNavigationBarTransparent:YES]

3. push page  

[self presentViewController:vc animated:YES completion:nil];

4. Apply for camera permission  
(i) Right click on the info.plist file-> Open As-> Source Code, add camera permissions.  
    ```  
    <key>NSCameraUsageDescription</key>  
    <string>我要使用摄像机</string>
    ```    
(ii) Right-click on the info.plist file-> Open As-> Property List and check if the camera permission is added successfully.  
<img src = "https://s.momocdn.com/w/u/others/custom/momo/camera.png" width = "700"/>   
5. After running, there will be an additional lua logo on the device screen. At this time, we can use [hot reload](https://github.com/momotech/MLN/wiki/%E7%83%AD%E9%87%8D%E8%BD%BD) for more convenient development.  

Note:  
* If the phone has a black screen, declare UIWindow in the AppDelegate.h file.`@property (strong, nonatomic) UIWindow *window;`  
* If you can't present the page, please check if the navigation controller is set.
Clone this wiki locally