在项目中引入C++ .so动态库代码,封装jni接口后打包成aar包,如何将C++代码所需的配置文件以及相关依赖文件打包到aar包中?
在Android Studio的右上角,点击"Gradle",在我们Module的名字下找到"Build"文件夹,然后点击里面的"assembel"选项,等待系统编译。
编译完毕之后,我们就可以在Module下看到"build"文件夹下:
里面的"outputs"文件夹下的"aar"就是我们生成的aar库:
生成aar的同时,我们也生成了对应的so库,路径在:
解决方案: 将aar打包发布到本地的Maven仓库,并在主项目中以依赖的形式引入aar。具体操作如下:
1.在开发的模块中的build.gradle中加入上传本地Maven仓库的配置:
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = "com.example.library"
pom.artifactId = "mylibrary"
pom.version = "1.0.0"
repository(url: "file://localhost/" + "Users/xxx/Library/Android/sdk" + "/extras/android/m2repository/");
}
}
}
注意:如果有自己的私服的话,可以将repository中的url改为私服地址,并加上用户名和密码的验证。
2.在命令行中进行编译:gradle uploadArchives
3.在主项目的build.gradle中配置仓库地址和引入aar:
repositories {
flatDir {
dirs 'libs'
}
maven {
url "https://jitpack.io"
}
maven {
url "http://xxx.xxx.xxx.xxx:xxxx/repository/android.lianmeng"
}
}
dependencies {
implementation fileTree (include: ['*.jar', '*.aar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'android.sdk.news:newsclient-release:1.0.0'
}
其中,android.sdk.news是上传到Maven仓库时设置的pom.groupId,newsclient-release是设置的pom.artifactId,1.0.0是设置的pom.version,需要根据上传时的设置进行修改。
4.同步一下gradle,即可在主项目中使用aar中的代码及其依赖和配置文件。