Skip to content

Commit

Permalink
save && fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MFM-347 committed Jan 26, 2025
1 parent b4630b8 commit 5b97605
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions _meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"old_package": "dev.mfm.app",
"new_package": "dev.mfm.app",
"name": "AppLate",
"details": "An opensource android template",
"version_name": "1.0.0",
"version_code": "1"
}
Binary file added app/src/main/res/drawable/icon.webp
Binary file not shown.
Binary file added app/src/main/res/drawable/theme.webp
Binary file not shown.
7 changes: 3 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

<ImageView
android:id="@+id/appIcon"
android:src="@drawable/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:tint="?attr/colorPrimary"
android:contentDescription="@string/app_name"
android:src="@drawable/icon"
android:layout_gravity="center"
android:tint="?attr/colorPrimary" />
android:layout_gravity="center" />

</com.google.android.material.card.MaterialCardView>

Expand All @@ -51,7 +51,6 @@
android:layout_height="wrap_content"
android:text="@string/app_details"
android:textAppearance="?attr/textAppearanceBody1"
android:fontFamily="sans-serif"
android:lineSpacingExtra="4dp"
android:layout_marginTop="8dp"
android:paddingHorizontal="24dp"
Expand Down
50 changes: 38 additions & 12 deletions init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def load_metadata(meta_file):
"""Load metadata from the JSON file."""
with open(meta_file, 'r') as file:
return json.load(file)

def replace_in_file(file_path, replacements):
"""Replace text in a file based on a dictionary of replacements."""
with open(file_path, 'r') as file:
Expand All @@ -15,6 +16,8 @@ def replace_in_file(file_path, replacements):
content = content.replace(old, new)
with open(file_path, 'w') as file:
file.write(content)
print(f"Updated file: {file_path}")

def rename_package_path(base_path, old_package, new_package):
"""Rename the package directory path."""
old_path = os.path.join(base_path, *old_package.split('.'))
Expand All @@ -24,50 +27,73 @@ def rename_package_path(base_path, old_package, new_package):
print(f"Renamed package path from {old_path} to {new_path}.")
else:
print(f"Old package path {old_path} does not exist!")
def update_gradle_file(gradle_file, app_name, version_name, version_code, new_package):

def update_gradle_file(gradle_file, app_name, details, version_name, version_code, new_package):
"""Update the app's Gradle build file."""
replacements = {
"$(name)": app_name,
"$(details)": details,
"$(version_name)": version_name,
"$(version_code)": version_code,
"$(application_id)": new_package
}
replace_in_file(gradle_file, replacements)
print(f"Updated Gradle file: {gradle_file}.")
def update_app_name(strings_xml_path, app_name):
"""Update the app_name in the strings.xml file."""

def update_strings_xml(strings_xml_path, app_name, details):
"""Update the app_name and app_details in the strings.xml file."""
if not os.path.exists(strings_xml_path):
print(f"strings.xml not found at: {strings_xml_path}")
return

with open(strings_xml_path, 'r') as file:
content = file.read()
updated_content = re.sub(
r'(<string name="app_name">)(.*?)(</string>)',
rf'\1{app_name}\3',
content
)

# Replace placeholders dynamically
updated_content = re.sub(r'(<string\s+name="app_name">)(.*?)(</string>)', rf'\1{app_name}\3', content)
updated_content = re.sub(r'(<string\s+name="app_details">)(.*?)(</string>)', rf'\1{details}\3', updated_content)

with open(strings_xml_path, 'w') as file:
file.write(updated_content)
print(f"Updated app_name to '{app_name}' in strings.xml.")
print(f"Updated app_name to '{app_name}' and app_details to '{details}' in strings.xml.")

def process_android_project(meta_file_path):
"""Process the Android project to update package name, app name, and Gradle configuration."""
metadata = load_metadata(meta_file_path)

# Ensure required keys exist in the metadata
required_keys = ["old_package", "new_package", "name", "details", "version_name", "version_code"]
for key in required_keys:
if key not in metadata:
print(f"Error: Missing key '{key}' in metadata file.")
return

old_package = metadata["old_package"]
new_package = metadata["new_package"]
app_name = metadata["name"]
details = metadata["details"]
version_name = metadata["version_name"]
version_code = metadata["version_code"]

app_src_main_java = "app/src/main/java/"
gradle_file = "app/build.gradle"
strings_xml_path = "app/src/main/res/values/strings.xml"

# Update package name in Java/Kotlin files
for root, _, files in os.walk(app_src_main_java):
for file in files:
if file.endswith(".java") or file.endswith(".kt"):
file_path = os.path.join(root, file)
replace_in_file(file_path, {f"package {old_package}": f"package {new_package}"})

# Rename package directories
rename_package_path(app_src_main_java, old_package, new_package)
update_gradle_file(gradle_file, app_name, version_name, version_code, new_package)
update_app_name(strings_xml_path, app_name)

# Update Gradle build file
update_gradle_file(gradle_file, app_name, details, version_name, version_code, new_package)

# Update strings.xml
update_strings_xml(strings_xml_path, app_name, details)

print("Project renaming complete!")

if __name__ == "__main__":
Expand Down

0 comments on commit 5b97605

Please sign in to comment.