Recently, there was a new requirement for location. The project itself, however, uses a dynamically loaded framework to address the requirements when only plug-ins are updated. First, let’s talk about Android positioning. Due to its own unstable positioning, WHEN I tested it, I could not locate it indoors. In addition, Google services were basically cut by most manufacturers. So for the sake of accuracy and stability, we can only use third-party apps for now. BAT’s three map SDK, Baidu’s and Ali’s, will inject a positioning service into the AndroidMainfest file, which means updating the dynamic loading framework, so these two are definitely not available, so WE choose Tencent’s, which is simple, small in size and convenient to implement. Most importantly, there is no need for AndroidMainfest injection services. However, the documentation said to inject a meta-data to write the ID, so I just changed his JAR package and wrote the ID dead, so I didn’t need to inject it in AndroidMainfest. OK, the next step is to import the. So file provided by Tencent SDK.
Normal method, set up under the main jniLibs, and then in the installation will copy of jniLibs inside to data/app/packageName/libs. The runtime then loads the best matching.so file in this directory, depending on the CPU type.
However, we are loading dynamically and cannot use this method because we cannot specify to save the.so file under data/app/package/ when we package the plugin.
So, have an idea for a start, dynamic loading. So file, is at run time,. So on assets, and then copy to the data/app/context. Getpackage/libs, then load.so, try some kind of, however, found and no, Because the data/app/packageName/libs this directory only can read permission. Cannot write.
In method, realize that System. The load and System. LoadLibrary can dynamically loaded. So, first tried System. Loadlibry, this not because this method, fixed load directory is in the data/app/packageName/libs, So you can’t use system.load() to specify the absolute path, OK OK. It should be noted that system. The load method can only be loaded in the data/app/packageName/libs under or in a private directory (data/data/package /…). Therefore, dynamic copy is also used to store. So in assets. In the code
/** * init location, do some copying. So */ public voidinitLoacation() {
LogUtils.d(Build.CPU_ABI);
File soFile = null;
if (DeviceInfo.getDeviceCpu().equals("armeabi-v7a")) {
soFile = new File(mContext.getFilesDir(), "libtencentloc-v7a.so");
}else if (DeviceInfo.getDeviceCpu().equals("armeabi")) {
soFile = new File(mContext.getFilesDir(), "libtencentloc-v7a.so");
} else if (DeviceInfo.getDeviceCpu().equals("x86")) {
soFile = new File(mContext.getFilesDir(), "libtencentloc-x86.so");
} if (soFile.exists()) {
System.load(soFile.getAbsolutePath());
startLocation();
} else {
copySoFileToLocal(soFile); }}
Copy the code
The above is to determine the CPU architecture, and then different architecture, copy different.So file, SO file I put in assets directory
** @param soFile */ private void copySoFileToLocal(final File soFile) {new Thread(newRunnable() {
@Override
public void run() {
try {
InputStream inputStream = mContext.getResources().getAssets().open(soFile.getName());
FileOutputStream fos = mContext.openFileOutput(soFile.getName(), Context.MODE_PRIVATE);
IOUtils.in2out(inputStream, fos);
System.load(soFile.getAbsolutePath());
LogUtils.d("had load " + soFile.getAbsolutePath());
KyxSDKGlobal.runOnMainThread(new Runnable() {
@Override
public void run() { startLocation(); }}); } catch (IOException e) { e.printStackTrace(); LogUtils.d("error"); } }}).start(); }Copy the code
OK, so I can load.so. Then you can continue to execute the relevant methods of Tencent SDK