-
-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: implement native convention plugins
- Allow to use environment variables / Gradle properties to determine CMake and NDK version and build ABI - Use legacy packing for JNI libs
- Loading branch information
1 parent
50e3f2b
commit 47b6910
Showing
9 changed files
with
159 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import org.gradle.api.Project | ||
import java.io.File | ||
import java.util.Properties | ||
|
||
object ApkRelease { | ||
private const val KEYSTORE_PROPERTIES = "keystore.properties" | ||
|
||
private val props by lazy { | ||
File(KEYSTORE_PROPERTIES).takeIf { it.exists() } | ||
?.let { Properties().apply { load(it.inputStream()) } } | ||
?: Properties() | ||
} | ||
|
||
val Project.storeFile | ||
get() = props["storeFile"] as? String | ||
|
||
val Project.storePassword | ||
get() = props["storePassword"] as? String | ||
|
||
val Project.keyAlias | ||
get() = props["keyAlias"] as? String | ||
|
||
val Project.keyPassword | ||
get() = props["keyPassword"] as? String | ||
|
||
val Project.buildApkRelease | ||
get() = storeFile?.let { File(it).exists() } ?: false | ||
} |
17 changes: 17 additions & 0 deletions
17
build-logic/convention/src/main/kotlin/NativeAppConventionPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.configure | ||
|
||
class NativeAppConventionPlugin : NativeBaseConventionPlugin() { | ||
override fun apply(target: Project) { | ||
super.apply(target) | ||
|
||
target.extensions.configure<BaseAppModuleExtension> { | ||
packaging { | ||
jniLibs { | ||
useLegacyPackaging = true | ||
} | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
build-logic/convention/src/main/kotlin/NativeBaseConventionPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Versions.cmakeVersion | ||
import Versions.ndkVersion | ||
import com.android.build.api.dsl.CommonExtension | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.tasks.Delete | ||
import org.gradle.kotlin.dsl.configure | ||
import org.gradle.kotlin.dsl.task | ||
|
||
open class NativeBaseConventionPlugin : Plugin<Project> { | ||
override fun apply(target: Project) { | ||
target.pluginManager.apply("com.android.application") | ||
target.extensions.configure<CommonExtension<*, *, *, *, *>>("android") { | ||
ndkVersion = target.ndkVersion | ||
// Use prebuilt JNI library if the "app/prebuilt" exists | ||
// | ||
// Steps to generate the prebuilt directory: | ||
// $ ./gradlew app:assembleRelease | ||
// $ cp --recursive app/build/intermediates/stripped_native_libs/universalRelease/out/lib app/prebuilt | ||
if (target.file("prebuilt").exists()) { | ||
sourceSets.getByName("main").jniLibs.srcDirs(setOf("prebuilt")) | ||
} else { | ||
externalNativeBuild { | ||
cmake { | ||
version = target.cmakeVersion | ||
path("src/main/jni/CMakeLists.txt") | ||
} | ||
} | ||
} | ||
|
||
if (ApkRelease.run { target.buildApkRelease }) { | ||
// in this case, the version code of arm64-v8a will be used for the single production, | ||
// unless `buildABI` is specified | ||
defaultConfig { | ||
ndk { | ||
abiFilters.add("armeabi-v7a") | ||
abiFilters.add("arm64-v8a") | ||
abiFilters.add("x86") | ||
abiFilters.add("x86_64") | ||
} | ||
} | ||
} else { | ||
splits { | ||
abi { | ||
isEnable = true | ||
reset() | ||
include(target.buildABI) | ||
isUniversalApk = false | ||
} | ||
} | ||
} | ||
} | ||
registerCleanCxxTask(target) | ||
} | ||
|
||
private fun registerCleanCxxTask(project: Project) { | ||
project.task<Delete>("cleanCxxIntermediates") { | ||
delete(project.file(".cxx")) | ||
}.also { | ||
project.cleanTask.dependsOn(it) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import org.gradle.api.Project | ||
|
||
object Versions { | ||
private const val DEFAULT_CMAKE = "3.22.1" | ||
private const val DEFAULT_NDK = "25.2.9519653" | ||
|
||
val Project.cmakeVersion | ||
get() = envOrProp("CMAKE_VERSION", "cmakeVersion") { DEFAULT_CMAKE } | ||
|
||
val Project.ndkVersion | ||
get() = envOrProp("NDK_VERSION", "ndkVersion") { DEFAULT_NDK } | ||
} |