Creating .so Files in Android Studio 1.0.2 with NDK

I am working on creating a very simple NDKSample application built in accordance with the walkthrough here . My problem: I cannot get Android Studio to generate .so files, so I have no libraries.

I understand that NDK support is now outdated, and an alternative will be offered earlier this year, however, it seems that there is currently nothing active that prevents me from using this feature. When I create my project, I get the following warning (not an error):

WARNING [Project :: app] Current NDK support is out of date. An alternative will be provided in the future.

My project is building, but when I run .apk it crashes (as expected) because it cannot find the /.so library files. We expect that they will be generated when the project is built, as shown in the example, is this correct? Here is the error:

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader [DexPathList [[zip file] "/data/app/com.example.ndksample-1/base.apk"], nativeLibraryDirectories = [/ vendor / lib, / system / lib ]]] could not find "libMyLib.so"

About my environment

Windows 7, Android Studio 1.0.2, ADB runs Nexus 5 (emulator-5554)

My code

According to an example:

Home Activity.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 Note: application

 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 

My questions if anyone can help:

Ultimately, how do I generate the required .so files !! ??

Supporting questions that may help answer the main question:

In my directory location there was my jni file in the application (so NDKSample / app / jni), is this correct? I was asked here not to place c files in the standard jni directory. I tried this, and building the project, it crashed. Mistake:

* FAILURE: assembly failure with exception.

  • What went wrong: Execution failed for task ': app: compileDebugNdk'.

    com.android.ide.common.internal.LoggedErrorException: Failed to execute the 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 *

The above example from Intel does not allow me to create the Android.mk file, the example does not, and it creates a working application. I tried putting it in the jni directory, but that didn't help. Should I create one, and if so, where should I put it

Is the image of my catalogs correct? Directories

Any help would be greatly appreciated.

Kyle

+8
android android-studio android-gradle android-ndk jni
source share
1 answer

The current support for NDK in Android Studio is minimal, so they are deprecated from 1.0.

When using the Android.mk file, it is automatically created and your sources are compiled when creating the project. Now it seems that the compilation is failing, but you should be able to get the initial error message from the Gradle console.

If you don't get any other error messages, I suspect that the ndk-build call fails because there is a place in your NDK installation path ( Android NDK )

You can either fix the current error, or continue the current configuration, or disable the default NDK integration, create Android.mk/Application.mk files and call ndk-build (.cmd) yourself or from a task:

 import org.apache.tools.ant.taskdefs.condition.Os android { sourceSets.main { jniLibs.srcDir 'src/main/libs' //set .so files location to libs jni.srcDirs = [] //disable automatic ndk-build call } // call regular ndk-build(.cmd) script from app directory task ndkBuild(type: Exec) { if (Os.isFamily(Os.FAMILY_WINDOWS)) { commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath } else { commandLine 'ndk-build', '-C', file('src/main').absolutePath } } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn ndkBuild } } 
+13
source share

All Articles