SoFunction
Updated on 2025-03-11

Example of method to migrate Eclipse NDK to Android Studio

I recently saw an NDK project. Because the source code is written using Eclipse IDE, I want to import the code into Android Studio. After all, it is much easier to use. After importing using AS, the first problem is the encoding problem. The project actually used GBK encoding before. The first thing is to change the encoding problem. I first changed the project encoding to UTF-8 in the settings. The build result showed a bunch of wrong garbled codes. I walked around the Internet and found a solution.

coding

  1. Swap UTF-8 in the lower right corner of AS with GBK.
  2. A prompt pops up and select "reload", and garbled codes such as comments will be displayed correctly.
  3. Select UTF-8 in the lower right corner
  4. The prompt pops up and selects "convert", and the encoding is converted from GBK to UTF-8.
  5. Compile and run, there will be no garbled errors.
  6. This is the same method for other garbled classes.

NDK support

After importing the project, there is an error in the build prompt:

Error: Flag 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 

Let's remove the middle =true. Then right-click on AS Linked C++ Project. Select cmake or ndk build to link.

  1. cmake: Select file
  2. NDK build: Select file

Or you can add it to your module

externalNativeBuild {
  ndkBuild {
   path 'src/main/jni/'
  }
 }

Unable to import

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

: Couldn't load xxx from loader

It seems that the library cannot be loaded, add it to the module:

sourceSets {
  main {
    = ['libs']
  }
 }

Then add in defaultConfig:

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

The method cannot be found

: No implementation found for int xxxxx

It probably means that the so library has been loaded successfully, but when Java calls the corresponding function, the corresponding c++ function cannot be found.

In this case, don't doubt that the package provided by SDK must copy the package name to the project in its complete copy. The path must correspond to the so function.

text relocations

... has text relocations

Just downgrade targetSdkVersion to 22.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.