配置环境
全局build.gradle
配置全局的gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
}
}
subprojects {
project.version = iceVersion
project.group = "com.zeroc"
apply plugin: 'idea'
apply from: "$rootProject.projectDir/gradle/props.gradle"
repositories {
if (distJarDir != null) {
flatDir {
name "local"
dirs distJarDir
}
}
if (!useLocalOnly) {
maven {
url "https://${iceMavenRepo}/nexus/content/repositories/thirdparty"
}
maven {
url "https://${iceMavenRepo}/nexus/content/repositories/releases"
}
}
}
}
//
// Used to build the gradle wrapper to automatically download and install
// the version of gradle needed to build Ice.
//
task wrapper(type: Wrapper) {
gradleVersion = "2.6"
}
grade.properites
增加一些属性值
// ********************************************************************** // // Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved. // // ********************************************************************** iceVersion = 3.6.3 // The Android versions used for the Ice build. ice_buildToolsVersion = 21.1.1 ice_compileSdkVersion = 17 ice_minSdkVersion = 17 ice_targetSdkVersion = 17 // // Set iceHome to location of Ice installation if Ice was installed in a // non-standard location. // iceHome = // // Set the location of a local Ice Builder for Gradle source directory // iceBuilderHome = // // Set the version of the Ice Builder to use for building // iceBuilderVersion = 1.4.0 // // Gradle build properties // org.gradle.daemon = true
拷贝props.gradle
拷贝props.gradle到gradle目录中
配置项目build.gradle
apply plugin: 'com.android.application'
apply from: "$rootProject.projectDir/../gradle/ice.gradle"
project.slice.output = project.file("${project.buildDir}/generated/source/ice")
slice {
java {
set1 {
include = ["${projectDir}/src/main/slice"]
srcDir = 'src/main'
}
}
}
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
defaultConfig {
applicationId "com.wangsu.iceserver"
minSdkVersion ice_minSdkVersion.toInteger()
targetSdkVersion ice_targetSdkVersion.toInteger()
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
// It would be ideal if the slice plugin did this, but it doesn't work as expected.
sourceSets {
main {
java {
srcDirs = ['src/main/java', project.slice.output]
}
}
}
buildToolsVersion '25.0.0'
}
task copyBksTask(type: Copy) {
// from "${rootProject.projectDir}/../../certs/client.bks"
from "${rootProject.projectDir}/../../certs/server.bks"
into "src/main/res/raw"
}
preBuild.dependsOn(copyBksTask)
clean {
// delete("src/main/res/raw/client.bks")
delete("src/main/res/raw/server.bks")
// delete rootProject.buildDir
}
dependencies {
// compile fileTree(dir: 'libs', include: ['*.jar'])
// androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
// exclude group: 'com.android.support', module: 'support-annotations'
// })
// compile 'com.android.support:appcompat-v7:25.3.0'
// compile 'com.android.support.constraint:constraint-layout:1.0.2'
// testCompile 'junit:junit:4.12'
compile localDependency('ice')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
修改AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wangsu.iceserver">
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".HelloWorld">
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
</activity>
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Ice开发
修改Activity
让继承AppCompatActivity,继承到其他Activity
编写Ice文件
在src/main中新建slice目录,在slice目录下编写ice文件
可参考ice-demos中的hello.ice
编译
gradle中有配置Ice Builder,所以编译时会生成对应的Java接口文件
实现
继承_HelloDisp,并重载接口,实现内容
创建服务端
new Thread(new Runnable() {
public void run() {
Communicator ic = null;
try {
InitializationData initData = new InitializationData();
/*
initData.dispatcher = new Dispatcher()
{
@Override
public void
dispatch(Runnable runnable, Connection connection)
{
Log.d(TAG, "dispatch: dddddddd");
//_uiHandler.post(runnable);
}
};
*/
initData.properties = Util.createProperties();
initData.properties.setProperty("Ice.Warn.Connections", "1");
initData.properties.setProperty("Ice.Trace.Network", "3");
initData.properties.setProperty("IceSSL.Trace.Security", "3");
initData.properties.setProperty("IceSSL.KeystoreType", "BKS");
initData.properties.setProperty("IceSSL.TruststoreType", "BKS");
initData.properties.setProperty("IceSSL.Password", "password");
initData.properties.setProperty("Ice.InitPlugins", "0");
initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL.PluginFactory");
initData.properties.setProperty("Ice.Admin.InstanceName", "server");
initData.properties.setProperty("IceMX.Metrics.Debug.GroupBy", "id");
initData.properties.setProperty("IceMX.Metrics.ByParent.GroupBy", "parent");
initData.properties.setProperty("Ice.MessageSizeMax", "12288");
initData.properties.setProperty("Ice.ThreadPool.Server.Size", "10");
initData.properties.setProperty("Ice.Override.Timeout", "180000");
initData.properties.setProperty("Ice.Override.ConnectTimeout", "180000");
// SDK versions < 21 only support TLSv1 with SSLEngine.
if (Build.VERSION.SDK_INT < 21) {
initData.properties.setProperty("IceSSL.Protocols", "tls1_0");
}
ic = Util.initialize(initData);
Plugin plugin = (Plugin) ic.getPluginManager().getPlugin("IceSSL");
//
// Be sure to pass the same input stream to the SSL plug-in for
// both the keystore and the truststore. This makes startup a
// little faster since the plugin will not initialize
// two keystores.
//
InputStream certs = getResources().openRawResource(R.raw.server);
plugin.setKeystoreStream(certs);
plugin.setTruststoreStream(certs);
ic.getPluginManager().initializePlugins();
Log.i(TAG, "run: ice");
// ic = Util.initialize();
// 创建提供服务的站点,缺省协议为TCP/IP,端口为10000
ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("helloAdapter", "default -p 10000");
// 创建具体提供服务的实例
HelloI object = new HelloI();
// 将服务实例添加到提供服务的站点
adapter.add(object, ic.stringToIdentity("hello"));
adapter.activate();
ic.waitForShutdown();
Log.d(TAG, "run: ice end");
} catch (LocalException e) {
Log.e(TAG, "ice error 1: " + e.getMessage());
}
if (ic != null) {
try {
Log.d(TAG, "ICE Clean up");
ic.destroy();
} catch (Exception e) {
Log.e(TAG, "ice error 2: " + e.getMessage());
}
}
}
}).start();
