Skip to content

Commit

Permalink
Java API:Android版の設計を色々修正 (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi authored Sep 18, 2023
1 parent 6a66275 commit 15db875
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 37 deletions.
10 changes: 8 additions & 2 deletions crates/voicevox_core_java_api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,31 @@ Java プロジェクトを動かすには、
- `lib/src/main/resources/dll/[target]/libvoicevox_core_java_api.so` を作成する(`libvoicevox_core_java_api.so`はプラットフォームによって異なります、詳細は後述)。

必要があります。
また、ハードウェアアクセラレーションを有効にする時は`DEVICE`環境変数を`cuda`または`directml`にし、Android 版をビルドする時は`OS`環境変数を`android`にしてください。

```console
❯ cargo build
❯ LD_LIBRARY_PATH=$(realpath ../../target/debug) ./gradlew build
❯ LD_LIBRARY_PATH=$(realpath ../../target/debug) ./gradlew test
# または
❯ cp ../../target/debug/libvoicevox_core_java_api.so lib/src/main/resources/dll/[target]/libvoicevox_core_java_api.so
❯ ./gradlew build
❯ ./gradlew test
❯ DEVICE=cuda ./gradlew test
❯ OS=android ./gradlew test
```

## ビルド(リリース)

`cargo build --release` で Rust 側を、`./gradlew build` で Java 側をビルドできます。
パッケージ化する時は lib/src/main/resources/dll 内に dll をコピーしてください。
`DEVICE`、`OS`環境変数は開発時と同様です。

```console
❯ cargo build --release
❯ cp ../../target/release/libvoicevox_core_java_api.so lib/src/main/resources/dll/[target]/libvoicevox_core_java_api.so
❯ ./gradlew build
❯ DEVICE=cuda ./gradlew build
❯ OS=android ./gradlew build
```

## テスト
Expand Down
56 changes: 56 additions & 0 deletions crates/voicevox_core_java_api/lib/build-android.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
plugins {
id 'com.android.library' version '8.1.1'
id 'org.jetbrains.kotlin.android' version '1.9.10'
}

version = gradle.ext.version

repositories {
google()
mavenCentral()
}

dependencies {
// Use JUnit Jupiter for testing.
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2'

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation group: 'com.google.code.gson', name: 'gson', version: gradle.ext.gsonVersion

// https://mvnrepository.com/artifact/jakarta.validation/jakarta.validation-api
implementation group: 'jakarta.validation', name: 'jakarta.validation-api', version: gradle.ext.jakartaValidationVersion

// https://mvnrepository.com/artifact/jakarta.annotation/jakarta.annotation-api
implementation group: 'jakarta.annotation', name: 'jakarta.annotation-api', version: gradle.ext.jakartaAnnotationVersion

implementation group: 'com.microsoft.onnxruntime', name: 'onnxruntime-android', version: gradle.ext.onnxruntimeVersion
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}

android {
compileSdkVersion 26

defaultConfig {
minSdkVersion 26
targetSdkVersion 26
}
namespace "jp.hiroshiba.voicevoxcore"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
main {
jniLibs.srcDirs = ["./src/main/resources/jniLibs"]
}
}
}
26 changes: 14 additions & 12 deletions crates/voicevox_core_java_api/lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ plugins {
id "com.diffplug.spotless" version "6.20.0"
}

version = '0.0.0'
def boolean isGpu = ['cuda', 'directml'].contains(gradle.ext.targetDevice)

version = gradle.ext.version

repositories {
// Use Maven Central for resolving dependencies.
Expand All @@ -24,28 +26,28 @@ dependencies {

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:31.1-jre'

// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
implementation group: 'com.google.code.gson', name: 'gson', version: gradle.ext.gsonVersion

// https://mvnrepository.com/artifact/jakarta.validation/jakarta.validation-api
implementation group: 'jakarta.validation', name: 'jakarta.validation-api', version: '3.0.2'
implementation group: 'jakarta.validation', name: 'jakarta.validation-api', version: gradle.ext.jakartaValidationVersion

implementation group: 'com.microsoft.onnxruntime', name: 'onnxruntime', version: '1.14.0'
// https://mvnrepository.com/artifact/jakarta.annotation/jakarta.annotation-api
implementation group: 'jakarta.annotation', name: 'jakarta.annotation-api', version: gradle.ext.jakartaAnnotationVersion

if (isGpu) {
implementation group: 'com.microsoft.onnxruntime', name: 'onnxruntime_gpu', version: gradle.ext.onnxruntimeVersion
} else {
implementation group: 'com.microsoft.onnxruntime', name: 'onnxruntime', version: gradle.ext.onnxruntimeVersion
}
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
languageVersion = JavaLanguageVersion.of(8)
}
}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** AccentPhrase (アクセント句ごとの情報)。 */
public class AccentPhrase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/** AudioQuery(音声合成用のクエリ)。 */
public class AudioQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;

