Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native codes support for Android #79

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(android): add native codes support
icyleaf committed Nov 27, 2024

Verified

This commit was signed with the committer’s verified signature.
icyleaf icyleaf
commit c2b505c35b4ba4cf38c0ee21e480486ad19f0be9
16 changes: 13 additions & 3 deletions lib/app_info/aab.rb
Original file line number Diff line number Diff line change
@@ -80,23 +80,33 @@ def components
@components ||= manifest.components.transform_values
end

# @param [String] base_path
# @return [Array<String>]
def native_codes(base_path: BASE_PATH)
@native_codes ||= zip.glob(::File.join(base_path, 'lib/**/*'))
.each_with_object([]) do |entry, obj|
lib = entry.name.split('/')[2]
obj << lib unless obj.include?(lib)
end
end

def each_file
zip.each do |entry|
next unless entry.file?

yield entry.name, @zip.read(entry)
yield entry.name, zip.read(entry)
end
end

def read_file(name, base_path: BASE_PATH)
content = @zip.read(entry(name, base_path: base_path))
content = zip.read(entry(name, base_path: base_path))
return parse_binary_xml(content) if xml_file?(name)

content
end

def entry(name, base_path: BASE_PATH)
entry = @zip.find_entry(::File.join(base_path, name))
entry = zip.find_entry(::File.join(base_path, name))
raise NotFoundError, "'#{name}'" if entry.nil?

entry
5 changes: 5 additions & 0 deletions lib/app_info/android.rb
Original file line number Diff line number Diff line change
@@ -136,6 +136,11 @@ def resource
not_implemented_error!(__method__)
end

# @abstract Subclass and override {#native_codes} to implement.
def native_codes
not_implemented_error!(__method__)
end

# @abstract Subclass and override {#zip} to implement.
def zip
not_implemented_error!(__method__)
8 changes: 8 additions & 0 deletions lib/app_info/apk.rb
Original file line number Diff line number Diff line change
@@ -59,6 +59,14 @@ def activities
components.select { |c| c.type == 'activity' }
end

# @return [Array<String>]
def native_codes
@native_codes ||= zip.glob('lib/**/*').each_with_object([]) do |entry, obj|
lib = entry.name.split('/')[1]
obj << lib unless obj.include?(lib)
end
end

# @return [::Android::Apk]
def apk
@apk ||= ::Android::Apk.new(@file)
9 changes: 9 additions & 0 deletions spec/app_info/aab_spec.rb
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@
it { expect(subject.manifest.label).to eq('AppInfoDemo') }
it { expect(subject.manifest.label(locale: 'en')).to eq('AppInfoDemo') }
it { expect(subject.manifest.label(locale: 'zh-CN')).to eq('AppInfo演示') }
it { expect(subject.native_codes).to be_empty }

it { expect(subject.signs).to be_kind_of(Hash) }
it { expect(subject.signs).to have_key('META-INF/KEY0.RSA') }
@@ -82,6 +83,7 @@
it { expect(subject.manifest.label).to eq('My Application') }
it { expect(subject.manifest.label(locale: 'en')).to eq('My Application') }
it { expect(subject.manifest.label(locale: 'zh-CN')).to eq('My Application') }
it { expect(subject.native_codes).to be_empty }
it { expect(subject.certificates).to be_empty }
it { expect(subject.signs).to be_empty }

@@ -109,5 +111,12 @@
expect(icons.size).to eq(0)
end
end

context 'with Native codes' do
let(:file) { fixture_path('apps/android-native-code.aab') }

it { expect(subject.native_codes.size).to eq(7) }
it { expect(subject.native_codes).to contain_exactly('arm64-v8a', 'armeabi', 'armeabi-v7a', 'mips', 'mips64', 'x86', 'x86_64') }
end
end
end
1 change: 1 addition & 0 deletions spec/app_info/android_spec.rb
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
it { expect { subject.services }.to raise_error NotImplementedError }
it { expect { subject.components }.to raise_error NotImplementedError }
it { expect { subject.manifest }.to raise_error NotImplementedError }
it { expect { subject.native_codes }.to raise_error NotImplementedError }
it { expect { subject.resource }.to raise_error NotImplementedError }
it { expect { subject.zip }.to raise_error NotImplementedError }
it { expect { subject.clear! }.to raise_error NotImplementedError }
11 changes: 11 additions & 0 deletions spec/app_info/apk_spec.rb
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@
it { expect(subject.manifest.use_permissions).to eq(subject.use_permissions) }
it { expect(subject.deep_links).to eq(['icyleaf.com']) }
it { expect(subject.schemes).to eq(['appinfo']) }
it { expect(subject.native_codes).to be_empty }

# TODO: it will remove soon.
it { expect(subject.certificates).to be_kind_of(Array) }
@@ -71,6 +72,13 @@
expect(icons.size).to eq(0)
end
end

context 'with Native codes' do
let(:file) { fixture_path('apps/android-native-code.apk') }

it { expect(subject.native_codes.size).to eq(7) }
it { expect(subject.native_codes).to contain_exactly('arm64-v8a', 'armeabi', 'armeabi-v7a', 'mips', 'mips64', 'x86', 'x86_64') }
end
end

describe '#Wear' do
@@ -101,6 +109,7 @@
it { expect(subject.icons.length).not_to be_nil }
it { expect(subject.min_sdk_version).to eq 21 }
it { expect(subject.target_sdk_version).to eq 23 }
it { expect(subject.native_codes).to be_empty }
end

describe '#TV' do
@@ -131,6 +140,7 @@
it { expect(subject.icons.length).not_to be_nil }
it { expect(subject.min_sdk_version).to eq 23 }
it { expect(subject.target_sdk_version).to eq 23 }
it { expect(subject.native_codes).to be_empty }
end

describe '#Automotive' do
@@ -161,5 +171,6 @@
it { expect(subject.icons.length).not_to be_nil }
it { expect(subject.min_sdk_version).to eq 29 }
it { expect(subject.target_sdk_version).to eq 31 }
it { expect(subject.native_codes).to be_empty }
end
end
2 changes: 2 additions & 0 deletions spec/app_info_spec.rb
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@
'android-v1-v2-v3-signed.apk' => :apk,
'android-v2-signed-only.apk' => :apk,
'android-v3-signed-only.apk' => :apk,
'android-native-code.apk' => :apk,
'android-native-code.aab' => :aab,
'tv.apk' => :apk,
'wear.apk' => :apk,
'automotive.apk' => :apk,
Binary file added spec/fixtures/apps/android-native-code.aab
Binary file not shown.
Binary file added spec/fixtures/apps/android-native-code.apk
Binary file not shown.