2017-01-03 38 views
5

Tôi mới sử dụng Android, vì vậy hãy tha thứ cho tôi nếu điều này có vẻ hơi nhỏ. Tôi có hai thư viện tĩnh được dựng sẵn, feta (../../feta/build/libfeta.a) và mish (../../mish/build/libmish.a) và tôi có thư viện JNI được chia sẻ. Sử dụng thư viện JNI hoạt động hoàn toàn tốt, nhưng tôi đang cố gắng truy cập cả hai fetamish thông qua thư viện JNI. Hai thư viện này liên tục được thay đổi và cập nhật cùng với dự án Android để sao chép chúng mỗi khi chúng được xây dựng không thực sự là một tùy chọn (nếu điều đó thậm chí sửa vấn đề liên kết), và tôi không muốn sao chép tệp nguồn vào dự án Android.Liên kết thư viện tĩnh với thư viện được chia sẻ của JNI trong Android

Tôi đã thử tìm kiếm, nhưng hầu hết các câu trả lời đều sử dụng phiên bản cũ của hệ thống và muốn tôi sửa đổi Android.mk mà tôi không có. Tôi đang sử dụng phiên bản Android Studio mới nhất, phiên bản này sử dụng plugin Gradle.

Tôi đã cố gắng sử dụng tất cả cấu hình từ hơn một tá hướng dẫn và câu trả lời Stackoverflow trong các thiết lập khác nhau, nhưng không có may mắn.

Nếu bạn trả lời, vui lòng cung cấp đầy đủ và làm việc build.gradle vì vậy tôi không gặp phải các vấn đề tương tự mà tôi đã nhận được từ các câu trả lời khác (cảm ơn!).

Tôi đã đặt câu hỏi này sau khi chỉ theo dõi this hướng dẫn, vì vậy tất cả các tệp sẽ phản ánh điều đó.

Đây là lỗi build tôi nhận được:

Error:A problem occurred configuring project ':app'. 
> The following model rules could not be applied due to unbound inputs and/or subjects: 
    android.sources { ... } @ app/build.gradle line 58, column 5 
     subject: 
     - android.sources Object [*] 
    repositories { ... } @ app/build.gradle line 39, column 5 
     subject: 
     - repositories Object [*] 
    [*] - indicates that a model item could not be found for the path or type. 

Dưới đây là build.gradle tập tin của tôi bên trong app mô-đun:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId "com.neonorb.mish_android" 
     minSdkVersion 15 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
     externalNativeBuild { 
      cmake { 
       cppFlags "-std=c++14 -Wno-implicit-exception-spec-mismatch" 
      } 
     } 
     ndk { 
      // ${targetPlatform.getName()} 
      // ${buildType.getName()} 
      stl "c++_static" 
      abiFilters "x86_64" 
     } 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    externalNativeBuild { 
     cmake { 
      path "CMakeLists.txt" 
     } 
    } 
} 

model { 
    repositories { 
     libs(PrebuiltLibraries) { 
      feta { 
       headers.srcDir "../../feta/include/" 
       binaries.withType(StaticLibraryBinary) { 
        staticLibraryFile = file("../../feta/build/libfeta.a") 
       } 
      } 
     } 
     libs(PrebuiltLibraries) { 
      mish { 
       headers.srcDir "../../mish/include/" 
       binaries.withType(StaticLibraryBinary) { 
        staticLibraryFile = file("../../mish/build/libmish.a") 
       } 
      } 
     } 
    } 

    android.sources { 
     main { 
      jni { 
       dependencies { 
        library "feta" linkage "static" 
        library "mish" linkage "static" 
       } 
      } 
     } 
    } 
} 

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' 
    }) 
    compile 'com.android.support:appcompat-v7:25.1.0' 
    compile 'com.google.android.gms:play-services-ads:10.0.1' 
    compile 'com.android.support:design:25.1.0' 
    testCompile 'junit:junit:4.12' 
} 

Và đây là thư mục gốc (mish-android) thư mục một:

// Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.2.3' 

     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 

allprojects { 
    repositories { 
     jcenter() 
    } 
} 

task clean(type: Delete) { 
    delete rootProject.buildDir 
} 

