Example method for migrating Eclipse NDK to Android Studio

  • 2021-08-28 21:16:47
  • OfStack

Recently, I saw an NDK project. Because the source code was written by Eclipse IDE, I want to import the code into Android Studio. After all, it is much easier to use. After importing AS, the first problem is coding. Before the project, I actually used GBK coding. The first is the problem of changing the coding. I first changed the project code to UTF-8 in the settings, and the result of build showed a pile of wrong garbled codes. I walked around the Internet for 1 circle and found a solution.

Code

Replace UTF-8 in the lower right corner of AS with GBK. Pop up the prompt and select "reload", and the garbled code such as comments will appear correctly. Select UTF-8 in the lower right corner Pop up the prompt and select "convert". At this time, the code changes from GBK to UTF-8. Compile and run, there will be no garbled errors. This is also the way for other garbled classes

NDK support

After importing the project, build is prompted with an error:

Error: Flag android.useDeprecatedNdk is no longer supported and will be removed in the next version of Android Studio. Please switch to a supported build system.
Consider using CMake or ndk-build integration. For more information

We remove android. useDeprecatedNdk=true from gradle. properties. Then right-click Linked C + + Project directly on AS. Select cmake or ndk build to link.

cmake: Select the CMakeLists. txt file NDK build: Select the Android. mk file

Or you can add it to your module


externalNativeBuild {
  ndkBuild {
   path 'src/main/jni/Android.mk'
  }
 }

Unable to import

After ndk support, now run the project, the project can be started, but run directly crash, crash log is:

java.lang.UnsatisfiedLinkError: Couldn't load xxx from loader dalvik.system.PathClassLoader

It seems that the library cannot be loaded, so add:


sourceSets {
  main {
   jniLibs.srcDirs = ['libs']
  }
 }

Then add in defaultConfig:


ndk {
   moduleName "your ndk module name"
   abiFilters "armeabi", "armeabi-v7a", "x86"
  }

Method not found

java.lang.UnsatisfiedLinkError: No implementation found for int xxxxx

The so library was loaded successfully, but when java called the corresponding function, the corresponding c + + function could not be found.

In this case, don't doubt that the package 1 provided by sdk must copy the package name completely to the project. The path should correspond to the so function.

text relocations

java.lang.UnsatisfiedLinkError...xxx.so has text relocations

Just downgrade targetSdkVersion to 22.


Related articles: