From 66bfa44052b72abafb161fae9c0029f59a6fa02e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E7=81=BF=E8=BE=89?= <122565648@qq.com> Date: Sun, 15 Mar 2020 13:38:47 +0800 Subject: [PATCH] Add library module --- app/build.gradle | 16 ++---- .../{ => demo}/ExampleInstrumentedTest.java | 2 +- app/src/main/AndroidManifest.xml | 2 +- app/src/main/cpp/native-lib.cpp | 10 ---- .../invoker/{ => demo}/MainActivity.java | 19 +++---- app/src/main/res/layout/activity_main.xml | 2 +- .../ffmpeg/invoker/demo/ExampleUnitTest.java | 17 +++++++ library/.gitignore | 1 + library/build.gradle | 50 +++++++++++++++++++ library/consumer-rules.pro | 0 library/proguard-rules.pro | 21 ++++++++ .../invoker/ExampleInstrumentedTest.java | 27 ++++++++++ library/src/main/AndroidManifest.xml | 2 + {app => library}/src/main/cpp/CMakeLists.txt | 6 +-- library/src/main/cpp/ffmpeg-invoker.cpp | 9 ++++ .../xch168/ffmpeg/invoker/FFmpegInvoker.java | 24 +++++++++ .../ffmpeg/invoker/ExampleUnitTest.java | 0 settings.gradle | 1 + 18 files changed, 167 insertions(+), 42 deletions(-) rename app/src/androidTest/java/com/github/xch168/ffmpeg/invoker/{ => demo}/ExampleInstrumentedTest.java (94%) delete mode 100644 app/src/main/cpp/native-lib.cpp rename app/src/main/java/com/github/xch168/ffmpeg/invoker/{ => demo}/MainActivity.java (52%) create mode 100644 app/src/test/java/com/github/xch168/ffmpeg/invoker/demo/ExampleUnitTest.java create mode 100644 library/.gitignore create mode 100644 library/build.gradle create mode 100644 library/consumer-rules.pro create mode 100644 library/proguard-rules.pro create mode 100644 library/src/androidTest/java/com/github/xch168/ffmpeg/invoker/ExampleInstrumentedTest.java create mode 100644 library/src/main/AndroidManifest.xml rename {app => library}/src/main/cpp/CMakeLists.txt (92%) create mode 100644 library/src/main/cpp/ffmpeg-invoker.cpp create mode 100644 library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java rename {app => library}/src/test/java/com/github/xch168/ffmpeg/invoker/ExampleUnitTest.java (100%) diff --git a/app/build.gradle b/app/build.gradle index 3d08a85..9b22ab7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,12 +12,6 @@ android { versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - - externalNativeBuild { - cmake { - cppFlags "" - } - } } buildTypes { @@ -26,13 +20,6 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } - - externalNativeBuild { - cmake { - path "src/main/cpp/CMakeLists.txt" - version "3.10.2" - } - } } dependencies { @@ -43,4 +30,7 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + + implementation project(path: ':library') + } diff --git a/app/src/androidTest/java/com/github/xch168/ffmpeg/invoker/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/github/xch168/ffmpeg/invoker/demo/ExampleInstrumentedTest.java similarity index 94% rename from app/src/androidTest/java/com/github/xch168/ffmpeg/invoker/ExampleInstrumentedTest.java rename to app/src/androidTest/java/com/github/xch168/ffmpeg/invoker/demo/ExampleInstrumentedTest.java index a1d6f58..bc78594 100644 --- a/app/src/androidTest/java/com/github/xch168/ffmpeg/invoker/ExampleInstrumentedTest.java +++ b/app/src/androidTest/java/com/github/xch168/ffmpeg/invoker/demo/ExampleInstrumentedTest.java @@ -1,4 +1,4 @@ -package com.github.xch168.ffmpeg.invoker; +package com.github.xch168.ffmpeg.invoker.demo; import android.content.Context; diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 74d0f55..d6252b8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="com.github.xch168.ffmpeg.invoker.demo"> -#include - -extern "C" JNIEXPORT jstring JNICALL -Java_com_github_xch168_ffmpeg_invoker_MainActivity_stringFromJNI( - JNIEnv* env, - jobject /* this */) { - std::string hello = "Hello from C++"; - return env->NewStringUTF(hello.c_str()); -} diff --git a/app/src/main/java/com/github/xch168/ffmpeg/invoker/MainActivity.java b/app/src/main/java/com/github/xch168/ffmpeg/invoker/demo/MainActivity.java similarity index 52% rename from app/src/main/java/com/github/xch168/ffmpeg/invoker/MainActivity.java rename to app/src/main/java/com/github/xch168/ffmpeg/invoker/demo/MainActivity.java index f753daf..d9ca552 100644 --- a/app/src/main/java/com/github/xch168/ffmpeg/invoker/MainActivity.java +++ b/app/src/main/java/com/github/xch168/ffmpeg/invoker/demo/MainActivity.java @@ -1,16 +1,14 @@ -package com.github.xch168.ffmpeg.invoker; - -import androidx.appcompat.app.AppCompatActivity; +package com.github.xch168.ffmpeg.invoker.demo; import android.os.Bundle; import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; + +import com.github.xch168.ffmpeg.invoker.FFmpegInvoker; + public class MainActivity extends AppCompatActivity { - // Used to load the 'native-lib' library on application startup. - static { - System.loadLibrary("native-lib"); - } @Override protected void onCreate(Bundle savedInstanceState) { @@ -19,12 +17,7 @@ public class MainActivity extends AppCompatActivity { // Example of a call to a native method TextView tv = findViewById(R.id.sample_text); - tv.setText(stringFromJNI()); + tv.setText(FFmpegInvoker.stringFromJNI()); } - /** - * A native method that is implemented by the 'native-lib' native library, - * which is packaged with this application. - */ - public native String stringFromJNI(); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a4e8d40..5bdae71 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainActivity"> + tools:context="com.github.xch168.ffmpeg.invoker.demo.MainActivity"> Testing documentation + */ +public class ExampleUnitTest { + @Test + public void addition_isCorrect() { + assertEquals(4, 2 + 2); + } +} \ No newline at end of file diff --git a/library/.gitignore b/library/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/library/.gitignore @@ -0,0 +1 @@ +/build diff --git a/library/build.gradle b/library/build.gradle new file mode 100644 index 0000000..01686b8 --- /dev/null +++ b/library/build.gradle @@ -0,0 +1,50 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 29 + buildToolsVersion "29.0.3" + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles 'consumer-rules.pro' + + externalNativeBuild { + cmake { + cppFlags "" + abiFilters "armeabi-v7a", "arm64-v8a", "x86" + } + } + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + + externalNativeBuild { + cmake { + path "src/main/cpp/CMakeLists.txt" + version "3.10.2" + } + } + + sourceSets.main { + jniLibs.srcDir 'libs' + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + + implementation 'androidx.appcompat:appcompat:1.1.0' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' +} diff --git a/library/consumer-rules.pro b/library/consumer-rules.pro new file mode 100644 index 0000000..e69de29 diff --git a/library/proguard-rules.pro b/library/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/library/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/library/src/androidTest/java/com/github/xch168/ffmpeg/invoker/ExampleInstrumentedTest.java b/library/src/androidTest/java/com/github/xch168/ffmpeg/invoker/ExampleInstrumentedTest.java new file mode 100644 index 0000000..6526c6b --- /dev/null +++ b/library/src/androidTest/java/com/github/xch168/ffmpeg/invoker/ExampleInstrumentedTest.java @@ -0,0 +1,27 @@ +package com.github.xch168.ffmpeg.invoker; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + + assertEquals("com.github.xch168.ffmpeg.invoker.test", appContext.getPackageName()); + } +} diff --git a/library/src/main/AndroidManifest.xml b/library/src/main/AndroidManifest.xml new file mode 100644 index 0000000..2f77415 --- /dev/null +++ b/library/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + diff --git a/app/src/main/cpp/CMakeLists.txt b/library/src/main/cpp/CMakeLists.txt similarity index 92% rename from app/src/main/cpp/CMakeLists.txt rename to library/src/main/cpp/CMakeLists.txt index 4a597f0..20269e6 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/library/src/main/cpp/CMakeLists.txt @@ -11,13 +11,13 @@ cmake_minimum_required(VERSION 3.4.1) # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. - native-lib + ffmpeg-invoker # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). - native-lib.cpp ) + ffmpeg-invoker.cpp) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by @@ -37,7 +37,7 @@ find_library( # Sets the name of the path variable. # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. - native-lib + ffmpeg-invoker # Links the target library to the log library # included in the NDK. diff --git a/library/src/main/cpp/ffmpeg-invoker.cpp b/library/src/main/cpp/ffmpeg-invoker.cpp new file mode 100644 index 0000000..5b6041f --- /dev/null +++ b/library/src/main/cpp/ffmpeg-invoker.cpp @@ -0,0 +1,9 @@ +#include +#include + +extern "C" +JNIEXPORT jstring JNICALL +Java_com_github_xch168_ffmpeg_invoker_FFmpegInvoker_stringFromJNI(JNIEnv *env, jclass clazz) { + std::string hello = "Hello from C++"; + return env->NewStringUTF(hello.c_str()); +} \ No newline at end of file diff --git a/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java b/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java new file mode 100644 index 0000000..8156718 --- /dev/null +++ b/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java @@ -0,0 +1,24 @@ +package com.github.xch168.ffmpeg.invoker; + +/** + * Created by XuCanHui on 2020/3/14. + */ +public class FFmpegInvoker { + + static { + System.loadLibrary("ffmpeg-invoker"); + } + + public static native String stringFromJNI(); + + public static void exec(Callback callback) + { + + } + + public interface Callback { + void onSuccess(); + void onFailure(); + void onProgress(); + } +} diff --git a/app/src/test/java/com/github/xch168/ffmpeg/invoker/ExampleUnitTest.java b/library/src/test/java/com/github/xch168/ffmpeg/invoker/ExampleUnitTest.java similarity index 100% rename from app/src/test/java/com/github/xch168/ffmpeg/invoker/ExampleUnitTest.java rename to library/src/test/java/com/github/xch168/ffmpeg/invoker/ExampleUnitTest.java diff --git a/settings.gradle b/settings.gradle index 38bfb0d..da325eb 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,3 @@ rootProject.name='FFmpeg-Invoker' include ':app' +include ':library'