Và đây là sốcủa tôi:

# Sets the minimum version of CMake required to build the native 
# library. You should either keep the default value or only pass a 
# value of 3.4.0 or lower. 

cmake_minimum_required(VERSION 3.4.1) 

# Creates and names a library, sets it as either STATIC 
# or SHARED, and provides the relative paths to its source code. 
# You can define multiple libraries, and CMake builds it for you. 
# Gradle automatically packages shared libraries with your APK. 

add_library(# Sets the name of the library. 
      mish-android 

      # 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. 
      src/main/cpp/mish.cpp) 

# Searches for a specified prebuilt library and stores the path as a 
# variable. Because system libraries are included in the search path by 
# default, you only need to specify the name of the public NDK library 
# you want to add. CMake verifies that the library exists before 
# completing its build. 

find_library(# Sets the name of the path variable. 
       log-lib 

       # Specifies the name of the NDK library that 
       # you want CMake to locate. 
       log) 

# Specifies libraries CMake should link to your target library. You 
# can link multiple libraries, such as libraries you define in the 
# build script, prebuilt third-party libraries, or system libraries. 

target_link_libraries(# Specifies the target library. 
         mish-android 

         # Links the target library to the log library 
         # included in the NDK. 
         ${log-lib}) 

Đây là cấu trúc thư mục của tôi nếu nó giúp ích.

enter image description here

+0

Cũng có một cái nhìn vào ví dụ hello-libs chính thức: https://github.com/googlesamples/android -ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs –

Trả lời

0

Khi nó quay ra, tôi cần nâng cấp plugin Gradle của mình. Tôi đã có thể xóa CMakeLists.txt. Tôi cũng cần nâng cấp phiên bản trình bao bọc Gradle của mình lên 3.2 để hỗ trợ plugin thử nghiệm mới.

Dưới đây là gốc rễ của tôi build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     //classpath 'com.android.tools.build:gradle:2.2.3' 
     classpath 'com.android.tools.build:gradle-experimental:0.9.0-beta1' 

     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 

allprojects { 
    repositories { 
     jcenter() 
    } 
} 

task clean(type: Delete) { 
    delete rootProject.buildDir 
} 

Dưới đây là tôi appbuild.gradle:

apply plugin: 'com.android.model.application' 

model { 
    android { 
     compileSdkVersion 25 
     buildToolsVersion "25.0.2" 
     defaultConfig { 
      applicationId "com.neonorb.mish_android" 
      minSdkVersion.apiLevel 15 
      targetSdkVersion.apiLevel 25 
      versionCode 1 
      versionName "1.0" 
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
     } 
     ndk { 
      moduleName "mish-android" 
      ldLibs.add("log") 
      cppFlags.add("-std=c++14") 
      cppFlags.add("-Wno-implicit-exception-spec-mismatch") 
      // ${targetPlatform.getName()} 
      // ${buildType.getName()} 
      stl "c++_static" 
      abiFilters.addAll(["x86_64"]) 
      // only build for x86_64 because that's all Feta and Mish support atm 
     } 
     buildTypes { 
      release { 
       minifyEnabled false 
       proguardFiles.add(file("proguard-rules.pro")) 
      } 
     } 
     sources { 
      main { 
       jni { 
        source { 
         srcDir "src" 
        } 
        dependencies { 
         library "feta" linkage "static" 
         library "mish" linkage "static" 
        } 
       } 
      } 
     } 
    } 
    repositories { 
     libs(PrebuiltLibraries) { 
      feta { 
       headers.srcDir "../../feta/include/" 
       binaries.withType(StaticLibraryBinary) { 
        staticLibraryFile = file("../../feta/build/libfeta.a") 
       } 
      } 
     } 
     libs(PrebuiltLibraries) { 
      mish { 
       headers.srcDir "../../mish/include/" 
       binaries.withType(StaticLibraryBinary) { 
        staticLibraryFile = file("../../mish/build/libmish.a") 
       } 
      } 
     } 
    } 
} 

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' 
    }) 
    compile 'com.android.support:appcompat-v7:25.1.0' 
    testCompile 'junit:junit:4.12' 
} 
Các vấn đề liên quan