You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用NDK存储API密钥遇频繁报错,寻求技术帮助

Troubleshooting NDK API Key Storage Issues

Hey there, sorry to hear you're stuck trying to store your API key via NDK—let's break this down and figure out where things might be going wrong. First, let's recap the steps you've shared to make sure we're on the same page:

  • Created a jni folder under src/main
  • Added Android.mk in the jni folder with this content:
    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE := keys
    LOCAL_SRC_FILES := keys.c
    include $(BUILD_SHARED_LIBRARY)
    
  • Started creating Application.mk but the content was cut off...

Common Fixes & Missing Pieces

1. Complete the Application.mk File

This file is critical for telling the NDK how to build your library. A basic, functional version should specify target ABIs and your app's minimum SDK level:

APP_ABI := arm64-v8a armeabi-v7a # Target common ABIs (or use "all" for full coverage)
APP_PLATFORM := android-21 # Match your app's minSdkVersion from build.gradle
APP_MODULES := keys # Match the LOCAL_MODULE name in Android.mk

2. Implement keys.c Correctly

If this file is missing or has incorrect JNI signatures, you'll get build or runtime errors. Here's a working example that exposes your API key via a native function:

#include <jni.h>
#include <string.h>

// Replace "com_your_package_MainActivity" with your actual package/class path
JNIEXPORT jstring JNICALL
Java_com_your_package_MainActivity_getApiKey(JNIEnv *env, jobject thiz) {
    return (*env)->NewStringUTF(env, "YOUR_SECRET_API_KEY");
}

3. Configure Android Studio for NDK Builds

Add this block to your app-level build.gradle (inside the android section) to link the NDK build files:

externalNativeBuild {
    ndkBuild {
        path file("src/main/jni/Android.mk")
    }
}

Also, ensure the NDK is installed via Android Studio's SDK Manager, and your local.properties points to its location (auto-generated if you install via SDK Manager):

ndk.dir=/Users/yourname/Library/Android/sdk/ndk/25.2.9519653 # Example path

4. Fix Runtime Linking Errors

If you get an UnsatisfiedLinkError, make sure you load the native library in your Java class and declare the native method correctly:

public class MainActivity extends AppCompatActivity {
    // Load the library (matches LOCAL_MODULE name from Android.mk)
    static {
        System.loadLibrary("keys");
    }

    // Declare the native method (matches the signature in keys.c)
    public native String getApiKey();

    // Use it like this:
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String apiKey = getApiKey();
        // Use your API key here
    }
}

Common Error Scenarios to Check

  • "No rule to make target 'keys.c'": Double-check that keys.c exists in the jni folder and the filename matches exactly (case-sensitive!).
  • ABI Mismatches: If your device/emulator uses an ABI not listed in APP_ABI, the library won't load. Stick to widely supported ABIs like arm64-v8a and armeabi-v7a.
  • Signature Mismatches: The JNI function name must exactly match your Java package/class path (dots replaced with underscores). For example, com.example.myapp.MyClass becomes Java_com_example_myapp_MyClass_getApiKey.

If you're still getting specific error messages, share them here! That will help narrow down the problem even faster.

内容的提问来源于stack exchange,提问作者user9427911

火山引擎 最新活动