/** モーラ(子音+母音)ごとの情報。 */
public class Mora {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package jp.hiroshiba.voicevoxcore;

import java.lang.ref.Cleaner;

/** テキスト解析器としてのOpen JTalk。 */
public class OpenJtalk extends Dll {
private long handle;
private static final Cleaner cleaner = Cleaner.create();

/**
* Open JTalkの辞書ディレクトリ。
Expand All @@ -14,8 +10,11 @@ public class OpenJtalk extends Dll {
*/
public OpenJtalk(String openJtalkDictDir) {
rsNewWithInitialize(openJtalkDictDir);
}

cleaner.register(this, () -> rsDrop());
protected void finalize() throws Throwable {
rsDrop();
super.finalize();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package jp.hiroshiba.voicevoxcore;

import com.google.gson.Gson;
import java.lang.ref.Cleaner;
import jakarta.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nonnull;

/**
* 音声シンセサイザ。
Expand All @@ -14,11 +13,14 @@
*/
public class Synthesizer extends Dll {
private long handle;
private static final Cleaner cleaner = Cleaner.create();

private Synthesizer(OpenJtalk openJtalk, Builder builder) {
rsNewWithInitialize(openJtalk, builder);
cleaner.register(this, () -> rsDrop());
}

protected void finalize() throws Throwable {
rsDrop();
super.finalize();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.internal.LinkedTreeMap;
import jakarta.annotation.Nonnull;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import java.lang.ref.Cleaner;
import java.util.HashMap;
import javax.annotation.Nonnull;

/** ユーザー辞書。 */
public class UserDict extends Dll {
private long handle;
private static final Cleaner cleaner = Cleaner.create();

/** ユーザー辞書を作成する。 */
public UserDict() {
rsNew();
}

cleaner.register(this, () -> rsDrop());
protected void finalize() throws Throwable {
rsDrop();
super.finalize();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import com.google.gson.Gson;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.lang.ref.Cleaner;
import javax.annotation.Nonnull;
import jakarta.annotation.Nonnull;

/** 音声モデル。 */
public class VoiceModel extends Dll {
private long handle;
private static final Cleaner cleaner = Cleaner.create();

/** ID。 */
@Nonnull public final String id;
Expand All @@ -27,8 +25,11 @@ public VoiceModel(String modelPath) {
throw new RuntimeException("Failed to parse metasJson");
}
metas = rawMetas;
}

cleaner.register(this, () -> rsDrop());
protected void finalize() throws Throwable {
rsDrop();
super.finalize();
}

private native void rsFromPath(String modelPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
このディレクトリに Android での JNI 用に読み込まれる DLL を配置します。
ディレクトリ名は以下のうちのいずれかになります。

- `arm64-v8a`
- `x86_64`
Empty file.
Empty file.
37 changes: 37 additions & 0 deletions crates/voicevox_core_java_api/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}

resolutionStrategy {
eachPlugin {
if(requested.id.namespace == "com.android") {
useModule("com.android.tools.build:gradle:${requested.version}")
}
}
}
}
plugins {
// Apply the foojay-resolver plugin to allow automatic download of JDKs
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.4.0'
}

rootProject.name = 'jp.hiroshiba.voicevoxcore'
def String targetOs = System.getenv('OS')?.toLowerCase() ?: "desktop"
def boolean isAndroid = targetOs == 'android'
include('lib')
if (isAndroid) {
project(':lib').buildFileName = 'build-android.gradle'
} else {
project(':lib').buildFileName = 'build.gradle'
}

def String cargoToml = file('../../Cargo.toml').text
def String cargoTomlVersion = (cargoToml =~ /(?m)^version = "(\S+)"$/)[0][1]

gradle.ext {
version = cargoTomlVersion

targetOs = targetOs
targetDevice = System.getenv('DEVICE') ?: 'cpu'

gsonVersion = '2.10.1'
jakartaValidationVersion = '3.0.2'
jakartaAnnotationVersion = '2.1.1'
onnxruntimeVersion = '1.14.0'
}

0 comments on commit 15db875

Please sign in to comment.