Skip to content

sdk access

xuwhale6 edited this page May 12, 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.1"
    // view、tool bridge library
    implementation "com.immomo.mls:mln:1.0.1"
    // code generation library
    annotationProcessor "com.immomo.mls:processor:1.0.0"
    // hotreload
    debugImplementation "com.immomo.luanative:hotreload:1.0.1"
    releaseImplementation "com.immomo.luanative:hotreload_empty:1.0.1"
    // 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)  
                        .setSdcardDir(sdcardDir)  // set sdcard directory
                        .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.

1.3 Request for Access in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />   

Note:

  • When running the app, you need to manually enable the storage and camera permissions on the mobile phone.

1.4 Show LuaView

// can edit 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()) {
   //illegal url
}
// remember to call the three life cycles of instance

    @Override
    protected void onResume() {
        super.onResume();
        instance.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        instance.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        instance.onDestroy();
    }

Note:

  • Remember to call the three life cycles of instance;
  • 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, we can use hot reload for more convenient development.

1.5 Connect via wifi

// 1. Set QrCaptureAdapter in the MLSEngine initialization. After setting, you can see an additional scan code icon in the lua floating window.
MLSEngine.init(mApplication, BuildConfig.DEBUG)
         .setQrCaptureAdapter(new MLSQrCaptureAdapter() {
              @Override
              public void startQrCapture(Context context) {
              //Jump to scan page

              }
          })

// 2. In the QR code scan result, set the hot reload mode to wifi connection
if (HotReloadHelper.isIPPortString(result)) {
       HotReloadHelper.setUseWifi(result);
}

1.6 Obfuscation settings

# Annotated and annotated classes are not confused
-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>;
}

2. iOS access

2.1 Access SDK

(i) pod configuration:

pod 'MLN','1.0.0.2'

(ii) Environment configuration

// AppDelegate is set as follows
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // can be preloaded as needed
    [MLNKitEnvironment instancePreload];
    // set the default network handler
    [MLNKitEnvironment setDefaultHttpHandler:...];
    // set the default ScrollRefreshHandler
    [MLNKitEnvironment setDefaultScrollRefreshHandler:...];
    // set the default image loader
    [MLNKitEnvironment setDefaultImageLoader:...];
    // set the default navigation jump tool
    [MLNKitEnvironment setDefaultNavigatorHandler:...];
}

(iii) Create a MLN Lua page

// Create a new .lua file in the project and name it demo.lua. Type the following lua code
label = Label()
label:setGravity(Gravity.CENTER)
label:text("Hello World!")
window:addView(label)

// then load demo.lua in the startup page
NSString *entryfile = @"demo.lua";
MLNKitViewController *kvc = [[MLNKitViewController alloc] initWithEntryFilePath:entryfile];

(iv) push page

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

(v) After running, the device will display the lua file we loaded as above.

2.2 Access hot reload

(i) pod configuration

pod 'MLNDevTool' , '0.1.8'

(ii) Create a MLN Lua page that supports HotReload

// create a Lua page
MLNHotReloadViewController *vc = [[MLNHotReloadViewController alloc] initWithNavigationBarTransparent:YES]  

(iii) push page

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

(iv) Apply for camera permission

  • Right click on the info.plist file-> Open As-> Source Code, add camera permissions.
    <key>NSCameraUsageDescription</key>  
    <string>我要使用摄像机</string>
    
  • Right-click on the info.plist file-> Open As-> Property List and check if the camera permission is added successfully.

(v) After running, there will be an additional lua logo on the device screen. At this time, we can use hot reload 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