注册

环信SDK初始化问题,java.lang.ClassNotFoundException: org.jivesoftware.smackx.ServiceDiscoveryManager

   我在Android 项目中继承环信SDK,但SDK初始化的时候时失败了。是在开发过程中突然报错的,尝试更新最新so库和Jar包也没效果。现在项目用的版本是:easemobchat_2.2.3。运行日志如下(附件01/02):


01.png




02.png


代码如下:


03.png




04.png

public abstract class HXSDKHelper {
private static final String TAG = "HXSDKHelper";
/**
* application context
*/
protected Context appContext = null;

/**
* HuanXin mode helper, which will manage the user data and user preferences
*/
protected HXSDKModel hxModel = null;

/**
* MyConnectionListener
*/
protected EMConnectionListener connectionListener = null;

/**
* HuanXin ID in cache
*/
protected String hxId = null;

/**
* password in cache
*/
protected String password = null;

/**
* init flag: test if the sdk has been inited before, we don't need to init again
*/
private boolean sdkInited = false;

/**
* the global HXSDKHelper instance
*/
private static HXSDKHelper me = null;

/**
* the notifier
*/
protected HXNotifier notifier = null;

public HXSDKHelper(){
me = this;
}

/**
* this function will initialize the HuanXin SDK
*
* @return boolean true if caller can continue to call HuanXin related APIs after calling onInit, otherwise false.
*
* 环信初始化SDK帮助函数
* 返回true如果正确初始化,否则false,如果返回为false,请在后续的调用中不要调用任何和环信相关的代码
*
* for example:
* 例子:
*
* public class DemoHXSDKHelper extends HXSDKHelper
*
* HXHelper = new DemoHXSDKHelper();
* if(HXHelper.onInit(context)){
* // do HuanXin related work
* }
*/
public synchronized boolean onInit(Context context){
if(sdkInited){
return true;
}

appContext = context;

// create HX SDK model
hxModel = createModel();

// create a defalut HX SDK model in case subclass did not provide the model
if(hxModel == null){
hxModel = new DefaultHXSDKModel(appContext);
}

int pid = android.os.Process.myPid();
String processAppName = getAppName(pid);
String str=hxModel.getAppProcessName();

EMLog.d(TAG, "process app name : " + processAppName);

// 如果app启用了远程的service,此application:onCreate会被调用2次
// 为了防止环信SDK被初始化2次,加此判断会保证SDK被初始化1次
// 默认的app会在以包名为默认的process name下运行,如果查到的process name不是app的process name就立即返回
if (processAppName == null || !processAppName.equalsIgnoreCase(processAppName)) {
EMLog.e(TAG, "enter the service process!");
// 则此application::onCreate 是被service 调用的,直接返回
return false;
}
String appkey = HelpDeskPreferenceUtils.getInstance(context).getSettingCustomerAppkey();
EMChat.getInstance().setAppkey(appkey);
// 初始化环信SDK,一定要先调用init()
EMChat.getInstance().init(context);

if(hxModel.isDebugMode()){
// set debug mode in development process
EMChat.getInstance().setDebugMode(true);
}

Log.d(TAG, "initialize EMChat SDK");

initHXOptions();
initListener();
sdkInited = true;
return true;
}

/**
* get global instance
* @return
*/
public static HXSDKHelper getInstance(){
return me;
}

public HXSDKModel getModel(){
return hxModel;
}

public String getHXId(){
SharedPreferences sharedPreferences = appContext.getSharedPreferences("shared", Context.MODE_PRIVATE);
String stname = sharedPreferences.getString("name", "zp");
return stname;
}

public String getPassword(){
SharedPreferences sharedPreferences = appContext.getSharedPreferences("shared", Context.MODE_PRIVATE);
String stpwd = sharedPreferences.getString("pwd", "123456");
return stpwd;
}

public void setHXId(String hxId){
if(hxModel.saveHXId(hxId)){
this.hxId = hxId;
}
}

public void setPassword(String password){
if(hxModel.savePassword(password)){
this.password = password;
}
}

/**
* the subclass must override this class to provide its own model or directly use {@link DefaultHXSDKModel}
* @return
*/
abstract protected HXSDKModel createModel();

/**
* please make sure you have to get EMChatOptions by following method and set related options
* EMChatOptions options = EMChatManager.getInstance().getChatOptions();
*/
protected void initHXOptions(){
Log.d(TAG, "init HuanXin Options");

// 获取到EMChatOptions对象
EMChatOptions options = EMChatManager.getInstance().getChatOptions();
// 默认添加好友时,是不需要验证的,改成需要验证
options.setAcceptInvitationAlways(hxModel.getAcceptInvitationAlways());
// 默认环信是不维护好友关系列表的,如果app依赖环信的好友关系,把这个属性设置为true
options.setUseRoster(hxModel.getUseHXRoster());
// // 设置收到消息是否有新消息通知(声音和震动提示),默认为true
// options.setNotifyBySoundAndVibrate(hxModel.getSettingMsgNotification());
// // 设置收到消息是否有声音提示,默认为true
// options.setNoticeBySound(hxModel.getSettingMsgSound());
// // 设置收到消息是否震动 默认为true
// options.setNoticedByVibrate(hxModel.getSettingMsgVibrate());
// // 设置语音消息播放是否设置为扬声器播放 默认为true
// options.setUseSpeaker(hxModel.getSettingMsgSpeaker());
// 设置是否需要已读回执
options.setRequireAck(hxModel.getRequireReadAck());
// 设置是否需要已送达回执
options.setRequireDeliveryAck(hxModel.getRequireDeliveryAck());
// // 设置notification消息点击时,跳转的intent为自定义的intent
// options.setOnNotificationClickListener(getNotificationClickListener());
notifier = createNotifier();
notifier.init(appContext);
notifier.setNotificationInfoProvider(getNotificationListener());
}

/**
* subclass can override this api to return the customer notifier
* @return
*/
protected HXNotifier createNotifier(){
return new HXNotifier();
}

public HXNotifier getNotifier(){
return notifier;
}

/**
* logout HuanXin SDK
*/
public void logout(final EMCallBack callback){
setPassword(null);
setHXId(null);
EMChatManager.getInstance().logout(new EMCallBack(){

@Override
public void onSuccess() {
if(callback != null){
callback.onSuccess();
}
}

@Override
public void onError(int code, String message) {}

@Override
public void onProgress(int progress, String status) {
if(callback != null){
callback.onProgress(progress, status);
}
}
});
}

/**
* 检查是否已经登录过
* @return
*/
public boolean isLogined(){
if(hxModel.getHXId() != null && hxModel.getPwd() != null){
return true;
}

return false;
}

/**
* get the message notify listener
* @return
*/
protected OnMessageNotifyListener getMessageNotifyListener(){
return null;
}

protected HXNotifier.HXNotificationInfoProvider getNotificationListener(){
return null;
}


/**
* init HuanXin listeners
*/
protected void initListener(){
Log.d(TAG, "init listener");

// create the global connection listener
connectionListener = new EMConnectionListener() {
@Override
public void onDisconnected(int error) {
if (error == EMError.USER_REMOVED) {
onCurrentAccountRemoved();
}else if (error == EMError.CONNECTION_CONFLICT) {
onConnectionConflict();
}else{
onConnectionDisconnected(error);
}
}

@Override
public void onConnected() {
onConnectionConnected();
}
};
EMChatManager.getInstance().addConnectionListener(connectionListener);
}

/**
* the developer can override this function to handle connection conflict error
*/
protected void onConnectionConflict(){}


/**
* the developer can override this function to handle user is removed error
*/
protected void onCurrentAccountRemoved(){}


/**
* handle the connection connected
*/
protected void onConnectionConnected(){}

/**
* handle the connection disconnect
* @param error see {@link EMError}
*/
protected void onConnectionDisconnected(int error){}

/**
* check the application process name if process name is not qualified, then we think it is a service process and we will not init SDK
* @param pID
* @return
*/
private String getAppName(int pID) {
String processName = null;
ActivityManager am = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = appContext.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
try {
if (info.pid == pID) {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
// Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
// info.processName +" Label: "+c.toString());
// processName = c.toString();
processName = info.processName;
return processName;
}
} catch (Exception e) {
// Log.d("Process", "Error>> :"+ e.toString());
}
}
return processName;
}

}

  非常期盼各位大神的热心解答,谢谢!
已邀请:

可笑的人人 - 90后,Android开发者

经过土逼几天的百度+Google,发现原来不是环信SDK的问题。
原来程序就因为API数超过了64K,用的MultiDex库,发现我的application中没有重写attachBaseContext方法。导致找不到类库的方法;
在程序application添加代码如下就完美解决:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
参考链接如下:http://www.itdadao.com/articles/c15a298843p0.html

beyond - imgeek运营

稍等

要回复问题请先登录注册