Android JNI stick career .a -> .so (gradle mode) (android studio)

Serial

Seeing Yilian Youmeng again, the story of jni.a is still going on. Originally planned to go with the mk method , the name has been thought out, the road is three thousand, I only take two scoops. Burn, the story begins.

gradle-experimental

The pig's foot of the story. A plugin, dedicated to JNI services. E Wen’s good classmates have roughly guessed that this is something in Gugou’s laboratory. Things are good things, that is, the word "experiment", hehehehe. Pure gradle way, replace mk, build integration, dream is good. But, Google doesn't seem to care much about it. Not much gossip, but if you want to practice this skill, you need to pay attention to a few points.

Note 1. Project gradle dependencies

For gradle-experimental, I am very happy. Integration, pure gradle, I have to appreciate. Love at first sight, then decline, three and exhausted. Hands up and down, a change of configuration, let me embark on the road of crossing the catastrophe. The configuration is as follows:

buildscript {
    ...
    dependencies {
          /**
         * 从前 classpath 'com.android.tools.build:gradle:2.2.3'
         * 由于插件的更换,蝴蝶来到了太平洋,一系列随之而来
         */
        classpath 'com.android.tools.build:gradle-experimental:0.9.3'
    }
}

Note 2. Current app module configuration

One's own decision, standing up is masturbation. The app module, as a place to cross the catastrophe, is about to start exciting preparations. You see:
app application plugin tweaks,

//从前, apply plugin: 'com.android.application' 
apply plugin: 'com.android.model.application'

Due to the support of the model plug-in, Du Jie is limited to the scope model, and the aura is different and stable. Knowledge and knowledge:

model{ 
    ...
    android{
    }
}

To cross the catastrophe, you have to look like you have to cross the catastrophe. The previous configuration naturally has to be slightly enhanced. Look, some chestnuts:

model{ 
    ...
    android{
      ...
      defaultConfig {
            ···
            //从前车马慢,minSdkVersion 19
            minSdkVersion.apiLevel 9
            targetSdkVersion.apiLevel 19
        }
    }
}

Note 3. Core configuration

On the way to cross the catastrophe, you need to rely on the .a static library to achieve so. First define the repositories for the library, which are used to specify the basic information of the library, including the path of the library file, the path of the header file, and the way of linking, etc. See the following code for details:

model {
    ...
    repositories {
        libs(PrebuiltLibraries) {
            libHello {
                // 头文件地址
                headers.srcDir "src/main/jni/sources/"
                // 静态链接库的引用,
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("src/main/jni/sources/${targetPlatform.getName()}/libHello.a")
                }
            }
        }
    }
}

Crossing the robbery, the key to success - NDK configuration, and then gradle, the crossing robbery begins. The NDK configuration is already familiar, but there are a few differences here. Spit out the details:

model{
  ...
  android{
     ...
     ndk {
            moduleName "JniDemo"
            //书信很远, cppFlags "-std=c++11"
            cppFlags.add("-std=c++11") //c++版本
            stl "gnustl_static" //支持 stl 标准库
            // 配置可多个, abiFilters "armeabi", "armeabi-v7a", "x86"
            // abiFilters.addAll(['armeabi', 'armeabi-v7a', 'x86'])
        }
  }
}

epilogue

At this point, he has gone farther and farther on the road of crossing the catastrophe. For mk, gradle-experimental, I originally wanted to make a comparison. Think about it later, each has its own merits, and they are all based on NDK build. As for the choice, let's not mention it for the time being, everyone has their own preferences. Looking forward to the next encounter, the next wonderful.
A github ticket is attached to take you to experience the vastness of the world

Guess you like

Origin blog.csdn.net/youyi300200/article/details/73477663