Android Studio - application module library + NDK

Having spent hours and hours searching for a solution to my problem and browsing through various forums (including this one), I finally decided to ask my question, hoping that he did not receive a complete answer elsewhere.

I am trying to build a rather complex architecture:

  • I have C sources that I compile in the section shares static libraries (.a)
  • I use them through JNI in the module library.
  • I want to use this library in an application project.

At first, I successfully completed the following tests - I have already managed to create a library of modules without NDK and compile it using the application. - I also managed to use static libraries and JNI directly in the application, but

I do not perform the next step: - The combination of JNI inside the module and the application that calls the module classes does not work.

I think the problem is with enabling aar, because I cannot find exploded-aar in the build directory of my application, while aar is in the build / output directory of the library. In addition, all previous tests (including the use of JNI were successful).

I do not use an experimental model because it is experimental and there are known limitations with static libraries.

The structure of my project:

- App - src - main - java - activity - bar - src - main - java - class - jni - include - *.h - libs - abis... - libmod1.a - libmod2.a Android.mk Application.mk bar.c bar.h 

The build.gradle application looks like this:

 apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { applicationId "com.test.foo" minSdkVersion 10 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { debuggable false jniDebuggable false minifyEnabled false } unsigned { debuggable false jniDebuggable false minifyEnabled false } debug { debuggable true jniDebuggable true minifyEnabled false } } productFlavors { x86 { ndk { abiFilter "x86" } } mips { ndk { abiFilter "mips" } } armv7 { ndk { abiFilter "armeabi-v7a" } } arm { ndk { abiFilter "armeabi" } } fat } project.ext.versionCodes = ['armeabi':1, 'armeabi-v7a':2, 'arm64-v8a':3, 'mips':5, 'mips64':6, 'x86':8, 'x86_64':9] android.applicationVariants.all { variant -> variant.outputs.each { output -> output.versionCodeOverride = project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + defaultConfig.versionCode } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar','*.aar']) compile project( ":bar" ) } 

The build.gradle module is as follows:

 apply plugin: 'com.android.library' android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { minSdkVersion 10 targetSdkVersion 23 versionCode 1 versionName "1.0" ndk { moduleName "module" } } buildTypes { release { debuggable false jniDebuggable false minifyEnabled false } unsigned { debuggable false jniDebuggable false minifyEnabled false } debug { debuggable true jniDebuggable true minifyEnabled false } } productFlavors { x86 { ndk { abiFilter "x86" } } mips { ndk { abiFilter "mips" } } armv7 { ndk { abiFilter "armeabi-v7a" } } arm { ndk { abiFilter "armeabi" } } fat } sourceSets.main { jniLibs.srcDir 'src/main/libs' jni.srcDirs = [] } task ndkBuild(type: Exec) { commandLine android.ndkDirectory.getAbsolutePath()+'/ndk-build', '-C', file('src/main').absolutePath } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn ndkBuild } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) } 

My Android.mk inside the module jni directory: LOCAL_PATH: = $ (call my-dir)

 #### Mod1 include $(CLEAR_VARS) LOCAL_MODULE := mod1 LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libmod1.a include $(PREBUILT_STATIC_LIBRARY) #### Mod2 include $(CLEAR_VARS) LOCAL_MODULE := pxnav LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libmod2.a LOCAL_STATIC_LIBRARIES := pxfd include $(PREBUILT_STATIC_LIBRARY) ##### Parser include $(CLEAR_VARS) LOCAL_MODULE := module LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_LDLIBS += -landroid -llog LOCAL_SRC_FILES := bar.c LOCAL_STATIC_LIBRARIES := mod1 mod2 include $(BUILD_SHARED_LIBRARY) 
+5
source share
1 answer

I found an easy workaround for a similar task: I just installed jniLibs.srcDir for the app module. Now I am not dependent on aar explosion to get .so files. BTW, when you decide to switch to an experimental plugin (to use your own debugging, C ++ syntax highlighting and cross-references and other subtleties), you are invited to check the definition of LOCAL_SRC_FILES in the ndk {} DSL .

0
source

Source: https://habr.com/ru/post/1232780/


All Articles