Android Studio 1.3 RC1 NDK Unable to load native library

∥☆過路亽.° 提交于 2019-12-24 17:05:48

问题


Tonight I switched my ndk application to work with the new Android Studio 1.3 preview. I have 2 devices Nexus 5(5.1.1) and MotoG(4.4.4). When compiled via ndk-build the application works on both devices. Using gradle build the Nexus 5 executes correctly but on the MotoG I get an exception:

07-11 04:13:58.509  16751-16751/com.mbyan.android E/AndroidRuntime﹕   FATAL EXCEPTION: main
Process: com.mbyan.android, PID: 16751
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbyan.android/com.mbyan.android.GL2JNIActivity}: java.lang.IllegalArgumentException: Unable to load native library: /data/app-lib/com.mbyan.android-1/libgl2jni.so
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.access$800(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: Unable to load native library: /data/app-lib/com.mbyan.android-1/libgl2jni.so
        at android.app.NativeActivity.onCreate(NativeActivity.java:183)
        at com.mbyan.android.GL2JNIActivity.onCreate(GL2JNIActivity.java:40)
        at android.app.Activity.performCreate(Activity.java:5248)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:111)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)

The build.gradle looks like:

apply plugin: 'com.android.model.application'

model {

    android {
        compileSdkVersion = 21
        buildToolsVersion = "22.0.1"

        defaultConfig.with {
            applicationId = "com.mbyan.android"
            minSdkVersion.apiLevel = 14
            targetSdkVersion.apiLevel = 14
            versionCode = 1
            versionName = "1.0"
        }

        compileOptions.with {
            sourceCompatibility JavaVersion.VERSION_1_6
            targetCompatibility JavaVersion.VERSION_1_6
        }
    }

    android.ndk {
        moduleName = "gl2jni"
        stl = "stlport_static"
        ldLibs += "log"
        ldLibs += "android"
        ldLibs += "EGL"
        ldLibs += "GLESv2"

        String includeBasePath = "-I${projectDir}/src/main/jni/engine/include"
        String includeBaseGlm = "-I${projectDir}/src/main/jni/engine/glm"
        String includeBaseLua = "-I${projectDir}/src/main/jni/engine/lua"
        String includeAppGlue = "-I${projectDir}/src/main/jni/android_app_glue"

        CFlags += includeBaseGlm
        CFlags += includeBasePath
        CFlags += includeBaseLua
        CFlags += includeAppGlue
        CFlags += "-DGL_V_2"
        CFlags += "-DUSE_ANDROID"

        cppFlags += includeBaseGlm
        cppFlags += includeBasePath
        cppFlags += includeBaseLua
        cppFlags += includeAppGlue
        cppFlags += "-DGL_V_2"
        cppFlags += "-DUSE_ANDROID"

    }

    // You can modify the NDK configuration for each variant.
    components.android {
        binaries.afterEach { binary ->
            binary.mergedNdkConfig.cppFlags.add(
                    "-DVARIANT=\"" + binary.name + "\"")
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}

Studio 1.3 build.gradle looks different than <1.3 the changes are outlined in:

http://tools.android.com/tech-docs/new-build-system/gradle-experimental

The working Android.mk:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := gl2jni
LOCAL_CFLAGS    := -DGL_V_2 -DUSE_ANDROID -I$(LOCAL_PATH)/engine/include -I $(LOCAL_PATH)/engine/glm -I $(LOCAL_PATH)/engine/lua
FILE_LIST := $(wildcard $(LOCAL_PATH)/**/*.cpp)
FILE_LIST += $(wildcard $(LOCAL_PATH)/*.cpp)
FILE_LIST += $(wildcard $(LOCAL_PATH)/engine/lua/*.c)
FILE_LIST += $(wildcard $(LOCAL_PATH)/*.c)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_STATIC_LIBRARIES := android_native_app_glue
LOCAL_LDLIBS := -llog -lGLESv2 -landroid -lEGL

include $(BUILD_SHARED_LIBRARY)
$(call import-module, native_app_glue)

And Application.mk:

APP_ABI := all
APP_STL := gnustl_static
APP_PLATFORM := android-14

Also you will note the dependency outlined in LOCAL_STATIC_LIBRARIES: android_native_app_glue. I have copied android_native_app_glue.c/android_native_app_glue.h to the project because I have not found a way to link dependant libraries.

BTW I am using the NativeActivity which automatically loads the native shared library from the AndroidManifest Activity Metadata.

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mbyan.android" >
    <application
            android:label="@string/gl2jni_activity" android:hasCode="true" android:icon="@drawable/ic_launcher">
        <activity android:name=".GL2JNIActivity"
                android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                android:launchMode="singleTask"
                android:screenOrientation="landscape"
                android:configChanges="orientation|keyboardHidden">
            <meta-data android:name="android.app.lib_name"
                       android:value="gl2jni" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>
    <uses-sdk android:minSdkVersion="14"/>
</manifest>

Has anyone had a similar issue?


回答1:


I think your issue comes from the fact that the APP_PLATFORM currently selected by the ndk integration is incorrect.

I've reported this bug earlier here: https://code.google.com/p/android/issues/detail?id=177530




回答2:


Citing from the official release doc, as of July 14:

There’s no support for NDK-only modules. The only supported project types are hybrid app projects and hybrid Library Projects.

So, probably, we should simply wait a bit for NativeActivity to be fully supported.



来源:https://stackoverflow.com/questions/31355605/android-studio-1-3-rc1-ndk-unable-to-load-native-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!