refactor(android): migrate to new expo modules system

This commit is contained in:
Giau. Tran Minh committed 2022-01-20 15:46:33 +07:00
1 parent e1159b4021
commit 1ac233eb43
10 files changed
+391 -265

No files matched your search

@@ -0,0 +1,261 @@
# Created by https://www.gitignore.io/api/java,maven,gradle,android,intellij,androidstudio
### Android ###
# Built application files
*.apk
*.ap_
# Files for the ART/Dalvik VM
*.dex
# Java class files
*.class
# Generated files
bin/
gen/
out/
# Gradle files
.gradle/
build/
# Local configuration file (sdk path, etc)
local.properties
# Proguard folder generated by Eclipse
proguard/
# Log Files
*.log
# Android Studio Navigation editor temp files
.navigation/
# Android Studio captures folder
captures/
# Intellij
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/dictionaries
.idea/libraries
# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild
# Freeline
freeline.py
freeline/
freeline_project_description.json
### Android Patch ###
gen-external-apklibs
### AndroidStudio ###
# Covers files to be ignored for android development using Android Studio.
# Built application files
# Files for the ART/Dalvik VM
# Java class files
# Generated files
# Gradle files
.gradle
# Signing files
.signing/
# Local configuration file (sdk path, etc)
# Proguard folder generated by Eclipse
# Log Files
# Android Studio
/*/build/
/*/local.properties
/*/out
/*/*/build
/*/*/production
*.ipr
*~
*.swp
# Android Patch
# External native build folder generated in Android Studio 2.2 and later
# NDK
obj/
# IntelliJ IDEA
*.iws
/out/
# User-specific configurations
.idea/libraries/
.idea/.name
.idea/compiler.xml
.idea/copyright/profiles_settings.xml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/scopes/scope_settings.xml
.idea/vcs.xml
.idea/jsLibraryMappings.xml
.idea/datasources.xml
.idea/dataSources.ids
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml
# OS-specific files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# Legacy Eclipse project files
.classpath
.project
.cproject
.settings/
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.war
*.ear
# virtual machine crash logs (Reference: http://www.java.com/en/download/help/error_hotspot.xml)
hs_err_pid*
## Plugin-specific files:
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Mongo Explorer plugin
.idea/mongoSettings.xml
### AndroidStudio Patch ###
!/gradle/wrapper/gradle-wrapper.jar
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea/**/workspace.xml
.idea/**/tasks.xml
# Sensitive or high-churn files:
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
# Gradle:
.idea/**/gradle.xml
.idea/**/libraries
# CMake
cmake-build-debug/
# Mongo Explorer plugin:
.idea/**/mongoSettings.xml
## File-based project format:
## Plugin-specific files:
# IntelliJ
# mpeltonen/sbt-idea plugin
# JIRA plugin
# Cursive Clojure plugin
.idea/replstate.xml
# Ruby plugin and RubyMine
/.rakeTasks
### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
.idea/sonarlint
### Java ###
# Compiled class file
# Log file
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
# Package Files #
*.jar
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
### Maven ###
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
!/.mvn/wrapper/maven-wrapper.jar
### Gradle ###
**/build/
# Ignore Gradle GUI config
gradle-app.setting
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
# Cache of project
.gradletasknamecache
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
# gradle/wrapper/gradle-wrapper.properties
# End of https://www.gitignore.io/api/java,maven,gradle,android,intellij,androidstudio
@@ -0,0 +1,78 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'maven'
group = 'host.exp.exponent'
version = '0.0.1'
buildscript {
// Simple helper that allows the root project to override versions declared by this library.
ext.safeExtGet = { prop, fallback ->
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${safeExtGet('kotlinVersion', '1.4.21')}")
}
}
// Upload android library to maven with javadoc and android sources
configurations {
deployerJars
}
// Creating sources with comments
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
// Put the androidSources and javadoc to the artifacts
artifacts {
archives androidSourcesJar
}
uploadArchives {
repositories {
mavenDeployer {
configuration = configurations.deployerJars
repository(url: mavenLocal().url)
}
}
}
android {
compileSdkVersion safeExtGet("compileSdkVersion", 30)
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion safeExtGet("minSdkVersion", 21)
targetSdkVersion safeExtGet("targetSdkVersion", 30)
versionCode 1
versionName '1.1.0'
}
lintOptions {
abortOnError false
}
}
dependencies {
implementation project(':expo-modules-core')
implementation project(':react-native-branch')
//noinspection GradleDynamicVersion
implementation 'com.facebook.react:react-native:+'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${safeExtGet('kotlinVersion', '1.4.21')}"
}
@@ -0,0 +1,2 @@
<manifest package="expo.modules.adapters.branch">
</manifest>
@@ -0,0 +1,15 @@
package expo.modules.adapters.branch
import android.app.Application
import android.content.Context
import expo.modules.core.interfaces.ApplicationLifecycleListener
import io.branch.rnbranch.RNBranchModule
class BranchApplicationLifecycleListener(context: Context?) : ApplicationLifecycleListener {
var context = context
override fun onCreate(application: Application) {
super.onCreate(application)
RNBranchModule.getAutoInstance(this.context)
}
}
@@ -0,0 +1,15 @@
package expo.modules.adapters.branch
import android.content.Context
import expo.modules.core.BasePackage
import expo.modules.core.interfaces.ApplicationLifecycleListener
import expo.modules.core.interfaces.ReactActivityLifecycleListener
class BranchPackage : BasePackage() {
override fun createApplicationLifecycleListeners(context: Context?): MutableList<ApplicationLifecycleListener> {
return mutableListOf(BranchApplicationLifecycleListener(context))
}
override fun createReactActivityLifecycleListeners(activityContext: Context): List<ReactActivityLifecycleListener> {
return listOf(BranchReactActivityLifecycleListener(activityContext))
}
}
@@ -0,0 +1,13 @@
package expo.modules.adapters.branch
import android.app.Activity
import android.content.Context
import android.os.Bundle
import expo.modules.core.interfaces.ReactActivityLifecycleListener
import io.branch.rnbranch.*
class BranchReactActivityLifecycleListener(activityContext: Context) : ReactActivityLifecycleListener {
override fun onCreate(activity: Activity, savedInstanceState: Bundle?) {
RNBranchModule.initSession(activity.getIntent().getData(), activity);
}
}
-5
View File
@@ -1,13 +1,8 @@
import { AndroidConfig } from "@expo/config-plugins";
import type { ConfigPlugin } from "@expo/config-plugins";
import type { ExpoConfig } from "@expo/config-types";
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
export declare function modifyMainApplication(mainApplication: string, language: "java" | "kt"): string;
export declare function addBranchInitSession(src: string): MergeResults;
export declare function addBranchOnNewIntent(src: string): MergeResults;
export declare function getBranchApiKey(config: ExpoConfig): string | null;
export declare function setBranchApiKey(apiKey: string, androidManifest: AndroidConfig.Manifest.AndroidManifest): AndroidConfig.Manifest.AndroidManifest;
export declare const modifyMainActivity: (mainActivity: string, language: "java" | "kt") => string;
export declare const withBranchAndroid: ConfigPlugin<{
apiKey?: string;
}>;
+1 -105
View File
@@ -1,97 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.withBranchAndroid = exports.modifyMainActivity = exports.setBranchApiKey = exports.getBranchApiKey = exports.addBranchOnNewIntent = exports.addBranchInitSession = exports.modifyMainApplication = void 0;
exports.withBranchAndroid = exports.setBranchApiKey = exports.getBranchApiKey = void 0;
const config_plugins_1 = require("@expo/config-plugins");
const codeMod_1 = require("@expo/config-plugins/build/android/codeMod");
const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode");
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow, removeMetaDataItemFromMainApplication, } = config_plugins_1.AndroidConfig.Manifest;
const META_BRANCH_KEY = "io.branch.sdk.BranchKey";
function addGetAutoInstanceIfNeeded(mainApplication, isJava) {
if (mainApplication.match(/\s+RNBranchModule\.getAutoInstance\(/m)) {
return mainApplication;
}
const semicolon = isJava ? ";" : "";
return codeMod_1.appendContentsInsideDeclarationBlock(mainApplication, "onCreate", ` RNBranchModule.getAutoInstance(this)${semicolon}\n `);
}
function modifyMainApplication(mainApplication, language) {
const isJava = language === "java";
mainApplication = codeMod_1.addImports(mainApplication, ["io.branch.rnbranch.RNBranchModule"], isJava);
mainApplication = addGetAutoInstanceIfNeeded(mainApplication, isJava);
return mainApplication;
}
exports.modifyMainApplication = modifyMainApplication;
function addBranchInitSession(src) {
const tag = "react-native-branch-init-session";
try {
const newSrc = [
" RNBranchModule.initSession(getIntent().getData(), this);",
];
return generateCode_1.mergeContents({
tag,
src,
newSrc: newSrc.join("\n"),
anchor: /super\.onStart\(\);/,
offset: 1,
comment: "//",
});
}
catch (err) {
if (err.code !== "ERR_NO_MATCH") {
throw err;
}
}
const newSrc = [
" @Override",
" protected void onStart() {",
" super.onStart();",
" RNBranchModule.initSession(getIntent().getData(), this);",
" }",
];
return generateCode_1.mergeContents({
tag,
src,
newSrc: newSrc.join("\n"),
anchor: `getMainComponentName`,
offset: 3,
comment: "//",
});
}
exports.addBranchInitSession = addBranchInitSession;
function addBranchOnNewIntent(src) {
const tag = "react-native-branch-on-new-intent";
try {
const newSrc = [" RNBranchModule.onNewIntent(intent);"];
return generateCode_1.mergeContents({
tag,
src,
newSrc: newSrc.join("\n"),
anchor: /super\.onNewIntent\(intent\);/,
offset: 1,
comment: "//",
});
}
catch (err) {
if (err.code !== "ERR_NO_MATCH") {
throw err;
}
}
const newSrc = [
" @Override",
" public void onNewIntent(Intent intent) {",
" super.onNewIntent(intent);",
" RNBranchModule.onNewIntent(intent);",
" }",
];
return generateCode_1.mergeContents({
tag,
src,
newSrc: newSrc.join("\n"),
anchor: `getMainComponentName`,
offset: 3,
comment: "//",
});
}
exports.addBranchOnNewIntent = addBranchOnNewIntent;
function getBranchApiKey(config) {
var _a, _b, _c, _d;
return (_d = (_c = (_b = (_a = config.android) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.branch) === null || _c === void 0 ? void 0 : _c.apiKey) !== null && _d !== void 0 ? _d : null;
@@ -110,14 +22,6 @@ function setBranchApiKey(apiKey, androidManifest) {
return androidManifest;
}
exports.setBranchApiKey = setBranchApiKey;
const modifyMainActivity = (mainActivity, language) => {
const isJava = language === "java";
mainActivity = codeMod_1.addImports(mainActivity, ["android.content.Intent", "io.branch.rnbranch.*"], isJava);
mainActivity = addBranchInitSession(mainActivity).contents;
mainActivity = addBranchOnNewIntent(mainActivity).contents;
return mainActivity;
};
exports.modifyMainActivity = modifyMainActivity;
const withBranchAndroid = (config, data) => {
var _a;
const apiKey = (_a = data.apiKey) !== null && _a !== void 0 ? _a : getBranchApiKey(config);
@@ -128,14 +32,6 @@ const withBranchAndroid = (config, data) => {
config.modResults = setBranchApiKey(apiKey, config.modResults);
return config;
});
config = config_plugins_1.withMainApplication(config, (config) => {
config.modResults.contents = modifyMainApplication(config.modResults.contents, config.modResults.language);
return config;
});
config = config_plugins_1.withMainActivity(config, (config) => {
config.modResults.contents = exports.modifyMainActivity(config.modResults.contents, config.modResults.language);
return config;
});
return config;
};
exports.withBranchAndroid = withBranchAndroid;
@@ -0,0 +1,5 @@
{
"platforms": [
"android"
]
}
@@ -1,17 +1,6 @@
import {
AndroidConfig,
withAndroidManifest,
withMainActivity,
withMainApplication,
} from "@expo/config-plugins";
import {
addImports,
appendContentsInsideDeclarationBlock,
} from "@expo/config-plugins/build/android/codeMod";
import { AndroidConfig, withAndroidManifest } from "@expo/config-plugins";
import type { ConfigPlugin } from "@expo/config-plugins";
import type { ExpoConfig } from "@expo/config-types";
import { mergeContents } from "@expo/config-plugins/build/utils/generateCode";
import type { MergeResults } from "@expo/config-plugins/build/utils/generateCode";
const {
addMetaDataItemToMainApplication,
@@ -21,114 +10,6 @@ const {
const META_BRANCH_KEY = "io.branch.sdk.BranchKey";
function addGetAutoInstanceIfNeeded(
mainApplication: string,
isJava: boolean
): string {
if (mainApplication.match(/\s+RNBranchModule\.getAutoInstance\(/m)) {
return mainApplication;
}
const semicolon = isJava ? ";" : "";
return appendContentsInsideDeclarationBlock(
mainApplication,
"onCreate",
` RNBranchModule.getAutoInstance(this)${semicolon}\n `
);
}
export function modifyMainApplication(
mainApplication: string,
language: "java" | "kt"
): string {
const isJava = language === "java";
mainApplication = addImports(
mainApplication,
["io.branch.rnbranch.RNBranchModule"],
isJava
);
mainApplication = addGetAutoInstanceIfNeeded(mainApplication, isJava);
return mainApplication;
}
export function addBranchInitSession(src: string): MergeResults {
const tag = "react-native-branch-init-session";
try {
const newSrc = [
" RNBranchModule.initSession(getIntent().getData(), this);",
];
return mergeContents({
tag,
src,
newSrc: newSrc.join("\n"),
anchor: /super\.onStart\(\);/,
offset: 1,
comment: "//",
});
} catch (err) {
if ((err as any).code !== "ERR_NO_MATCH") {
throw err;
}
}
const newSrc = [
" @Override",
" protected void onStart() {",
" super.onStart();",
" RNBranchModule.initSession(getIntent().getData(), this);",
" }",
];
return mergeContents({
tag,
src,
newSrc: newSrc.join("\n"),
anchor: `getMainComponentName`,
offset: 3,
comment: "//",
});
}
export function addBranchOnNewIntent(src: string): MergeResults {
const tag = "react-native-branch-on-new-intent";
try {
const newSrc = [" RNBranchModule.onNewIntent(intent);"];
return mergeContents({
tag,
src,
newSrc: newSrc.join("\n"),
anchor: /super\.onNewIntent\(intent\);/,
offset: 1,
comment: "//",
});
} catch (err) {
if ((err as any).code !== "ERR_NO_MATCH") {
throw err;
}
}
const newSrc = [
" @Override",
" public void onNewIntent(Intent intent) {",
" super.onNewIntent(intent);",
" RNBranchModule.onNewIntent(intent);",
" }",
];
return mergeContents({
tag,
src,
newSrc: newSrc.join("\n"),
anchor: `getMainComponentName`,
offset: 3,
comment: "//",
});
}
export function getBranchApiKey(config: ExpoConfig) {
return config.android?.config?.branch?.apiKey ?? null;
}
@@ -150,23 +31,6 @@ export function setBranchApiKey(
return androidManifest;
}
export const modifyMainActivity = (
mainActivity: string,
language: "java" | "kt"
): string => {
const isJava = language === "java";
mainActivity = addImports(
mainActivity,
["android.content.Intent", "io.branch.rnbranch.*"],
isJava
);
mainActivity = addBranchInitSession(mainActivity).contents;
mainActivity = addBranchOnNewIntent(mainActivity).contents;
return mainActivity;
};
export const withBranchAndroid: ConfigPlugin<{ apiKey?: string }> = (
config,
data
@@ -181,23 +45,5 @@ export const withBranchAndroid: ConfigPlugin<{ apiKey?: string }> = (
return config;
});
config = withMainApplication(config, (config) => {
config.modResults.contents = modifyMainApplication(
config.modResults.contents,
config.modResults.language
);
return config;
});
config = withMainActivity(config, (config) => {
config.modResults.contents = modifyMainActivity(
config.modResults.contents,
config.modResults.language
);
return config;
});
return config;
};