Skip to content
Open
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
139 changes: 139 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
**/.cxx
local.properties
# Created by .ignore support plugin (hsz.mobi)
### NetBeans template
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml

# Mac OS X Garbage
.DS_Store
Thumbs.db

.idea/

### Android Studio
/jniLibs
mobile/mobile.iml
mobile/mobile.iml
tv/tv.iml
app/app.iml
app/*.iml
*.iml
mobile/*.iml
tv/*.iml
.idea/workspace.xml
.idea/libraries
.idea/
.idea
/build
/captures
### Android template
# Built application files
*.apk
*.ap_
/build
/captures
# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/

# Gradle files
.gradle/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log


### Java template
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


### Eclipse template
*.pydevproject
.metadata
.gradle
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
.settings/
.loadpath
/build
/captures
# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath

# sbteclipse plugin
.target

# TeXlipse plugin
.texlipse

# Android Studio
*.iml

# Keep external libs
!app/libs/*.jar

# script output
check-dpi.txt*

# spring output
target

#KMP & CMP
.kotlin
*.podspec
/common/common.podspec
94 changes: 85 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject

plugins {
id 'com.android.application'
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
}

ext {
ncnnJniDir = file("src/main/jni")
ncnnTargetDirName = "ncnn-android-vulkan"
ncnnDir = file("${ncnnJniDir}/${ncnnTargetDirName}")
}

android {
namespace 'com.tencent.piperncnn'
compileSdk 33
compileSdk 36

defaultConfig {
applicationId "com.tencent.piperncnn"
archivesBaseName = "$applicationId"

minSdk 24
minSdk 21

externalNativeBuild {
cmake {
arguments "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
arguments "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
"-Dncnn_DIR_FROM_GRADLE=${project.ext.ncnnDir.absolutePath}"
}
}
}
Expand All @@ -26,13 +37,78 @@ android {
}
}

dependencies {
implementation 'com.android.support:support-v4:24.0.0'
}

packaging {
jniLibs {
useLegacyPackaging true
}
}
}

tasks.register('setupNcnn') {
group = "NCNN Setup"
description = "Downloads and extracts the latest NCNN library for Android Vulkan."
outputs.dir(project.ext.ncnnDir)
doLast {
println "==============================================="
println "Executing 'setupNcnn' task..."
def ncnnApiUrl = "https://api.github.com/repos/Tencent/ncnn/releases/latest"
def client = new OkHttpClient()
def request = new Request.Builder().url(ncnnApiUrl).build()
def downloadUrl = ""
def zipRootFolderName = ""

try {
client.newCall(request).execute().withCloseable { response ->
if (!response.isSuccessful()) {
throw new IOException("Failed to query GitHub API: ${response}")
}
def responseBody = response.body().string()
def releaseInfo = new JSONObject(responseBody)
def assets = releaseInfo.getJSONArray("assets")

for (int i = 0; i < assets.length(); i++) {
def asset = assets.getJSONObject(i)
String name = asset.getString("name")
if (name.contains("-android-vulkan.zip")) {
downloadUrl = asset.getString("browser_download_url")
zipRootFolderName = name.replace(".zip", "")
println "Found latest NCNN release asset: ${name}"
println "Download URL: ${downloadUrl}"
println "Inferred zip root folder: ${zipRootFolderName}"
break
}
}
}
} catch (Exception e) {
throw new GradleException("Failed to get NCNN download URL from GitHub API. Please check your network. Error: ${e.message}")
}

if (downloadUrl.isEmpty()) {
throw new GradleException("Could not find 'ncnn-YYYYMMDD-android-vulkan.zip' in the latest release assets.")
}
def outputZip = file("${buildDir}/downloads/ncnn.zip")
outputZip.parentFile.mkdirs()
new URL(downloadUrl).withInputStream { i -> outputZip.withOutputStream { o -> o << i } }
println "Download complete: ${outputZip}"
println "Extracting ${outputZip} to ${project.ext.ncnnDir}..."
project.copy {
from(zipTree(outputZip))
into(project.ext.ncnnDir)
eachFile { file ->
file.path = file.path.replaceFirst("^${zipRootFolderName}/", "")
}
includeEmptyDirs = false
}

println "NCNN extracted successfully to ${project.ext.ncnnDir}"
println "==============================================="
}
}

tasks.named("preBuild").configure {
dependsOn("setupNcnn")
}

dependencies {

}
15 changes: 9 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.1">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application android:label="@string/app_name">
<activity android:name="MainActivity" android:label="@string/app_name">
<application
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

</manifest>

11 changes: 10 additions & 1 deletion app/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ project(piperncnn)

cmake_minimum_required(VERSION 3.10)

set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20250916-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn)
#set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20250503-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn)
if(DEFINED ncnn_DIR_FROM_GRADLE)
message(STATUS "Using NCNN path from Gradle: ${ncnn_DIR}")
# set(ncnn_DIR ${ncnn_DIR_FROM_GRADLE}/${ANDROID_ABI}/lib/cmake/ncnn)
set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn)
else()
message(WARNING "ncnn_DIR_FROM_GRADLE not set. Falling back to default path. Please sync project with Gradle.")
set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn)
endif()

find_package(ncnn REQUIRED)

add_library(piperncnn SHARED piperncnn.cpp piper.cpp simpleg2p.cpp audioplayer.cpp)
Expand Down
17 changes: 15 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.7.3' apply false
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.squareup.okhttp3:okhttp:4.12.0"
classpath "org.json:json:20251224"
}
}

plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.kotlin.android) apply false
}
24 changes: 23 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
org.gradle.jvmargs=-Xmx4096m
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. For more details, visit
# https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
Loading