解决Caused by: java.lang.IllegalArgumentException: Unable to find native libary main using classloader

记录一个最近UE4项目接入硬核的错误解决方式
错误描述如下截图
解决Caused by: java.lang.IllegalArgumentException: Unable to find native libary main using classloader_第1张图片
接入是采用aar引入的方式,由于MainActivity本身继承了游戏GameActivity,而后者引用了UE4的相关so库,导致在调用onCreate方法时报main这个库,找不到。
出错的相关源码,参考NativeActivityonCreate方法

String libname = "main"; // 之所以报main找不到都是因为这个,默认main很具有误导性
String funcname = "ANativeActivity_onCreate";
ActivityInfo ai;

mIMM = getSystemService(InputMethodManager.class);

getWindow().takeSurface(this);
getWindow().takeInputQueue(this);
getWindow().setFormat(PixelFormat.RGB_565);
getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
        | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

mNativeContentView = new NativeContentView(this);
mNativeContentView.mActivity = this;
setContentView(mNativeContentView);
mNativeContentView.requestFocus();
mNativeContentView.getViewTreeObserver().addOnGlobalLayoutListener(this);

try {
    ai = getPackageManager().getActivityInfo(
            getIntent().getComponent(), PackageManager.GET_META_DATA);
    if (ai.metaData != null) {
        String ln = ai.metaData.getString(META_DATA_LIB_NAME); // 此处会读取android.app.lib_name值,没有就是main
        if (ln != null) libname = ln;
        ln = ai.metaData.getString(META_DATA_FUNC_NAME);
        if (ln != null) funcname = ln;
    }
} catch (PackageManager.NameNotFoundException e) {
    throw new RuntimeException("Error getting activity info", e);
}

BaseDexClassLoader classLoader = (BaseDexClassLoader) getClassLoader();
String path = classLoader.findLibrary(libname);

if (path == null) {
    throw new IllegalArgumentException("Unable to find native library " + libname +
                                       " using classloader: " + classLoader.toString());
    // 最终折在了这里,因为根本就没有main这个玩意
}

因此最终的错误看上去像这样

Caused by: java.lang.IllegalArgumentException: Unable to find native libary main using classloader: dalvik.system.PathClassLoader

解决方法很简单,只需要在这个引入aar库的MainActivity中增加一个meta标记即可,目的就是指定要引入的库!
像这样

        <activity
            android:name="com.xy.gswz.MainActivity"
            android:launchMode="${mainLaunchMode}"
            android:screenOrientation="${splashScape}"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="adjustResize">
            
            <meta-data android:name="android.app.lib_name" android:value="UE4"/>

        activity>

这样操作后,需要使用NativeActivity时就能正确识别了。

你可能感兴趣的:(java,android,unreal,engine,4)