A Jitpack project template which make publishing a general android library project to Jitpack easily
- Project level
build.gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
- App level
build.gradle
dependencies {
implementation 'com.github.yikuo123:Template-Jitpack:4d16276647'
}
android {
defaultConfig {
ndk {
moduleName "Demo"
}
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
}
package com.ikecin.sdk.jni;
public class Demo {
static {
System.loadLibrary("Demo");
}
public static native String text();
}
以下的命令都是在 library
目录执行
javac src/main/java/com/ikecin/sdk/jni/Demo.java -d ./build
javah -d src/main/cpp -classpath ./build com.ikecin.sdk.jni.Demo
library/main/cpp
下会自动生成头文件
创建同名c文件,实现头文件中的方法
在library
中创建文件 src/main/cpp/CMakeLists.txt
配置cmake,内容如下(详见Demo)
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
Demo
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
com_ikecin_sdk_jni_Demo.c)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
target_link_libraries( # Specifies the target library.
Demo
# Links the target library to the log library
# included in the NDK.
${log-lib})
注意,要使用Linux的分割符,否则在JitPack构建会出错(Windows上使用Linux分隔符仍能正常编译)
-keepclasseswithmembernames,includedescriptorclasses class * { native <methods>; }
http://wiki.jikexueyuan.com/project/jni-ndk-developer-guide/