我一直致力於根據步驟here構建一個非常簡單的NDKSample應用程序。 我的問題是,我無法讓Android Studio生成.so文件,所以我沒有庫。使用NDK在Android Studio 1.0.2中生成.so文件
據我所知,NDK支持已被棄用,今年年初將會有一個替代品,不過目前似乎沒有任何東西阻止我使用該功能。當我建立我的項目,我給出下面的警告(不是錯誤):
WARNING [Project: :app] Current NDK support is deprecated. Alternative will be provided in the future.
我的項目的基礎,但是當我運行它崩潰(如預期),因爲它不能找到這些庫/ .apk文件。所以文件。我們希望這些是在項目建成時產生的,就像這個例子那樣,這是正確的嗎?以下是錯誤:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.ndksample-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "libMyLib.so"
關於我的環境
的Windows 7,Android的工作室1.0.2,亞行正在運行的Nexus 5(模擬器-5554)
我的代碼
按照示例:
主法案ivity.java
package com.example.ndksample;
//import android.support.v7.app.ActionBarActivity;
// This line is not needed as we are not targetting older devices
import android.app.Activity; //Import this app package to use onCreate etc.
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
static{
System.loadLibrary("MyLib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.my_textview);
tv.setText(getStringFromNative());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public native String getStringFromNative();
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="@+id/my_textview"
android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
的main.c
#include "com_example_ndksample_MainActivity.h"
/* Header for class com_example_ndksample_MainActivity */
JNIEXPORT jstring JNICALL Java_com_example_ndksample_app_MainActivity_getStringFromNative
(JNIEnv * env, jobject obj)
{
return (*env)->NewStringUTF(env, "Hello from Kyle");
}
的build.gradle注:應用
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.ndksample"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
ndk {
moduleName "MyLib"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
個
local.properties
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=D\:\\Programs\\Android\\Android SDK
ndk.dir=D\:\\Programs\\Android\\Android NDK
我的問題,如果有人能夠幫助請:
最終,我怎麼生成所需的.so文件!! ??
子問題,這可能會在回答的主要問題有幫助:
我的目錄佈局了下我的應用程序目錄JNI(所以NDKSample /應用/ JNI),這是正確的?我被建議here不要把c文件放在標準的jni目錄中。我對此進行了嘗試,並在構建該項目時崩潰。錯誤:
*失敗:生成失敗,出現異常。
- 出錯: 任務':app:compileDebugNdk'的執行失敗。
com.android.ide.common.internal.LoggedErrorException: Failed to run command: D:\Programs\Android\Android NDK\ndk-build.cmd NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Users\Kyle\AndroidStudioProjects\NDKSample\app\build\intermediates\ndk\debug\Android.mk APP_PLATFORM=android-21 NDK_OUT=C:\Users\Kyle\AndroidStudioProjects\NDKSample\app\build\intermediates\ndk\debug\obj NDK_LIBS_OUT=C:\Users\Kyle\AndroidStudioProjects\NDKSample\app\build\intermediates\ndk\debug\lib APP_ABI=all Error Code: 1*
英特爾上面的例子並不直接我建立一個Android.mk文件,示例不和他產生工作的應用程序。我試過把一個放在jni目錄中,但它沒有幫助。我應該創建一個,如果是的話,我應該把它放在哪裏
我的目錄下面的圖像是否正確?
任何幫助將不勝感激。
凱爾
我有一個問題關於如何在我的android項目中使用.so文件。你可以看到它.http://www.facebook.com/stackoverflow.com/questions/44929511/specify-so-files-in-build-gradle-file-android-studio –