Skip to content

Commit 862f5e4

Browse files
authored
android : introduce starter project example (ggml-org#4926)
* Introduce starter project for Android Based on examples/llama.swiftui. * Add github workflow * Set NDK version * Only build arm64-v8a in CI * Sync bench code * Rename CI prop to skip-armeabi-v7a * Remove unused tests
1 parent 3a48d55 commit 862f5e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1807
-0
lines changed

.github/workflows/build.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,31 @@ jobs:
515515
- name: Build Xcode project
516516
run: xcodebuild -project examples/llama.swiftui/llama.swiftui.xcodeproj -scheme llama.swiftui -sdk iphoneos CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= -destination 'generic/platform=iOS' build
517517

518+
android-build:
519+
runs-on: ubuntu-latest
520+
521+
steps:
522+
- name: Clone
523+
uses: actions/checkout@v3
524+
525+
- name: Set up JDK
526+
uses: actions/setup-java@v3
527+
with:
528+
java-version: 17
529+
distribution: zulu
530+
531+
- name: Setup Android SDK
532+
uses: android-actions/setup-android@v3
533+
with:
534+
log-accepted-android-sdk-licenses: false
535+
536+
- name: Build
537+
run: |
538+
cd examples/llama.android
539+
540+
# Skip armeabi-v7a for now (https://github.com/llvm/llvm-project/issues/65820).
541+
./gradlew build --no-daemon -Pskip-armeabi-v7a
542+
518543
# freeBSD-latest:
519544
# runs-on: macos-12
520545
# steps:

examples/llama.android/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Gradle files
2+
.gradle/
3+
build/
4+
5+
# Local configuration file (sdk path, etc)
6+
local.properties
7+
8+
# Log/OS Files
9+
*.log
10+
11+
# Android Studio generated files and folders
12+
captures/
13+
.externalNativeBuild/
14+
.cxx/
15+
*.apk
16+
output.json
17+
18+
# IntelliJ
19+
*.iml
20+
.idea/
21+
misc.xml
22+
deploymentTargetDropDown.xml
23+
render.experimental.xml
24+
25+
# Keystore files
26+
*.jks
27+
*.keystore
28+
29+
# Google Services (e.g. APIs or Firebase)
30+
google-services.json
31+
32+
# Android Profiling
33+
*.hprof

examples/llama.android/README.md

Whitespace-only changes.

examples/llama.android/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
plugins {
2+
id("com.android.application")
3+
id("org.jetbrains.kotlin.android")
4+
}
5+
6+
android {
7+
namespace = "com.example.llama"
8+
compileSdk = 34
9+
10+
ndkVersion = "26.1.10909125"
11+
12+
defaultConfig {
13+
applicationId = "com.example.llama"
14+
minSdk = 33
15+
targetSdk = 34
16+
versionCode = 1
17+
versionName = "1.0"
18+
19+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
20+
vectorDrawables {
21+
useSupportLibrary = true
22+
}
23+
ndk {
24+
// Workaround for https://github.com/llvm/llvm-project/issues/65820
25+
// affecting armeabi-v7a. Skip armeabi-v7a when invoked with
26+
// -Pskip-armeabi-v7a (e.g., ./gradlew build -Pskip-armeabi-v7a).
27+
if (project.hasProperty("skip-armeabi-v7a")) {
28+
abiFilters += listOf("arm64-v8a", "x86_64", "x86")
29+
}
30+
}
31+
externalNativeBuild {
32+
cmake {
33+
cppFlags += listOf()
34+
arguments += listOf()
35+
}
36+
}
37+
}
38+
39+
buildTypes {
40+
release {
41+
isMinifyEnabled = false
42+
proguardFiles(
43+
getDefaultProguardFile("proguard-android-optimize.txt"),
44+
"proguard-rules.pro"
45+
)
46+
}
47+
}
48+
compileOptions {
49+
sourceCompatibility = JavaVersion.VERSION_1_8
50+
targetCompatibility = JavaVersion.VERSION_1_8
51+
}
52+
kotlinOptions {
53+
jvmTarget = "1.8"
54+
}
55+
buildFeatures {
56+
compose = true
57+
}
58+
composeOptions {
59+
kotlinCompilerExtensionVersion = "1.5.1"
60+
}
61+
packaging {
62+
resources {
63+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
64+
}
65+
}
66+
externalNativeBuild {
67+
cmake {
68+
path = file("src/main/cpp/CMakeLists.txt")
69+
version = "3.22.1"
70+
}
71+
}
72+
}
73+
74+
dependencies {
75+
76+
implementation("androidx.core:core-ktx:1.12.0")
77+
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
78+
implementation("androidx.activity:activity-compose:1.8.2")
79+
implementation(platform("androidx.compose:compose-bom:2023.08.00"))
80+
implementation("androidx.compose.ui:ui")
81+
implementation("androidx.compose.ui:ui-graphics")
82+
implementation("androidx.compose.ui:ui-tooling-preview")
83+
implementation("androidx.compose.material3:material3")
84+
testImplementation("junit:junit:4.13.2")
85+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
86+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
87+
androidTestImplementation(platform("androidx.compose:compose-bom:2023.08.00"))
88+
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
89+
debugImplementation("androidx.compose.ui:ui-tooling")
90+
debugImplementation("androidx.compose.ui:ui-test-manifest")
91+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:dataExtractionRules="@xml/data_extraction_rules"
10+
android:fullBackupContent="@xml/backup_rules"
11+
android:icon="@mipmap/ic_launcher"
12+
android:label="@string/app_name"
13+
android:roundIcon="@mipmap/ic_launcher_round"
14+
android:supportsRtl="true"
15+
android:theme="@style/Theme.LlamaAndroid"
16+
>
17+
18+
<activity
19+
android:name=".MainActivity"
20+
android:exported="true"
21+
android:theme="@style/Theme.LlamaAndroid">
22+
<intent-filter>
23+
<action android:name="android.intent.action.MAIN" />
24+
25+
<category android:name="android.intent.category.LAUNCHER" />
26+
</intent-filter>
27+
</activity>
28+
</application>
29+
30+
</manifest>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
# For more information about using CMake with Android Studio, read the
3+
# documentation: https://d.android.com/studio/projects/add-native-code.html.
4+
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.
5+
6+
# Sets the minimum CMake version required for this project.
7+
cmake_minimum_required(VERSION 3.22.1)
8+
9+
# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
10+
# Since this is the top level CMakeLists.txt, the project name is also accessible
11+
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
12+
# build script scope).
13+
project("llama-android")
14+
15+
include(FetchContent)
16+
FetchContent_Declare(
17+
llama
18+
GIT_REPOSITORY https://github.com/ggerganov/llama.cpp
19+
GIT_TAG master
20+
)
21+
22+
# Also provides "common"
23+
FetchContent_MakeAvailable(llama)
24+
25+
# Creates and names a library, sets it as either STATIC
26+
# or SHARED, and provides the relative paths to its source code.
27+
# You can define multiple libraries, and CMake builds them for you.
28+
# Gradle automatically packages shared libraries with your APK.
29+
#
30+
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
31+
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
32+
# is preferred for the same purpose.
33+
#
34+
# In order to load a library into your app from Java/Kotlin, you must call
35+
# System.loadLibrary() and pass the name of the library defined here;
36+
# for GameActivity/NativeActivity derived applications, the same library name must be
37+
# used in the AndroidManifest.xml file.
38+
add_library(${CMAKE_PROJECT_NAME} SHARED
39+
# List C/C++ source files with relative paths to this CMakeLists.txt.
40+
llama-android.cpp)
41+
42+
# Specifies libraries CMake should link to your target library. You
43+
# can link libraries from various origins, such as libraries defined in this
44+
# build script, prebuilt third-party libraries, or Android system libraries.
45+
target_link_libraries(${CMAKE_PROJECT_NAME}
46+
# List libraries link to the target library
47+
llama
48+
common
49+
android
50+
log)

0 commit comments

Comments
 (0)