Skip to content

Instantly share code, notes, and snippets.

View wajahatkarim3's full-sized avatar
:octocat:
Subscribe to my newsletter: wajahatkarim.com/subscribe

Wajahat Karim wajahatkarim3

:octocat:
Subscribe to my newsletter: wajahatkarim.com/subscribe
View GitHub Profile
View Demo.kt
object YourKeys {
init {
System.loadLibrary("native-lib")
}
external fun apiKey(): String
}
View Demo.cpp
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstringJNICALL
Java_com_package_name_YourKeys_apiKey(JNIEnv *env, jobject object) {
std::string api_key = "your_api_key";
return env->NewStringUTF(api_key.c_str());
View Demo.kt
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.texturelabs.rosera.pop_movies"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
View Demo.kt
object Constants {
const val API_KEY = "abcdefg2q4qsaf34rtyy421k"
}
View strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--API Key-->
<string name="api_key">abcdefg2q4qsaf34rtyy421k</string>
</resources>
View Demo.kt
val securePreferences = EncryptedSharedPreferences.create(
"secure_prefs",
mainKeyAlias,
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
// Storing Data in Preferences
with(securePreferences.edit()) {
View Demo.kt
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
View Demo.kt
val preferences = getSharedPreferences("normal_prefs", MODE_PRIVATE)
// Storing Data in Preferences
with(preferences.edit()) {
putBoolean("Bool", true)
putString("String", "Some normal string value")
putInt("Integer", 10)
commit()
}
View Demo.kt
object CustomerData {
var customerId = “123”
var name = “John”
var totalBill = 284.34
var purchasedItems: List<Item> = arrayListOf()
fun addItem(item: Item) {
purchasedItems.add(item)
totalBill = calculateBill()
}
View Demo.kt
// Get the DataStore object
private val settingsDataStore by preferencesDataStore(name = “app_settings”)
// Get the keys
val myStrKey = stringPreferencesKey("myStrKey")
// Read the values through coroutine scopes
lifecycleScope.launch {
settingsDataStore.data.map { pref ->
// Read the values here