Mars服务器和客户端demo运行教程

wiki地址:https://github.com/Tencent/mars/wiki

第一步:首先我们需要把项目下载下来,项目分成了几个部门,其中包括android,ios客户端和Service端的代码。

第二步:运行service端代码:

    cmd 进入到sample/Service目录下,然后执行 python start_server.py 。

    第一次运行需要下载很多配置文件,需要的时间比较久,执行完成后,会弹出两个界面。证明服务端已经可以运行了

第三步:将android项目导入as中。在wrapper模块中,我们需要将本地的id配置完成:

    wrapper模块/service/MarsServiceStub.class文件下

@Override
public String[] onNewDns(String host) {
    return new String[]{
            "xx.xx.98.xxx"
    };
}

    默认该方法返回的是null 。

第四步:就可以运行了。

    以上为demo的运行教程,下面是项目集成的教程。

-------------------------

独立运行模块:

第一步:在项目gradle中添加依赖

classpath "com.github.dcendents:android-maven-gradle-plugin:1.4.1"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.6'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"

和全局的配置参数

ext {
    GROUP = 'com.tencent.mars.sample'
    VERSION_NAME_PREFIX = '1.2.1'
    VERSION_NAME_SUFFIX = '' // '-SNAPSHOT'
    WRAPPER_VERSION_NAME_PREFIX = '1.1.8'
    WRAPPER_VERSION_NAME_SUFFIX = '' // '-SNAPSHOT'
    javaVersion = JavaVersion.VERSION_1_7

    VERSION_NAME = "${VERSION_NAME_PREFIX}${VERSION_NAME_SUFFIX}"
}

第二步:在app.gralde中添加mars依赖包

implementation 'com.tencent.mars:mars-core:1.2.1'
implementation 'com.tencent.mars:mars-xlog:1.0.6'

第三步:将官方提供的demo中wrapper模块添加到自己的项目中,然后在gradle中添加引入

compile project(path: ':wrapper')

第四步:在Application中进行初始化工作

/**
 * Created by malei on 2018/4/13.
 */

public class MyApplication extends Application {

    private static final String TAG = "Mars.SampleApplication";

    private static Context context;

    public static AppLogic.AccountInfo accountInfo = new AppLogic.AccountInfo(
            new Random(System.currentTimeMillis() / 1000).nextInt(), "anonymous");

    public static volatile boolean hasSetUserName = false;

    private static class SampleMarsServiceProfile extends DebugMarsServiceProfile {

        @Override
        public String longLinkHost() {
            return "marsopen.cn";
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;

        System.loadLibrary("stlport_shared");
        System.loadLibrary("marsxlog");
        openXlog();

        MarsServiceNative.setProfileFactory(new MarsServiceProfileFactory() {
            @Override
            public MarsServiceProfile createMarsServiceProfile() {
                return new SampleMarsServiceProfile();
            }
        });

        // NOTE: MarsServiceProxy is for client/caller
        // Initialize MarsServiceProxy for local client, can be moved to other place
        MarsServiceProxy.init(this, getMainLooper(), null);
        MarsServiceProxy.inst.accountInfo = accountInfo;

        // Auto bind all activity event
//        ActivityEvent.bind(getApplicationContext());

        Log.i(TAG, "application started");
    }

    @Override
    public void onTerminate() {
        Log.i(TAG, "application terminated");

        super.onTerminate();

        Log.appenderClose();

    }

    public static  void openXlog() {

        int pid = android.os.Process.myPid();
        String processName = null;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : am.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                processName = appProcess.processName;
                break;
            }
        }

        if (processName == null) {
            return;
        }

        final String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
        final String logPath = SDCARD + "/marssample/log";

        String logFileName = processName.indexOf(":") == -1 ? "MarsSample" : ("MarsSample_" + processName.substring(processName.indexOf(":") + 1));

        if (BuildConfig.DEBUG) {
            Xlog.appenderOpen(Xlog.LEVEL_VERBOSE, Xlog.AppednerModeAsync, "", logPath, logFileName, "");
            Xlog.setConsoleLogOpen(true);
        } else {
            Xlog.appenderOpen(Xlog.LEVEL_INFO, Xlog.AppednerModeAsync, "", logPath, logFileName, "");
            Xlog.setConsoleLogOpen(false);
        }
        Log.setLogImp(new Xlog());
    }

    public static Context getContext() {
        return context;
    }
}

完毕!

你可能感兴趣的:(android)