web-dev-qa-db-ja.com

Android with Android Studio 2.2 and CMakeで、ABI依存の実行可能バイナリをコンパイルして使用します

新しいAndroid Studio C/C++ビルドをCMake経由で安定したGradle( http://tools.Android.com/tech-docs/external-c-ビルド )。

私のアプリでは、既にルート化されたデバイスで、ABIに依存するバイナリを使用する必要があります。これは、Android Studio。

標準ライブラリをコンパイルしようとすると

add_library(mylib SHARED mylib.c)

それは自動的にコンパイルされ、APKのlib/[ABI]フォルダー(例:/lib/armeabi/mylib.so)内にコピーされますが、実行可能バイナリを次のようにコンパイルした場合:

add_executable(mybinary mybinary.cpp)

バイナリはビルドフォルダー内で正しく生成されます。

app/build/intermediates/cmake/debug/lib/armeabi/mybinary
app/build/intermediates/cmake/debug/lib/x86_64/mybinary 
...

しかし、それらはapk内のどこにもコピーされていないようです。

このニーズに対処する正しい方法はどれですか? Gradleタスクは進むべき道ですか?

build.gradle:

apply plugin: 'com.Android.application'

Android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"
    defaultConfig {
        applicationId "com.my.app"
        minSdkVersion 10
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "Android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild{
        cmake{
            path "CMakeLists.txt"
        }
    }

    defaultConfig {
        externalNativeBuild {
            cmake {
                targets "
                arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_PLATFORM=Android-21"
                cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"
                cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
                abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a'
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.Android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.Android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile 'com.Android.support:appcompat-v7:24.1.1'
    compile 'com.Android.support:design:24.1.1'
    compile 'com.Android.support:recyclerview-v7:24.1.1'
    compile 'eu.chainfire:libsuperuser:1.0.0.201607041850'
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_VERBOSE_MAKEFILE on)

add_executable(mybinary ${CMAKE_CURRENT_SOURCE_DIR}/mybinary.cpp)
target_link_libraries( mybinary libcustom)
target_include_directories (mybinary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

mybinary.cpp

#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {

    string hello = "Hello from C++";
    cout << "Message from native code: " << hello << "\n";

    return EXIT_SUCCESS;
}

アプリがmybinaryと対話する方法:

import eu.chainfire.libsuperuser.Shell;
...
Shell.SU.run("/path/to/mybinary");
16
ADD

わかりました。かなり快適に思えるソリューションを見つけましたが、おそらくもっと適切な方法があります。

CMakeLists.txtはデフォルトでmyAppProject/app内に配置されるため、CMakeLists.txtに次の行を追加しました。

set(EXECUTABLE_OUTPUT_PATH      "${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/${Android_ABI}")

完全なapp/CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_VERBOSE_MAKEFILE on)

# set binary output folder to Android assets folder
set(EXECUTABLE_OUTPUT_PATH      "${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/${Android_ABI}")

add_subdirectory (src/main/cpp/mylib)
add_subdirectory (src/main/cpp/mybinary)

完全なapp/src/main/cpp/mybinary/CMakeLists.txt:

add_executable(mybinary ${CMAKE_CURRENT_SOURCE_DIR}/mybinary.cpp)
# mybinary, in this example, has mylib as dependency
target_link_libraries( mybinary mylib)
target_include_directories (mybinary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

完全なapp/src/main/cpp/mylib/CMakeLists.txt:

add_library( # Sets the name of the library.
             mylib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             # Associated headers in the same location as their source
             # file are automatically included.
             ${CMAKE_CURRENT_SOURCE_DIR}/mylib.cpp )

target_include_directories (mylib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

そうすることで、実行可能バイナリはすべて、名前がターゲットABIであるサブフォルダー内のアセットフォルダーに直接コンパイルされます。例:

assets/armeabi/mybinary
assets/x86_64/mybinary
... 

アプリ内で適切なバイナリを使用するには、正しいバイナリを選択する必要があります。

String abi;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Lollipop) {
    abi = Build.SUPPORTED_ABIS[0];
} else {
    //noinspection deprecation
    abi = Build.CPU_ABI;
}
String folder;
if (abi.contains("armeabi-v7a")) {
    folder = "armeabi-v7a";
} else if (abi.contains("x86_64")) {
    folder = "x86_64";
} else if (abi.contains("x86")) {
    folder = "x86";
} else if (abi.contains("armeabi")) {
    folder = "armeabi";
}
...
AssetManager assetManager = getAssets();
InputStream in = assetManager.open(folder+"/" + "mybinary");

次に、適切な実行権限を持つアセットフォルダーからバイナリをコピーします。

OutputStream out = context.openFileOutput("mybinary", MODE_PRIVATE);
long size = 0;
int nRead;
while ((nRead = in.read(buff)) != -1) {
    out.write(buff, 0, nRead);
    size += nRead;
}
out.flush();
Log.d(TAG, "Copy success: " +  " + size + " bytes");
File execFile = new File(context.getFilesDir()+"/mybinary");
execFile.setExecutable(true);

それで全部です!

更新:gradle.buildファイル:

apply plugin: 'com.Android.application'

Android {
    compileSdkVersion 25
    buildToolsVersion "25"
    defaultConfig {
        applicationId "com.myapp.example"
        minSdkVersion 10
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "Android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    defaultConfig {
        externalNativeBuild {
            cmake {
                targets "mylib", "mybinary"
                arguments "-DANDROID_TOOLCHAIN=clang"
                cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"
                cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
                abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64'
            }
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.Android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.Android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile 'com.Android.support:appcompat-v7:25.0.0'
    compile 'com.Android.support:design:25.0.0'
    compile 'com.Android.support:recyclerview-v7:25.0.0'
    compile 'com.Android.support:cardview-v7:25.0.0'
    compile 'eu.chainfire:libsuperuser:1.0.0.201607041850'
}
18
ADD
  1. Android Gradleプラグインがライブラリを想定している場所に実行可能ファイルを出力します。

    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

  2. プラグインをだまして、実行可能ファイルが共有オブジェクトであると考えます。

    add_executable(i_am_an_executable.so main.c)

  3. APKを確認します。

    $ 7z l build/outputs/apk/app-debug.apk lib/[2:08:56] 
    日付時間属性サイズ圧縮名
     ----------- -------- ----- ------------ ------------ ------------- ----------- 
     ..... 9684 4889 lib/armeabi/i_am_an_executable.so 
     ..... 6048 1710 lib/arm64-v8a/i_am_an_executable。 so 
     ..... 9688 4692 lib/armeabi-v7a/i_am_an_executable.so 
     ..... 5484 1715 lib/x86/i_am_an_executable.so 
     .... 。6160 1694 lib/x86_64/i_am_an_executable.so 
    
  4. 実行可能ファイルにアクセスして実行します。 context.getApplicationInfo().nativeLibraryDirにあります。

これの欠点は、Android:extractNativeLibsfalseに設定できないことです。アプリ内からAPKでlib/にアクセスする方法がわかりません。

3
angelsl