Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Posts “2020-03-08-link-ndk-with-gradle/index” #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 17 additions & 12 deletions content/posts/2020-03-08-link-ndk-with-gradle/index.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
---
title: "将 ndk-build 关联到 Gradle"
date: 2020-03-08
author: rosu
slug: "link-ndk-with-gradle"
slug: link-ndk-with-gradle
title: 将 ndk-build 关联到 Gradle
date: 2020-03-08
tags:
- 'Android'
- Android
hero: images/group-1.png
categories:
- "技术"
description: "最近在倒腾 Mpg123-Android 的时候,尝试了把 ndk-build 关联到 Gradle 中。不需要单独执行 `ndk-build` 指令来构建 `so` 了。在使用此种方式后,有些小发现,和诸君分享。"
- 技术
description: 最近在倒腾 Mpg123-Android 的时候,尝试了把 ndk-build 关联到 Gradle 中。不需要单独执行
`ndk-build` 指令来构建 `so` 了。在使用此种方式后,有些小发现,和诸君分享。
---

## 前言

最近在倒腾[Mpg123-Android](https://github.com/rosuH/MPG123-Android)的时候,尝试了把 ndk-build 关联到 Gradle 中。不需要单独执行 `ndk-build` 指令来构建 `so` 了。在使用此种方式后,有些小发现,和诸君分享。

## 怎么关联

在模块级别的`build.gradle`文件中指定如下:

```xml
android {
...
externalNativeBuild {
ndkBuild {
// native 编译配置文件位置
// native 编译配置文件位置
path file('src/main/jni/Android.mk')
// so 产物输出文件夹,编译后会输出到 $buildStagingDirectory/ndkBuild/.. 下面
// so 产物输出文件夹,编译后会输出到 $buildStagingDirectory/ndkBuild/.. 下面
buildStagingDirectory "src/main/libs"
}
}
Expand All @@ -38,15 +41,17 @@ android {
## 使用场景和解决方法

- 开源库:建议关联 Gradle,理由:
1. 一般情况下开发这个 Native 就是唯一的工作,所以不存在编译影响开发效率的情况
2. 不需要手动生产 So,即便是创建 release,用户拉取你的库的时候,也会自动编译 so,和手动编译放到库里没有差别。
- 一般情况下开发这个 Native 就是唯一的工作,所以不存在编译影响开发效率的情况
- 不需要手动生产 So,即便是创建 release,用户拉取你的库的时候,也会自动编译 so,和手动编译放到库里没有差别。

其他时候,主要看项目大小和核心功能。一般来说

- 项目比较大,功能丰富,不总是需要对这个 Native 开发的时候,那么不要关联是最好的。因为项目总是需要频繁重新构建,如果耗费大量时间去编译无关迭代的模块,肯定很不值得
- 核心和此 Native 相关,频繁迭代,或者是模块负责人,那么肯定关联最好了。

---
- - -

参看:

- [Link Gradle to your native library](https://developer.android.com/studio/projects/gradle-external-native-builds#configure-gradle)
- [NdkBuildOptions](http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.NdkBuildOptions.html)