Skip to content

Commit f86c652

Browse files
authored
Merge pull request #317 from YellowLabrador/master
Added an Android Studio project
2 parents 6c5953b + 267d799 commit f86c652

Some content is hidden

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

44 files changed

+1172
-1
lines changed

RtMidi.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -4974,7 +4974,7 @@ static void androidOpenDevice(jobject deviceInfo, void* target, bool isOutput) {
49744974

49754975
auto listenerClass = env->FindClass("com/yellowlab/rtmidi/MidiDeviceOpenedListener");
49764976
if (!listenerClass) {
4977-
LOGE(LOG_TAG, "Midi listener class not found com.yellowlab.rtmidi.MidiDeviceOpenedListener. Did you forget to add it to your APK?");
4977+
LOGE("Midi listener class not found com.yellowlab.rtmidi.MidiDeviceOpenedListener. Did you forget to add it to your APK?");
49784978
return;
49794979
}
49804980

@@ -5072,6 +5072,11 @@ std::string MidiInAndroid :: getPortName(unsigned int portNumber) {
50725072
}
50735073

50745074
void MidiInAndroid :: closePort() {
5075+
// Don't try to close a port before it was open
5076+
if (!reading) {
5077+
return;
5078+
}
5079+
50755080
reading = false;
50765081
pthread_join(readThread, NULL);
50775082

android/.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

android/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Example Android Studio project
2+
3+
Simple app using RtMidi. There are symlinks to 3 files in the main RtMidi project
4+
* RtMidi.cpp
5+
* RdMidi.h
6+
* MidiDeviceOpenedListener.java
7+
8+
The main goal of this project is to demonstrate how to build RtMidi into an Android app. The
9+
app itself simply lists midi devices, opens the selected port and prints out any incoming packet
10+
11+
![Screenshot](./images/midi.png)

android/app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

android/app/build.gradle

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
android {
7+
namespace 'com.rtmidi.yellowlab.midireader'
8+
compileSdk 33
9+
10+
defaultConfig {
11+
applicationId "com.rtmidi.yellowlab.midireader"
12+
minSdk 31
13+
targetSdk 33
14+
versionCode 1
15+
versionName "1.0"
16+
17+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
vectorDrawables {
19+
useSupportLibrary true
20+
}
21+
}
22+
23+
buildTypes {
24+
release {
25+
minifyEnabled false
26+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
27+
}
28+
debug {
29+
jniDebuggable true
30+
}
31+
}
32+
compileOptions {
33+
sourceCompatibility JavaVersion.VERSION_1_8
34+
targetCompatibility JavaVersion.VERSION_1_8
35+
}
36+
kotlinOptions {
37+
jvmTarget = '1.8'
38+
}
39+
buildFeatures {
40+
compose true
41+
}
42+
composeOptions {
43+
kotlinCompilerExtensionVersion '1.4.8'
44+
}
45+
packagingOptions {
46+
resources {
47+
excludes += '/META-INF/{AL2.0,LGPL2.1}'
48+
}
49+
}
50+
externalNativeBuild {
51+
cmake {
52+
path file('src/main/cpp/CMakeLists.txt')
53+
version '3.22.1'
54+
}
55+
}
56+
buildFeatures {
57+
viewBinding true
58+
}
59+
}
60+
61+
dependencies {
62+
63+
implementation 'androidx.core:core-ktx:1.10.1'
64+
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.9.0')
65+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
66+
implementation 'androidx.activity:activity-compose:1.7.2'
67+
implementation platform('androidx.compose:compose-bom:2023.06.01')
68+
implementation 'androidx.compose.ui:ui'
69+
implementation 'androidx.compose.ui:ui-graphics'
70+
implementation 'androidx.compose.ui:ui-tooling-preview'
71+
implementation 'androidx.compose.material3:material3'
72+
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
73+
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1'
74+
testImplementation 'junit:junit:4.13.2'
75+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
76+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
77+
debugImplementation 'androidx.compose.ui:ui-tooling'
78+
debugImplementation 'androidx.compose.ui:ui-test-manifest'
79+
}

android/app/proguard-rules.pro

+21
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.rtmidi.yellowlab.midireader
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.rtmidi.yellowlab.midireader", appContext.packageName)
23+
}
24+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
<application
6+
android:allowBackup="false"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="false"
11+
android:theme="@style/Theme.MidiReader"
12+
tools:targetApi="31">
13+
<activity
14+
android:name=".MainActivity"
15+
android:exported="true"
16+
android:label="@string/app_name"
17+
android:theme="@style/Theme.MidiReader">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# For more information about using CMake with Android Studio, read the
2+
# documentation: https://d.android.com/studio/projects/add-native-code.html
3+
4+
# Sets the minimum version of CMake required to build the native library.
5+
6+
cmake_minimum_required(VERSION 3.22.1)
7+
8+
# Declares and names the project.
9+
10+
project("midireader")
11+
12+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__AMIDI__")
13+
14+
# Creates and names a library, sets it as either STATIC
15+
# or SHARED, and provides the relative paths to its source code.
16+
# You can define multiple libraries, and CMake builds them for you.
17+
# Gradle automatically packages shared libraries with your APK.
18+
19+
add_library( # Sets the name of the library.
20+
midireader
21+
22+
# Sets the library as a shared library.
23+
SHARED
24+
25+
# Provides a relative path to your source file(s).
26+
RtMidi.cpp app-jni.cpp)
27+
28+
# Searches for a specified prebuilt library and stores the path as a
29+
# variable. Because CMake includes system libraries in the search path by
30+
# default, you only need to specify the name of the public NDK library
31+
# you want to add. CMake verifies that the library exists before
32+
# completing its build.
33+
34+
find_library( # Sets the name of the path variable.
35+
log-lib
36+
37+
# Specifies the name of the NDK library that
38+
# you want CMake to locate.
39+
log)
40+
41+
# Specifies libraries CMake should link to your target library. You
42+
# can link multiple libraries, such as libraries you define in this
43+
# build script, prebuilt third-party libraries, or system libraries.
44+
45+
target_link_libraries( # Specifies the target library.
46+
midireader
47+
48+
# Links the target library to the log library
49+
# included in the NDK.
50+
${log-lib} amidi nativehelper)

android/app/src/main/cpp/RtMidi.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../RtMidi.cpp

android/app/src/main/cpp/RtMidi.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../RtMidi.h

android/app/src/main/cpp/app-jni.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <jni.h>
2+
#include <string>
3+
#include "RtMidi.h"
4+
5+
static RtMidiIn* midiLib = new RtMidiIn( RtMidi::Api::ANDROID_AMIDI );
6+
static jobject callbackObj = NULL;
7+
static jmethodID callbackMethod;
8+
static JavaVM* jvm;
9+
10+
extern "C" JNIEXPORT jobjectArray JNICALL
11+
Java_com_rtmidi_yellowlab_midireader_MidiViewModel_portNames(
12+
JNIEnv* env,
13+
jobject /* this */) {
14+
15+
auto portCount = midiLib->getPortCount();
16+
auto names = env->NewObjectArray(portCount, env->FindClass("java/lang/String"), NULL);
17+
18+
for (int i=0; i<portCount; i++) {
19+
auto name = midiLib->getPortName(i);
20+
env->SetObjectArrayElement(names, i, env->NewStringUTF(name.c_str()));
21+
}
22+
23+
return names;
24+
}
25+
26+
static void midicallback( double deltatime, std::vector< unsigned char > *message, void */*userData*/ ) {
27+
unsigned int numBytes = message->size();
28+
29+
JNIEnv* env;
30+
jvm->AttachCurrentThread(&env, NULL);
31+
32+
// Allocate the Java array and fill with received data
33+
jbyteArray ret = env->NewByteArray(numBytes);
34+
env->SetByteArrayRegion(ret, 0, numBytes, (jbyte*)message);
35+
36+
// send it to the (Java) callback
37+
env->CallVoidMethod(callbackObj, callbackMethod, deltatime, ret);
38+
}
39+
40+
extern "C"
41+
JNIEXPORT void JNICALL
42+
Java_com_rtmidi_yellowlab_midireader_MidiViewModel_openPort(
43+
JNIEnv *env,
44+
jobject /* this */,
45+
jint port,
46+
jobject listener) {
47+
48+
env->GetJavaVM(&jvm);
49+
50+
midiLib->setCallback( midicallback );
51+
midiLib->openPort(port);
52+
53+
if (callbackObj) env->DeleteGlobalRef(callbackObj);
54+
callbackObj = env->NewGlobalRef(listener);
55+
callbackMethod = env->GetMethodID(env->GetObjectClass(listener), "onMidiMessage", "(D[B)V");
56+
}
57+
58+
extern "C" JNIEXPORT void JNICALL
59+
Java_com_rtmidi_yellowlab_midireader_MidiViewModel_closePort(
60+
JNIEnv *env,
61+
jobject /* this */) {
62+
63+
midiLib->closePort();
64+
if (callbackObj) env->DeleteGlobalRef(callbackObj);
65+
callbackObj = NULL;
66+
callbackMethod = NULL;
67+
}

0 commit comments

Comments
 (0)